core: split parseNameText from mkStorePathName

so it can be used for `OutputName` as well
This commit is contained in:
sorki 2023-12-05 07:50:41 +01:00
parent 70443c884b
commit b8294ffb74
2 changed files with 17 additions and 13 deletions

View File

@ -20,6 +20,7 @@ module System.Nix.StorePath
-- * Manipulating 'StorePathName'
, InvalidNameError(..)
, mkStorePathName
, parseNameText
-- * Reason why a path is not valid
, InvalidPathError(..)
, -- * Rendering out 'StorePath's
@ -136,7 +137,11 @@ data InvalidPathError
-- | Make @StorePathName@ from @Text@ (name part of the @StorePath@)
-- or fail with @InvalidNameError@ if it isn't valid
mkStorePathName :: Text -> Either InvalidNameError StorePathName
mkStorePathName n
mkStorePathName = fmap StorePathName . parseNameText
-- | Parse name (either @StorePathName@ or @OutputName@)
parseNameText :: Text -> Either InvalidNameError Text
parseNameText n
| n == ""
= Left EmptyName
| Data.Text.length n > 211
@ -151,7 +156,7 @@ mkStorePathName n
= Left
$ InvalidCharacters
$ Data.Text.filter (not . validStorePathNameChar) n
| otherwise = pure $ StorePathName n
| otherwise = pure n
validStorePathNameChar :: Char -> Bool
validStorePathNameChar c =

View File

@ -3,36 +3,35 @@
module StorePath where
import Test.Hspec (Spec, describe, it, shouldBe, shouldSatisfy)
import qualified Data.Either
import Test.Hspec (Spec, describe, it, shouldBe)
import qualified Data.Text
import System.Nix.StorePath (mkStorePathName)
import System.Nix.StorePath (parseNameText, InvalidNameError(..))
spec_storePath :: Spec
spec_storePath = do
describe "StorePathName" $ do
describe "parseNameText" $ do
it "parses valid name" $
mkStorePathName "name-dev.dotok"
`shouldSatisfy`
Data.Either.isRight
parseNameText "name-dev.dotok"
`shouldBe`
pure "name-dev.dotok"
it "fails on empty" $
mkStorePathName mempty
parseNameText mempty
`shouldBe`
Left EmptyName
it "fails on too long" $
mkStorePathName (Data.Text.replicate 256 "n")
parseNameText (Data.Text.replicate 256 "n")
`shouldBe`
Left (NameTooLong 256)
it "fails on leading dot" $
mkStorePathName ".ab"
parseNameText ".ab"
`shouldBe`
Left LeadingDot
it "fails on invalid characters" $
mkStorePathName "ab!cd#@"
parseNameText "ab!cd#@"
`shouldBe`
Left (InvalidCharacters "!#@")