{-# LANGUAGE RecordWildCards   #-}
{-# LANGUAGE OverloadedStrings #-}

module Nix.NarInfo.Builder
    ( -- * 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 = 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
ca :: forall fp txt hash. NarInfo fp txt hash -> Maybe txt
sig :: forall fp txt hash. NarInfo fp txt hash -> Maybe txt
system :: forall fp txt hash. NarInfo fp txt hash -> Maybe txt
deriver :: forall fp txt hash. NarInfo fp txt hash -> Maybe txt
references :: forall fp txt hash. NarInfo fp txt hash -> Set fp
narSize :: forall fp txt hash. NarInfo fp txt hash -> Integer
narHash :: forall fp txt hash. NarInfo fp txt hash -> hash
fileSize :: forall fp txt hash. NarInfo fp txt hash -> Integer
fileHash :: forall fp txt hash. NarInfo fp txt hash -> hash
compression :: forall fp txt hash. NarInfo fp txt hash -> txt
url :: forall fp txt hash. NarInfo fp txt hash -> txt
storePath :: forall fp txt hash. NarInfo fp txt hash -> fp
ca :: Maybe txt
sig :: Maybe txt
system :: Maybe txt
deriver :: Maybe txt
references :: Set fp
narSize :: Integer
narHash :: hash
fileSize :: Integer
fileHash :: hash
compression :: txt
url :: txt
storePath :: fp
..}) =
     Builder -> fp -> Builder
keyPath Builder
"StorePath"   fp
storePath
  forall a. Semigroup a => a -> a -> a
<> Builder -> txt -> Builder
key     Builder
"URL"         txt
url
  forall a. Semigroup a => a -> a -> a
<> Builder -> txt -> Builder
key     Builder
"Compression" txt
compression
  forall a. Semigroup a => a -> a -> a
<> Builder -> hash -> Builder
keyHash Builder
"FileHash"    hash
fileHash
  forall a. Semigroup a => a -> a -> a
<> forall {p}. Show p => Builder -> p -> Builder
keyNum  Builder
"FileSize"    Integer
fileSize
  forall a. Semigroup a => a -> a -> a
<> Builder -> hash -> Builder
keyHash Builder
"NarHash"     hash
narHash
  forall a. Semigroup a => a -> a -> a
<> forall {p}. Show p => Builder -> p -> Builder
keyNum  Builder
"NarSize"     Integer
narSize

  forall a. Semigroup a => a -> a -> a
<> forall {a}. (Semigroup a, IsString a) => a -> a -> a
key'    Builder
"References"  (forall a. Monoid a => [a] -> a
mconcat
      forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a]
Data.List.intersperse Builder
" "
        forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (Bool -> fp -> Builder
filepath Bool
False)
          forall a b. (a -> b) -> a -> b
$ forall a. Ord a => [a] -> [a]
Data.List.sort forall a b. (a -> b) -> a -> b
$ forall a. Set a -> [a]
Data.Set.toList Set fp
references)

  forall a. Semigroup a => a -> a -> a
<> Builder -> Maybe txt -> Builder
optKey  Builder
"Deriver"     Maybe txt
deriver
  forall a. Semigroup a => a -> a -> a
<> Builder -> Maybe txt -> Builder
optKey  Builder
"System"      Maybe txt
system
  forall a. Semigroup a => a -> a -> a
<> Builder -> Maybe txt -> Builder
optKey  Builder
"Sig"         Maybe txt
sig
  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 forall a. Semigroup a => a -> a -> a
<> a
": " forall a. Semigroup a => a -> a -> a
<> a
v forall a. Semigroup a => a -> a -> a
<> a
"\n"
    key :: Builder -> txt -> Builder
key Builder
k txt
v     = 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  = forall {a}. (Semigroup a, IsString a) => a -> a -> a
key' Builder
k (FilePath -> Builder
Data.Text.Lazy.Builder.fromString forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> FilePath
show forall a b. (a -> b) -> a -> b
$ p
v)
    keyPath :: Builder -> fp -> Builder
keyPath Builder
k fp
v = 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 = 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    = forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (Builder -> txt -> Builder
key Builder
k)