Skip to content

Commit 7606e42

Browse files
committed
switch pathParser to Text.Lazy, add storePathToText, refactor imports a bit
1 parent 00debcd commit 7606e42

File tree

5 files changed

+37
-30
lines changed

5 files changed

+37
-30
lines changed

hnix-store-core/src/System/Nix/Internal/StorePath.hs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ import Data.Hashable (Hashable(..))
3333
import Data.HashSet (HashSet)
3434
import Data.Proxy (Proxy(..))
3535

36-
import Data.Attoparsec.ByteString.Char8 (Parser, (<?>))
37-
import qualified Data.Attoparsec.ByteString.Char8 as P
38-
import System.FilePath (splitFileName)
36+
import Data.Attoparsec.Text.Lazy (Parser, (<?>))
37+
38+
import qualified Data.Attoparsec.Text.Lazy
39+
import qualified System.FilePath
3940

4041
-- | A path in a Nix store.
4142
--
@@ -162,6 +163,12 @@ storePathToFilePath
162163
-> FilePath
163164
storePathToFilePath = BC.unpack . storePathToRawFilePath
164165

166+
-- | Render a 'StorePath' as a 'Text'.
167+
storePathToText
168+
:: StorePath
169+
-> Text
170+
storePathToText = T.pack . BC.unpack . storePathToRawFilePath
171+
165172
-- | Build `narinfo` suffix from `StorePath` which
166173
-- can be used to query binary caches.
167174
storePathToNarInfo
@@ -180,7 +187,7 @@ parsePath
180187
-> Either String StorePath
181188
parsePath expectedRoot x =
182189
let
183-
(rootDir, fname) = splitFileName . BC.unpack $ x
190+
(rootDir, fname) = System.FilePath.splitFileName . BC.unpack $ x
184191
(digestPart, namePart) = T.breakOn "-" $ T.pack fname
185192
digest = decodeBase32 digestPart
186193
name = makeStorePathName . T.drop 1 $ namePart
@@ -195,28 +202,26 @@ parsePath expectedRoot x =
195202

196203
pathParser :: FilePath -> Parser StorePath
197204
pathParser expectedRoot = do
198-
P.string (BC.pack expectedRoot)
205+
Data.Attoparsec.Text.Lazy.string (T.pack expectedRoot)
199206
<?> "Store root mismatch" -- e.g. /nix/store
200207

201-
P.char '/'
208+
Data.Attoparsec.Text.Lazy.char '/'
202209
<?> "Expecting path separator"
203210

204-
digest <- decodeBase32 . T.pack . BC.unpack
205-
<$> P.takeWhile1 (\c -> c `elem` digits32)
211+
digest <- decodeBase32
212+
<$> Data.Attoparsec.Text.Lazy.takeWhile1 (\c -> c `elem` digits32)
206213
<?> "Invalid Base32 part"
207214

208-
P.char '-'
215+
Data.Attoparsec.Text.Lazy.char '-'
209216
<?> "Expecting dash (path name separator)"
210217

211-
c0 <- P.satisfy (\c -> c /= '.' && validStorePathNameChar c)
218+
c0 <- Data.Attoparsec.Text.Lazy.satisfy (\c -> c /= '.' && validStorePathNameChar c)
212219
<?> "Leading path name character is a dot or invalid character"
213220

214-
rest <- P.takeWhile validStorePathNameChar
221+
rest <- Data.Attoparsec.Text.Lazy.takeWhile validStorePathNameChar
215222
<?> "Path name contains invalid character"
216223

217-
let name = makeStorePathName
218-
$ T.pack . BC.unpack
219-
$ BC.cons c0 rest
224+
let name = makeStorePathName $ T.cons c0 rest
220225

221226
either fail return
222227
$ StorePath <$> digest <*> name <*> pure expectedRoot

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ module System.Nix.StorePath
1414
, unStorePathName
1515
, validStorePathName
1616
, -- * Rendering out 'StorePath's
17-
storePathToRawFilePath
18-
, storePathToFilePath
17+
storePathToFilePath
18+
, storePathToRawFilePath
19+
, storePathToText
1920
, storePathToNarInfo
20-
, parsePath
21+
, -- * Parsing 'StorePath's
22+
parsePath
2123
, pathParser
2224
) where
2325

hnix-store-core/tests/StorePath.hs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55

66
module StorePath where
77

