mirror of
https://github.com/haskell-nix/hnix-store.git
synced 2024-11-30 22:42:02 +03:00
core: add System.Nix.StorePath.parsePathFromText
This commit is contained in:
parent
ca472249f9
commit
fc0133f5a1
@ -10,6 +10,7 @@
|
||||
* `BuildResult`s `timesBuild` field changes type from `Integer` to `Int` [#231](https://github.com/haskell-nix/hnix-store/pull/231)
|
||||
|
||||
* Additions:
|
||||
* `System.Nix.StorePath.parsePathFromText` [#236](https://github.com/haskell-nix/hnix-store/pull/236)
|
||||
* `Default StoreDir` instance [#231](https://github.com/haskell-nix/hnix-store/pull/231)
|
||||
* `System.Nix.StorePath.storePathHashPartToText` [#231](https://github.com/haskell-nix/hnix-store/pull/231)
|
||||
* Added `Generic` and `Show` instances for
|
||||
|
@ -26,6 +26,7 @@ module System.Nix.StorePath
|
||||
, storePathHashPartToText
|
||||
, -- * Parsing 'StorePath's
|
||||
parsePath
|
||||
, parsePathFromText
|
||||
, pathParser
|
||||
)
|
||||
where
|
||||
@ -223,15 +224,14 @@ storePathHashPartToText :: StorePathHashPart -> Text
|
||||
storePathHashPartToText =
|
||||
System.Nix.Base.encodeWith NixBase32 . unStorePathHashPart
|
||||
|
||||
-- | Parse `StorePath` from `ByteString`, checking
|
||||
-- that store directory matches `expectedRoot`.
|
||||
parsePath
|
||||
-- | Parse `StorePath` from `String`, internal
|
||||
parsePath'
|
||||
:: StoreDir
|
||||
-> ByteString
|
||||
-> String
|
||||
-> Either InvalidPathError StorePath
|
||||
parsePath expectedRoot x =
|
||||
parsePath' expectedRoot stringyPath =
|
||||
let
|
||||
(rootDir, fname) = System.FilePath.splitFileName . Data.ByteString.Char8.unpack $ x
|
||||
(rootDir, fname) = System.FilePath.splitFileName stringyPath
|
||||
(storeBasedHashPart, namePart) = Data.Text.breakOn "-" $ Data.Text.pack fname
|
||||
hashPart = Data.Bifunctor.bimap
|
||||
HashDecodingFailure
|
||||
@ -252,6 +252,22 @@ parsePath expectedRoot x =
|
||||
in
|
||||
either Left (pure $ StorePath <$> hashPart <*> name) storeDir
|
||||
|
||||
-- | Parse `StorePath` from `ByteString`, checking
|
||||
-- that store directory matches `expectedRoot`.
|
||||
parsePath
|
||||
:: StoreDir -- ^ expected @StoreDir@
|
||||
-> ByteString
|
||||
-> Either InvalidPathError StorePath
|
||||
parsePath sd = parsePath' sd . Data.ByteString.Char8.unpack
|
||||
|
||||
-- | Parse `StorePath` from `Text`, checking
|
||||
-- that store directory matches `expectedRoot`.
|
||||
parsePathFromText
|
||||
:: StoreDir -- ^ expected @StoreDir@
|
||||
-> Text
|
||||
-> Either InvalidPathError StorePath
|
||||
parsePathFromText sd = parsePath' sd . Data.Text.unpack
|
||||
|
||||
-- | Attoparsec @StorePath@ @Parser@
|
||||
pathParser :: StoreDir -> Parser StorePath
|
||||
pathParser expectedRoot = do
|
||||
|
@ -11,6 +11,11 @@ prop_storePathRoundtrip :: StoreDir -> StorePath -> Property
|
||||
prop_storePathRoundtrip storeDir x =
|
||||
parsePath storeDir (storePathToRawFilePath storeDir x) === pure x
|
||||
|
||||
-- | Test @StorePath@ roundtrips using @parsePathFromText@
|
||||
prop_storePathFromTextRoundtrip :: StoreDir -> StorePath -> Property
|
||||
prop_storePathFromTextRoundtrip storeDir x =
|
||||
parsePathFromText storeDir (storePathToText storeDir x) === pure x
|
||||
|
||||
-- | Test @StorePath@ roundtrips using @pathParser@
|
||||
prop_storePathRoundtripParser :: StoreDir -> StorePath -> Property
|
||||
prop_storePathRoundtripParser storeDir x =
|
||||
|
Loading…
Reference in New Issue
Block a user