first pass at using new keys

It compiles. It sorta works. Several subcommands are FIXME marked and
broken, because things that used to accept separate --backend and --key
params need to be changed to accept just a --key that encodes all the key
info, now that there is metadata in keys.
This commit is contained in:
Joey Hess 2011-03-15 21:34:13 -04:00
parent 675ee89749
commit 9d49fe2c17
20 changed files with 116 additions and 123 deletions

45
Key.hs
View file

@ -5,20 +5,35 @@
- Licensed under the GNU GPL version 3 or higher.
-}
module Key where
module Key (
Key(..),
stubKey,
readKey,
prop_idempotent_key_read_show
) where
import Test.QuickCheck
import Utility
import System.Posix.Types
{- A Key has a unique name, is associated with a backend,
- and may contain other metadata. -}
{- A Key has a unique name, is associated with a key/value backend,
- and may contain other optional metadata. -}
data Key = Key {
keyName :: String,
keyBackend :: String,
keySize :: Maybe Int,
keyMtime :: Maybe Int
keyBackendName :: String,
keySize :: Maybe Integer,
keyMtime :: Maybe EpochTime
} deriving (Eq, Ord)
stubKey :: Key
stubKey = Key {
keyName = "",
keyBackendName = "",
keySize = Nothing,
keyMtime = Nothing
}
fieldSep :: Char
fieldSep = ','
@ -26,7 +41,7 @@ fieldSep = ','
- The name field is always shown last, and is the only field
- allowed to contain the fieldSep. -}
instance Show Key where
show Key { keyBackend = b, keySize = s, keyMtime = m, keyName = n } =
show Key { keyBackendName = b, keySize = s, keyMtime = m, keyName = n } =
('b' : b) +++ ('s' ?: s) +++ ('m' ?: m) +++ ('n' : n)
where
"" +++ y = y
@ -36,16 +51,9 @@ instance Show Key where
_ ?: _ = ""
readKey :: String -> Maybe Key
readKey s = if key == stub then Nothing else key
readKey s = if key == Just stubKey then Nothing else key
where
key = findfields s stub
stub = Just Key {
keyName = "",
keyBackend = "",
keySize = Nothing,
keyMtime = Nothing
}
key = findfields s $ Just stubKey
findfields ('n':v) (Just k) = Just $ k { keyName = v }
findfields (c:v) (Just k) =
@ -54,7 +62,7 @@ readKey s = if key == stub then Nothing else key
_ -> Nothing
findfields _ v = v
addfield k 'b' v = Just k { keyBackend = v }
addfield k 'b' v = Just k { keyBackendName = v }
addfield k 's' v = Just k { keySize = readMaybe v }
addfield k 'm' v = Just k { keyMtime = readMaybe v }
addfield _ _ _ = Nothing
@ -65,8 +73,7 @@ instance Arbitrary Key where
n <- arbitrary
b <- elements ['A'..'Z']
s <- arbitrary
m <- arbitrary
return $ Key { keyName = n, keyBackend = [b] , keySize = s, keyMtime = m }
return $ Key { keyName = n, keyBackendName = [b] , keySize = s }
prop_idempotent_key_read_show :: Key -> Bool
prop_idempotent_key_read_show k = Just k == (readKey $ show k)