Add store path type.

This commit is contained in:
Shea Levy 2018-04-26 21:33:06 -07:00
parent d0fd35ba81
commit 8b7a367021
No known key found for this signature in database
GPG Key ID: 5C0BD6957D86FE27
2 changed files with 44 additions and 5 deletions

View File

@ -17,12 +17,10 @@ extra-source-files: ChangeLog.md, README.md
cabal-version: >=1.10
library
exposed-modules: Crypto.Hash.Truncated
exposed-modules: Crypto.Hash.Truncated, System.Nix.Store
build-depends: base >=4.10 && <4.11,
cryptonite,
memory,
-- Drop foundation when we can drop cryptonite <0.25
foundation,
basement
cryptonite, memory, foundation, basement,
text, regex-base, regex-tdfa-text
hs-source-dirs: src
default-language: Haskell2010

View File

@ -0,0 +1,41 @@
{-|
Description : Types and effects for interacting with the Nix store.
Maintainer : Shea Levy <shea@shealevy.com>
-}
{-# LANGUAGE DataKinds #-}
module System.Nix.Store
( PathName, pathNameContents, pathName
, PathHashAlgo, Path(..)
) where
import Crypto.Hash (Digest)
import Crypto.Hash.Truncated (Truncated)
import Crypto.Hash.Algorithms (SHA256)
import Data.Text (Text)
import Text.Regex.Base.RegexLike (makeRegex, matchTest)
import Text.Regex.TDFA.Text (Regex)
-- | The name portion of a Nix path.
--
-- Must be composed of a-z, A-Z, 0-9, +, -, ., _, ?, and =, can't
-- start with a ., and must have at least one character.
newtype PathName = PathName
{ pathNameContents :: Text -- ^ The contents of the path name
}
-- | A regular expression for matching a valid 'PathName'
nameRegex :: Regex
nameRegex =
makeRegex "[a-zA-Z0-9\\+\\-\\_\\?\\=][a-zA-Z0-9\\+\\-\\.\\_\\?\\=]*"
-- | Construct a 'PathName', assuming the provided contents are valid.
pathName :: Text -> Maybe PathName
pathName n = case matchTest nameRegex n of
True -> Just $ PathName n
False -> Nothing
-- | The hash algorithm used for store path hashes.
type PathHashAlgo = Truncated SHA256 20
-- | A path in a store.
data Path = Path !(Digest PathHashAlgo) !PathName