{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
module Nix.NarInfo.Builder
(
buildNarInfo
, buildNarInfoWith
) where
import Data.Set (Set)
import Data.Text (Text)
import Data.Text.Lazy.Builder (Builder)
import Nix.NarInfo.Types
import qualified Control.Applicative
import qualified Data.Char
import qualified Data.Set
import qualified Data.List
import qualified Data.Text
import qualified Data.Text.Lazy.Builder
buildNarInfo :: (NarInfo FilePath Text Text) -> Builder
buildNarInfo :: NarInfo FilePath Text Text -> Builder
buildNarInfo = (Bool -> FilePath -> Builder)
-> (Text -> Builder)
-> (Text -> Builder)
-> NarInfo FilePath Text Text
-> Builder
forall fp txt hash.
Ord fp =>
(Bool -> fp -> Builder)
-> (txt -> Builder)
-> (hash -> Builder)
-> NarInfo fp txt hash
-> Builder
buildNarInfoWith
(\Bool
_needPrefix FilePath
fp -> FilePath -> Builder
Data.Text.Lazy.Builder.fromString FilePath
fp)
Text -> Builder
Data.Text.Lazy.Builder.fromText
Text -> Builder
Data.Text.Lazy.Builder.fromText
buildNarInfoWith :: (Ord fp)
=> (Bool -> fp -> Builder)
-> (txt -> Builder)
-> (hash -> Builder)
-> NarInfo fp txt hash
-> Builder
buildNarInfoWith :: forall fp txt hash.
Ord fp =>
(Bool -> fp -> Builder)
-> (txt -> Builder)
-> (hash -> Builder)
-> NarInfo fp txt hash
-> Builder
buildNarInfoWith Bool -> fp -> Builder
filepath txt -> Builder
string hash -> Builder
hash (NarInfo{fp
txt
hash
Integer
Maybe txt
Set fp
storePath :: fp
url :: txt
compression :: txt
fileHash :: hash
fileSize :: Integer
narHash :: hash
narSize :: Integer
references :: Set fp
deriver :: Maybe txt
system :: Maybe txt
sig :: Maybe txt
ca :: Maybe txt
storePath :: forall fp txt hash. NarInfo fp txt hash -> fp
url :: forall fp txt hash. NarInfo fp txt hash -> txt
compression :: forall fp txt hash. NarInfo fp txt hash -> txt
fileHash :: forall fp txt hash. NarInfo fp txt hash -> hash
fileSize :: forall fp txt hash. NarInfo fp txt hash -> Integer
narHash :: forall fp txt hash. NarInfo fp txt hash -> hash
narSize :: forall fp txt hash. NarInfo fp txt hash -> Integer
references :: forall fp txt hash. NarInfo fp txt hash -> Set fp
deriver :: forall fp txt hash. NarInfo fp txt hash -> Maybe txt
system :: forall fp txt hash. NarInfo fp txt hash -> Maybe txt
sig :: forall fp txt hash. NarInfo fp txt hash -> Maybe txt
ca :: forall fp txt hash. NarInfo fp txt hash -> Maybe txt
..}) =
Builder -> fp -> Builder
keyPath Builder
"StorePath" fp
storePath
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder -> txt -> Builder
key Builder
"URL" txt
url
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder -> txt -> Builder
key Builder
"Compression" txt
compression
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder -> hash -> Builder
keyHash Builder
"FileHash" hash
fileHash
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder -> Integer -> Builder
forall {p}. Show p => Builder -> p -> Builder
keyNum Builder
"FileSize" Integer
fileSize
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder -> hash -> Builder
keyHash Builder
"NarHash" hash
narHash
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder -> Integer -> Builder
forall {p}. Show p => Builder -> p -> Builder
keyNum Builder
"NarSize" Integer
narSize
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder -> Builder -> Builder
forall {a}. (Semigroup a, IsString a) => a -> a -> a
key' Builder
"References" ([Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat
([Builder] -> Builder) -> [Builder] -> Builder
forall a b. (a -> b) -> a -> b
$ Builder -> [Builder] -> [Builder]
forall a. a -> [a] -> [a]
Data.List.intersperse Builder
" "
([Builder] -> [Builder]) -> [Builder] -> [Builder]
forall a b. (a -> b) -> a -> b
$ (fp -> Builder) -> [fp] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> fp -> Builder
filepath Bool
False)
([fp] -> [Builder]) -> [fp] -> [Builder]
forall a b. (a -> b) -> a -> b
$ [fp] -> [fp]
forall a. Ord a => [a] -> [a]
Data.List.sort ([fp] -> [fp]) -> [fp] -> [fp]
forall a b. (a -> b) -> a -> b
$ Set fp -> [fp]
forall a. Set a -> [a]
Data.Set.toList Set fp
references)
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder -> Maybe txt -> Builder
optKey Builder
"Deriver" Maybe txt
deriver
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder -> Maybe txt -> Builder
optKey Builder
"System" Maybe txt
system
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder -> Maybe txt -> Builder
optKey Builder
"Sig" Maybe txt
sig
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder -> Maybe txt -> Builder
optKey Builder
"Ca" Maybe txt
ca
where
key' :: a -> a -> a
key' a
k a
v = a
k a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
": " a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
v a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
"\n"
key :: Builder -> txt -> Builder
key Builder
k txt
v = Builder -> Builder -> Builder
forall {a}. (Semigroup a, IsString a) => a -> a -> a
key' Builder
k (txt -> Builder
string txt
v)
keyNum :: Builder -> p -> Builder
keyNum Builder
k p
v = Builder -> Builder -> Builder
forall {a}. (Semigroup a, IsString a) => a -> a -> a
key' Builder
k (FilePath -> Builder
Data.Text.Lazy.Builder.fromString (FilePath -> Builder) -> (p -> FilePath) -> p -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p -> FilePath
forall a. Show a => a -> FilePath
show (p -> Builder) -> p -> Builder
forall a b. (a -> b) -> a -> b
$ p
v)
keyPath :: Builder -> fp -> Builder
keyPath Builder
k fp
v = Builder -> Builder -> Builder
forall {a}. (Semigroup a, IsString a) => a -> a -> a
key' Builder
k (Bool -> fp -> Builder
filepath Bool
True fp
v)
keyHash :: Builder -> hash -> Builder
keyHash Builder
k hash
v = Builder -> Builder -> Builder
forall {a}. (Semigroup a, IsString a) => a -> a -> a
key' Builder
k (hash -> Builder
hash hash
v)
optKey :: Builder -> Maybe txt -> Builder
optKey Builder
k = Builder -> (txt -> Builder) -> Maybe txt -> Builder
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Builder
forall a. Monoid a => a
mempty (Builder -> txt -> Builder
key Builder
k)