Skip to content

Commit

Permalink
add doc and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
soulomoon authored and sorki committed Apr 22, 2022
1 parent c90b261 commit e3723c7
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions hnix-store-core/src/System/Nix/Internal/Nar/Streamer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{-# language ScopedTypeVariables #-}

module System.Nix.Internal.Nar.Streamer
( NarSource
( NarSource
, dumpString
, dumpPath
, streamNarIO
Expand All @@ -22,28 +22,32 @@ import System.FilePath ( (</>) )
import qualified System.Nix.Internal.Nar.Effects as Nar


-- | NarSource
-- the source to provide nar to the handler
-- | NarSource
-- The source to provide nar to the handler `(ByteString -> m ())`.
-- It is isomorphic to ByteString by Yoneda lemma
-- if the result is meant to be m ().
-- It is done in CPS style so IO can be chunks.
type NarSource m = (ByteString -> m ()) -> m ()


-- | dumpString
-- dump a string to nar
dumpString :: forall m . IO.MonadIO m => ByteString -> NarSource m
dumpString text yield = do
yield $ str "nix-archive-1"
yield $ str "("
yield $ str "type"
yield $ str "regular"
yield $ str "contents"
yield $ str text
yield $ str ")"
-- dump a string to nar in CPS style. The function takes in a `ByteString`,
-- and build a `NarSource m`.
dumpString
:: forall m. IO.MonadIO m
=> ByteString -- ^ the string you want to dump
-> NarSource m -- ^ The nar result in CPS style
dumpString text yield = traverse_ (yield . str)
["nix-archive-1", "(", "type" , "regular", "contents", text, ")"]


-- | dumpPath
-- shorthand
-- build a Source that turn file path to nar using the default narEffectsIO.
dumpPath :: forall m . IO.MonadIO m => FilePath -> NarSource m
dumpPath
:: forall m . IO.MonadIO m
=> FilePath -- ^ path for the file you want to dump to nar
-> NarSource m -- ^ the nar result in CPS style
dumpPath = streamNarIO Nar.narEffectsIO


Expand All @@ -53,9 +57,7 @@ dumpPath = streamNarIO Nar.narEffectsIO
streamNarIO :: forall m . IO.MonadIO m => Nar.NarEffects IO -> FilePath -> NarSource m
streamNarIO effs basePath yield = do
yield $ str "nix-archive-1"
yield $ str "("
go basePath
yield $ str ")"
parens $ go basePath
where
go :: FilePath -> m ()
go path = do
Expand Down Expand Up @@ -98,7 +100,6 @@ streamNarIO effs basePath yield = do
yieldFile path fsize = do
mapM_ yield . Bytes.Lazy.toChunks =<< IO.liftIO (Bytes.Lazy.readFile path)
yield $ Bytes.replicate (padLen $ fromIntegral fsize) 0


data IsExecutable = NonExecutable | Executable
deriving (Eq, Show)
Expand Down Expand Up @@ -128,4 +129,4 @@ padBS :: Int -> ByteString -> ByteString
padBS strSize bs = bs <> Bytes.replicate (padLen strSize) 0

strs :: [ByteString] -> ByteString
strs xs = Bytes.concat $ str <$> xs
strs xs = Bytes.concat $ str <$> xs

0 comments on commit e3723c7

Please sign in to comment.