git-annex/Types/Key.hs

91 lines
2.3 KiB
Haskell
Raw Normal View History

2011-03-15 21:47:29 +00:00
{- git-annex Key data type
-
- Most things should not need this, using Types instead
2011-03-15 21:47:29 +00:00
-
- Copyright 2011 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Types.Key (
Key(..),
2013-07-04 06:36:02 +00:00
AssociatedFile,
stubKey,
key2file,
file2key,
prop_idempotent_key_encode
) where
2011-03-15 21:47:29 +00:00
import System.Posix.Types
2011-03-15 21:47:29 +00:00
import Common
2013-02-28 01:48:46 +00:00
import Utility.QuickCheck
2013-07-04 06:36:02 +00:00
{- A Key has a unique name, which is derived from a particular backend,
- and may contain other optional metadata. -}
2013-07-04 06:45:46 +00:00
data Key = Key
{ keyName :: String
, keyBackendName :: String
, keySize :: Maybe Integer
, keyMtime :: Maybe EpochTime
} deriving (Eq, Ord, Read, Show)
2011-03-15 21:47:29 +00:00
2013-07-04 06:36:02 +00:00
{- A filename may be associated with a Key. -}
type AssociatedFile = Maybe FilePath
stubKey :: Key
2013-07-04 06:45:46 +00:00
stubKey = Key
{ keyName = ""
, keyBackendName = ""
, keySize = Nothing
, keyMtime = Nothing
}
2011-03-15 21:47:29 +00:00
fieldSep :: Char
2011-03-16 01:54:38 +00:00
fieldSep = '-'
2011-03-15 21:47:29 +00:00
{- Converts a key to a string that is suitable for use as a filename.
2011-03-16 01:54:38 +00:00
- The name field is always shown last, separated by doubled fieldSeps,
- and is the only field allowed to contain the fieldSep. -}
key2file :: Key -> FilePath
key2file Key { keyBackendName = b, keySize = s, keyMtime = m, keyName = n } =
b +++ ('s' ?: s) +++ ('m' ?: m) +++ (fieldSep : n)
2012-11-11 04:51:07 +00:00
where
"" +++ y = y
x +++ "" = x
x +++ y = x ++ fieldSep:y
c ?: (Just v) = c : show v
_ ?: _ = ""
2011-03-15 23:11:21 +00:00
file2key :: FilePath -> Maybe Key
file2key s = if key == Just stubKey then Nothing else key
2012-11-11 04:51:07 +00:00
where
key = startbackend stubKey s
2011-03-15 21:47:29 +00:00
2012-11-11 04:51:07 +00:00
startbackend k v = sepfield k v addbackend
2011-03-15 23:11:21 +00:00
2012-11-11 04:51:07 +00:00
sepfield k v a = case span (/= fieldSep) v of
(v', _:r) -> findfields r $ a k v'
_ -> Nothing
2011-03-16 01:54:38 +00:00
2012-11-11 04:51:07 +00:00
findfields (c:v) (Just k)
| c == fieldSep = Just $ k { keyName = v }
| otherwise = sepfield k v $ addfield c
findfields _ v = v
2011-03-16 01:54:38 +00:00
2012-11-11 04:51:07 +00:00
addbackend k v = Just k { keyBackendName = v }
addfield 's' k v = Just k { keySize = readish v }
addfield 'm' k v = Just k { keyMtime = readish v }
addfield _ _ _ = Nothing
2011-03-15 21:47:29 +00:00
instance Arbitrary Key where
arbitrary = Key
2013-09-11 17:02:10 +00:00
<$> (listOf1 $ elements $ ['A'..'Z'] ++ ['a'..'z'] ++ ['0'..'9'] ++ "-_\r\n \t")
<*> (listOf1 $ elements ['A'..'Z']) -- BACKEND
<*> ((abs <$>) <$> arbitrary) -- size cannot be negative
<*> arbitrary
prop_idempotent_key_encode :: Key -> Bool
prop_idempotent_key_encode k = Just k == (file2key . key2file) k