Skip to content

Commit 8ac46de

Browse files
committed
Recursive -> FileIngestionMethod, remove Bools
Actually the same thing, now with prefixed constructors for extra clarity. Closes #238
1 parent f6b06e0 commit 8ac46de

File tree

12 files changed

+57
-39
lines changed

12 files changed

+57
-39
lines changed

hnix-store-core/hnix-store-core.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ library
7373
, System.Nix.Nar.Options
7474
, System.Nix.ReadonlyStore
7575
, System.Nix.Signature
76+
, System.Nix.Store.Types
7677
, System.Nix.StorePath
7778
, System.Nix.StorePath.Metadata
7879
build-depends:

hnix-store-core/src/System/Nix/ContentAddress.hs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,13 @@ import Data.Text (Text)
1818
import Data.Text.Lazy.Builder (Builder)
1919
import GHC.Generics (Generic)
2020
import System.Nix.Hash (HashAlgo)
21+
import System.Nix.Store.Types (FileIngestionMethod(..))
2122

2223
import qualified Data.Attoparsec.Text
2324
import qualified Data.Text.Lazy
2425
import qualified Data.Text.Lazy.Builder
2526
import qualified System.Nix.Hash
2627

27-
data FileIngestionMethod
28-
= Flat
29-
| FileRecursive
30-
deriving (Eq, Bounded, Generic, Enum, Ord, Show)
31-
3228
data ContentAddressMethod
3329
= FileIngestionMethod !FileIngestionMethod
3430
-- ^ The path was added to the store via makeFixedOutputPath or
@@ -74,8 +70,8 @@ contentAddressBuilder (ContentAddress method digest) = case method of
7470

7571
fileIngestionMethodBuilder :: FileIngestionMethod -> Builder
7672
fileIngestionMethodBuilder = \case
77-
Flat -> ""
78-
FileRecursive -> "r:"
73+
FileIngestionMethod_Flat -> ""
74+
FileIngestionMethod_FileRecursive -> "r:"
7975

8076
-- | Parse `ContentAddressableAddress` from `ByteString`
8177
parseContentAddress
@@ -95,7 +91,9 @@ contentAddressParser = do
9591
parseContentAddressMethod :: Parser ContentAddressMethod
9692
parseContentAddressMethod =
9793
TextIngestionMethod <$ "text:"
98-
<|> FileIngestionMethod <$ "fixed:" <*> (FileRecursive <$ "r:" <|> pure Flat)
94+
<|> FileIngestionMethod <$ "fixed:"
95+
<*> (FileIngestionMethod_FileRecursive <$ "r:"
96+
<|> pure FileIngestionMethod_Flat)
9997

10098
parseTypedDigest :: Parser (Either String (DSum HashAlgo Digest))
10199
parseTypedDigest = System.Nix.Hash.mkNamedDigest <$> parseHashType <*> parseHash

hnix-store-core/src/System/Nix/ReadonlyStore.hs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Crypto.Hash (Context, Digest, SHA256)
1313
import Data.ByteString (ByteString)
1414
import Data.HashSet (HashSet)
1515
import System.Nix.Hash (BaseEncoding(Base16), NamedAlgo(algoName))
16+
import System.Nix.Store.Types (FileIngestionMethod(..))
1617
import System.Nix.StorePath (StoreDir, StorePath(StorePath), StorePathName)
1718

1819
import qualified Crypto.Hash
@@ -66,20 +67,21 @@ makeFixedOutputPath
6667
:: forall hashAlgo
6768
. NamedAlgo hashAlgo
6869
=> StoreDir
69-
-> Bool
70+
-> FileIngestionMethod
7071
-> Digest hashAlgo
7172
-> StorePathName
7273
-> StorePath
7374
makeFixedOutputPath storeDir recursive h =
74-
if recursive && (algoName @hashAlgo) == "sha256"
75-
then makeStorePath storeDir "source" h
76-
else makeStorePath storeDir "output:out" h'
75+
if recursive == FileIngestionMethod_FileRecursive
76+
&& (algoName @hashAlgo) == "sha256"
77+
then makeStorePath storeDir "source" h
78+
else makeStorePath storeDir "output:out" h'
7779
where
7880
h' =
7981
Crypto.Hash.hash @ByteString @SHA256
8082
$ "fixed:out:"
8183
<> Data.Text.Encoding.encodeUtf8 (algoName @hashAlgo)
82-
<> (if recursive then ":r:" else ":")
84+
<> (if recursive == FileIngestionMethod_FileRecursive then ":r:" else ":")
8385
<> Data.Text.Encoding.encodeUtf8 (System.Nix.Hash.encodeDigestWith Base16 h)
8486
<> ":"
8587

@@ -96,12 +98,15 @@ computeStorePathForPath
9698
:: StoreDir
9799
-> StorePathName -- ^ Name part of the newly created `StorePath`
98100
-> FilePath -- ^ Local `FilePath` to add
99-
-> Bool -- ^ Add target directory recursively
101+
-> FileIngestionMethod -- ^ Add target directory recursively
100102
-> (FilePath -> Bool) -- ^ Path filter function
101103
-> Bool -- ^ Only used by local store backend
102104
-> IO StorePath
103105
computeStorePathForPath storeDir name pth recursive _pathFilter _repair = do
104-
selectedHash <- if recursive then recursiveContentHash else flatContentHash
106+
selectedHash <-
107+
if recursive == FileIngestionMethod_FileRecursive
108+
then recursiveContentHash
109+
else flatContentHash
105110
pure $ makeFixedOutputPath storeDir recursive selectedHash name
106111
where
107112
recursiveContentHash :: IO (Digest SHA256)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module System.Nix.Store.Types
2+
( FileIngestionMethod(..)
3+
) where
4+
5+
import GHC.Generics (Generic)
6+
7+
-- | Add path recursively or not
8+
data FileIngestionMethod
9+
= FileIngestionMethod_Flat
10+
| FileIngestionMethod_FileRecursive
11+
deriving (Bounded, Eq, Generic, Enum, Ord, Show)