8-
import qualified Data.ByteString.Char8 as BSC
9-
import qualified Data.Attoparsec.ByteString.Char8 as P
8+
import qualified Data.Attoparsec.Text.Lazy
109

1110
import Test.Tasty.QuickCheck
1211

13-
import System.Nix.Internal.StorePath
12+
import System.Nix.StorePath
1413
import Arbitrary
1514

1615
-- | Test that Nix(OS) like paths roundtrip
@@ -22,9 +21,9 @@ prop_storePathRoundtrip' x =
2221
(parsePath (storePathRoot x) $ storePathToRawFilePath x) === Right x
2322

2423
prop_storePathRoundtripParser (_ :: NixLike) = \(NixLike x) ->
25-
(P.parseOnly (pathParser (storePathRoot x))
26-
$ storePathToRawFilePath x) === Right x
24+
(Data.Attoparsec.Text.Lazy.parseOnly (pathParser (storePathRoot x))
25+
$ storePathToText x) === Right x
2726

2827
prop_storePathRoundtripParser' x =
29-
(P.parseOnly (pathParser (storePathRoot x))
30-
$ storePathToRawFilePath x) === Right x
28+
(Data.Attoparsec.Text.Lazy.parseOnly (pathParser (storePathRoot x))
29+
$ storePathToText x) === Right x

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ import qualified Data.Set
4747
import Data.Proxy (Proxy)
4848
import Data.Text (Text)
4949

50-
import qualified System.Nix.Build as Build
5150

51+
import System.Nix.Build (BuildMode, BuildResult)
5252
import System.Nix.Hash (Digest, ValidAlgo)
5353
import System.Nix.StorePath
5454
import System.Nix.Hash
@@ -60,7 +60,7 @@ import System.Nix.Store.Remote.Types
6060
import System.Nix.Store.Remote.Protocol
6161
import System.Nix.Store.Remote.Util
6262

63-
import qualified Data.Text.Encoding -- (encodeUtf8)
63+
import qualified Data.Text.Encoding
6464

6565
type RepairFlag = Bool
6666
type CheckFlag = Bool
@@ -150,7 +150,7 @@ addTempRoot :: StorePath -> MonadStore ()
150150
addTempRoot pn = do
151151
void $ simpleOpArgs AddTempRoot $ putPath pn
152152

153-
buildPaths :: StorePathSet -> Build.BuildMode -> MonadStore ()
153+
buildPaths :: StorePathSet -> BuildMode -> MonadStore ()
154154
buildPaths ps bm = do
155155
void $ simpleOpArgs BuildPaths $ do
156156
putPaths ps

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import Data.Time.Clock.POSIX
1616
import Data.ByteString (ByteString)
1717
import qualified Data.ByteString.Char8 as BSC
1818
import qualified Data.ByteString.Lazy as BSL
19-
import qualified Data.HashSet as HashSet
2019

2120
import Network.Socket.ByteString (recv, sendAll)
2221

@@ -25,6 +24,8 @@ import System.Nix.StorePath
2524
import System.Nix.Store.Remote.Binary
2625
import System.Nix.Store.Remote.Types
2726

27+
import qualified Data.HashSet
28+
import qualified Data.Map
2829

2930
genericIncremental :: (MonadIO m) => m (Maybe ByteString) -> Get a -> m a
3031
genericIncremental getsome parser = go decoder
@@ -105,13 +106,13 @@ getPath :: FilePath -> Get (Either String StorePath)
105106
getPath sd = parsePath sd <$> getByteStringLen
106107

107108
getPaths :: FilePath -> Get StorePathSet
108-
getPaths sd = HashSet.fromList . rights . map (parsePath sd) <$> getByteStrings
109+
getPaths sd = Data.HashSet.fromList . rights . map (parsePath sd) <$> getByteStrings
109110

110111
putPath :: StorePath -> Put
111112
putPath = putByteStringLen . BSL.fromStrict . storePathToRawFilePath
112113

113114
putPaths :: StorePathSet -> Put
114-
putPaths = putByteStrings . HashSet.toList . HashSet.map (BSL.fromStrict . storePathToRawFilePath)
115+
putPaths = putByteStrings . Data.HashSet.toList . Data.HashSet.map (BSL.fromStrict . storePathToRawFilePath)
115116

116117
putBool :: Bool -> Put
117118
putBool True = putInt (1 :: Int)

0 commit comments

Comments
 (0)