{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
module Nix.NarInfo.Parser
(
parseNarInfo
, parseNarInfoWith
) where
import Data.Set (Set)
import Data.Text (Text)
import Data.Attoparsec.Text (Parser)
import Nix.NarInfo.Types
import qualified Control.Applicative
import qualified Data.Char
import qualified Data.Set
import qualified Data.Text
import qualified Data.Attoparsec.Text
parseNarInfo :: Parser (NarInfo FilePath Text Text)
parseNarInfo :: Parser (NarInfo FilePath Text Text)
parseNarInfo = (Bool -> Parser FilePath)
-> Parser Text
-> Parser Text
-> Parser (NarInfo FilePath Text Text)
forall fp txt hash.
Ord fp =>
(Bool -> Parser fp)
-> Parser txt -> Parser hash -> Parser (NarInfo fp txt hash)
parseNarInfoWith Bool -> Parser FilePath
forall p. p -> Parser FilePath
pathParse Parser Text
textParse Parser Text
hashParse
where
textParse :: Parser Text
textParse = (Char -> Bool) -> Parser Text
Data.Attoparsec.Text.takeWhile (Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
Data.Char.isSpace)
pathParse :: p -> Parser FilePath
pathParse _hasPrefix :: p
_hasPrefix = Text -> FilePath
Data.Text.unpack (Text -> FilePath) -> Parser Text -> Parser FilePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text
textParse
hashParse :: Parser Text
hashParse = Parser Text
textParse
parseNarInfoWith :: (Ord fp)
=> (Bool -> Parser fp)
-> Parser txt
-> Parser hash
-> Parser (NarInfo fp txt hash)
parseNarInfoWith :: (Bool -> Parser fp)
-> Parser txt -> Parser hash -> Parser (NarInfo fp txt hash)
parseNarInfoWith pathParser :: Bool -> Parser fp
pathParser textParser :: Parser txt
textParser hashParser :: Parser hash
hashParser = do
fp
storePath <- Text -> Parser fp
keyPath "StorePath"
txt
url <- Text -> Parser txt
key "URL"
txt
compression <- Text -> Parser txt
key "Compression"
hash
fileHash <- Text -> Parser hash
keyHash "FileHash"
Integer
fileSize <- Text -> Parser Text Integer
forall b. Integral b => Text -> Parser Text b
keyNum "FileSize"
hash
narHash <- Text -> Parser hash
keyHash "NarHash"
Integer
narSize <- Text -> Parser Text Integer
forall b. Integral b => Text -> Parser Text b
keyNum "NarSize"
Set fp
references <- [fp] -> Set fp
forall a. Ord a => [a] -> Set a
Data.Set.fromList ([fp] -> Set fp) -> Parser Text [fp] -> Parser Text (Set fp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> Parser Text [fp] -> Parser Text [fp]
forall b. Text -> Parser Text b -> Parser Text b
parseKey "References" (Parser Text [fp] -> Parser Text [fp])
-> Parser Text [fp] -> Parser Text [fp]
forall a b. (a -> b) -> a -> b
$
(Bool -> Parser fp
pathParser Bool
False) Parser fp -> Parser Text Char -> Parser Text [fp]
forall (f :: * -> *) a s. Alternative f => f a -> f s -> f [a]
`Data.Attoparsec.Text.sepBy` Char -> Parser Text Char
Data.Attoparsec.Text.char ' ')
Maybe txt
deriver <- Text -> Parser Text (Maybe txt)
optKey "Deriver"
Maybe txt
system <- Text -> Parser Text (Maybe txt)
optKey "System"
Maybe txt
sig <- Text -> Parser Text (Maybe txt)
optKey "Sig"
Maybe txt
ca <- Text -> Parser Text (Maybe txt)
optKey "Ca"
NarInfo fp txt hash -> Parser (NarInfo fp txt hash)
forall (m :: * -> *) a. Monad m => a -> m a
return (NarInfo fp txt hash -> Parser (NarInfo fp txt hash))
-> NarInfo fp txt hash -> Parser (NarInfo fp txt hash)
forall a b. (a -> b) -> a -> b
$ NarInfo :: forall fp txt hash.
fp
-> txt
-> txt
-> hash
-> Integer
-> hash
-> Integer
-> Set fp
-> Maybe txt
-> Maybe txt
-> Maybe txt
-> Maybe txt
-> NarInfo fp txt hash
NarInfo {..}
where
parseKey :: Text -> Parser Text b -> Parser Text b
parseKey key :: Text
key parser :: Parser Text b
parser = do
Text -> Parser Text
Data.Attoparsec.Text.string Text
key
Text -> Parser Text
Data.Attoparsec.Text.string ": "
b
out <- Parser Text b
parser
Char -> Parser Text Char
Data.Attoparsec.Text.char '\n'
b -> Parser Text b
forall (m :: * -> *) a. Monad m => a -> m a
return b
out
key :: Text -> Parser txt
key = (Text -> Parser txt -> Parser txt)
-> Parser txt -> Text -> Parser txt
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> Parser txt -> Parser txt
forall b. Text -> Parser Text b -> Parser Text b
parseKey Parser txt
textParser
optKey :: Text -> Parser Text (Maybe txt)
optKey = Parser txt -> Parser Text (Maybe txt)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
Control.Applicative.optional (Parser txt -> Parser Text (Maybe txt))
-> (Text -> Parser txt) -> Text -> Parser Text (Maybe txt)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Parser txt
key
keyNum :: Text -> Parser Text b
keyNum x :: Text
x = Text -> Parser Text b -> Parser Text b
forall b. Text -> Parser Text b -> Parser Text b
parseKey Text
x Parser Text b
forall a. Integral a => Parser a
Data.Attoparsec.Text.decimal
keyPath :: Text -> Parser fp
keyPath x :: Text
x = Text -> Parser fp -> Parser fp
forall b. Text -> Parser Text b -> Parser Text b
parseKey Text
x (Bool -> Parser fp
pathParser Bool
True)
keyHash :: Text -> Parser hash
keyHash x :: Text
x = Text -> Parser hash -> Parser hash
forall b. Text -> Parser Text b -> Parser Text b
parseKey Text
x Parser hash
hashParser