hnix-store-core/tests/ReadOnly.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Test.Hspec (Spec, describe, it, shouldBe)
88
import Crypto.Hash (hash, Digest, SHA256(..))
99
import Data.ByteString (ByteString)
1010
import System.Nix.StorePath (StorePath, StorePathName)
11+
import System.Nix.Store.Types (FileIngestionMethod(..))
1112

1213
import qualified Data.HashSet
1314
import qualified System.Nix.StorePath
@@ -84,7 +85,7 @@ spec_readOnly = do
8485
(pure
8586
$ makeFixedOutputPath
8687
def
87-
True
88+
FileIngestionMethod_FileRecursive
8889
testDigest
8990
testName
9091
)
@@ -97,7 +98,7 @@ spec_readOnly = do
9798
(pure
9899
$ makeFixedOutputPath
99100
def
100-
False
101+
FileIngestionMethod_Flat
101102
testDigest
102103
testName
103104
)

hnix-store-remote/src/System/Nix/Store/Remote.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import qualified System.Nix.Hash
4343
import qualified Data.ByteString.Lazy as BSL
4444

4545
import System.Nix.Derivation (Derivation)
46+
import System.Nix.Store.Types (FileIngestionMethod(..))
4647
import System.Nix.Build ( BuildMode
4748
, BuildResult
4849
)
@@ -80,7 +81,7 @@ addToStore
8081
. (NamedAlgo a)
8182
=> StorePathName -- ^ Name part of the newly created `StorePath`
8283
-> NarSource MonadStore -- ^ provide nar stream
83-
-> Recursive -- ^ Add target directory recursively
84+
-> FileIngestionMethod -- ^ Add target directory recursively
8485
-> RepairFlag -- ^ Only used by local store backend
8586
-> MonadStore StorePath
8687
addToStore name source recursive repair = do
@@ -90,8 +91,11 @@ addToStore name source recursive repair = do
9091
runOpArgsIO AddToStore $ \yield -> do
9192
yield $ BSL.toStrict $ Data.Binary.Put.runPut $ do
9293
putText $ System.Nix.StorePath.unStorePathName name
93-
putBool $ not $ System.Nix.Hash.algoName @a == "sha256" && (unRecursive recursive)
94-
putBool (unRecursive recursive)
94+
putBool
95+
$ not
96+
$ System.Nix.Hash.algoName @a == "sha256"
97+
&& recursive == FileIngestionMethod_FileRecursive
98+
putBool (recursive == FileIngestionMethod_FileRecursive)
9599
putText $ System.Nix.Hash.algoName @a
96100
source yield
97101
sockGetPath

hnix-store-remote/src/System/Nix/Store/Remote/Types.hs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ module System.Nix.Store.Remote.Types
1313
, doSubstitute
1414
, dontSubstitute
1515
, unSubstituteFlag
16-
, Recursive
17-
, addRecursive
18-
, addNonRecursive
19-
, unRecursive
2016
, Logger(..)
2117
, Field(..)
2218
, mapStoreDir
@@ -74,16 +70,6 @@ doSubstitute, dontSubstitute :: SubstituteFlag
7470
doSubstitute = SubstituteFlag True
7571
dontSubstitute = SubstituteFlag False
7672

77-
-- | Recursive, used by @addToStore@
78-
newtype Recursive = Recursive { unRecursive :: Bool }
79-
deriving (Eq, Ord, Show)
80-
81-
addRecursive, addNonRecursive :: Recursive
82-
-- | Add target directory recursively
83-
addRecursive = Recursive True
84-
-- | Add target directory non-recursively
85-
addNonRecursive = Recursive False
86-
8773
type MonadStore a
8874
= ExceptT
8975
String

hnix-store-remote/tests-io/NixDaemon.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ spec_protocol = Hspec.around withNixDaemon $
262262
itRights "adds file to store" $ do
263263
fp <- liftIO $ writeSystemTempFile "addition" "lal"
264264
let name = Data.Either.fromRight (error "impossible") $ makeStorePathName "tmp-addition"
265-
res <- addToStore @SHA256 name (dumpPath fp) addNonRecursive dontRepair
265+
res <- addToStore @SHA256 name (dumpPath fp) FileIngestionMethod_Flat dontRepair
266266
liftIO $ print res
267267

268268
context "with dummy" $ do

hnix-store-tests/hnix-store-tests.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ library
4141
, System.Nix.Arbitrary.Derivation
4242
, System.Nix.Arbitrary.DerivedPath
4343
, System.Nix.Arbitrary.Hash
44+
, System.Nix.Arbitrary.Store.Types
4445
, System.Nix.Arbitrary.StorePath
4546
, Test.Hspec.Nix
4647
build-depends:

hnix-store-tests/src/System/Nix/Arbitrary.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ import System.Nix.Arbitrary.ContentAddress ()
55
import System.Nix.Arbitrary.Derivation ()
66
import System.Nix.Arbitrary.DerivedPath ()
77
import System.Nix.Arbitrary.Hash ()
8+
import System.Nix.Arbitrary.Store.Types ()
89
import System.Nix.Arbitrary.StorePath ()

0 commit comments

Comments
 (0)