git-annex/TypeInternals.hs

106 lines
2.8 KiB
Haskell
Raw Normal View History

2010-10-18 06:06:27 +00:00
{- git-annex internal data types
2010-10-14 06:52:17 +00:00
-
2010-10-18 06:06:27 +00:00
- Most things should not need this, using Types and/or Annex instead.
2010-10-27 20:53:54 +00:00
-
- Copyright 2010 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
2010-10-14 06:52:17 +00:00
-}
2010-10-12 19:52:18 +00:00
2010-10-18 06:06:27 +00:00
module TypeInternals where
2010-10-12 19:52:18 +00:00
2010-10-14 23:36:11 +00:00
import Control.Monad.State (StateT)
2010-10-13 00:04:36 +00:00
import Data.String.Utils
import qualified Data.Map as M
import Test.QuickCheck
2010-10-16 20:20:49 +00:00
2010-10-14 06:36:41 +00:00
import qualified GitRepo as Git
import qualified GitQueue
2010-10-12 19:52:18 +00:00
-- command-line flags
type FlagName = String
data Flag =
FlagBool Bool |
FlagString String
2010-10-19 17:08:05 +00:00
deriving (Eq, Read, Show)
2010-10-14 06:52:17 +00:00
-- git-annex's runtime state type doesn't really belong here,
-- but it uses Backend, so has to be here to avoid a depends loop.
2010-10-14 01:28:47 +00:00
data AnnexState = AnnexState {
2010-10-14 06:36:41 +00:00
repo :: Git.Repo,
backends :: [Backend Annex],
supportedBackends :: [Backend Annex],
flags :: M.Map FlagName Flag,
repoqueue :: GitQueue.Queue,
quiet :: Bool
2010-10-12 20:06:10 +00:00
} deriving (Show)
2010-10-14 01:28:47 +00:00
-- git-annex's monad
type Annex = StateT AnnexState IO
2010-10-14 23:36:11 +00:00
-- annexed filenames are mapped through a backend into keys
type KeyName = String
2010-10-14 23:36:11 +00:00
type BackendName = String
2010-11-07 21:26:21 +00:00
data Key = Key (BackendName, KeyName) deriving (Eq, Ord)
-- constructs a key in a backend
genKey :: Backend a -> KeyName -> Key
genKey b f = Key (name b,f)
2010-10-14 23:36:11 +00:00
-- show a key to convert it to a string; the string includes the
-- name of the backend to avoid collisions between key strings
instance Show Key where
2010-10-14 23:36:11 +00:00
show (Key (b, k)) = b ++ ":" ++ k
instance Read Key where
readsPrec _ s = [(Key (b,k), "")]
2010-10-14 23:36:11 +00:00
where
l = split ":" s
b = if null l then "" else head l
2010-10-14 23:36:11 +00:00
k = join ":" $ drop 1 l
2010-10-12 20:06:10 +00:00
-- for quickcheck
instance Arbitrary Key where
arbitrary = do
backendname <- arbitrary
keyname <- arbitrary
return $ Key (backendname, keyname)
prop_idempotent_key_read_show :: Key -> Bool
prop_idempotent_key_read_show k
-- backend names will never contain colons
| elem ':' (backendName k) = True
| otherwise = k == (read $ show k)
2010-10-15 00:05:04 +00:00
backendName :: Key -> BackendName
2010-10-31 20:00:32 +00:00
backendName (Key (b,_)) = b
keyName :: Key -> KeyName
2010-10-31 20:00:32 +00:00
keyName (Key (_,k)) = k
2010-10-15 00:05:04 +00:00
2010-10-16 20:15:31 +00:00
-- this structure represents a key-value backend
data Backend a = Backend {
2010-10-12 20:06:10 +00:00
-- name of this backend
name :: String,
-- converts a filename to a key
getKey :: FilePath -> a (Maybe Key),
2010-10-12 20:06:10 +00:00
-- stores a file's contents to a key
storeFileKey :: FilePath -> Key -> a Bool,
2010-10-12 20:06:10 +00:00
-- retrieves a key's contents to a file
retrieveKeyFile :: Key -> FilePath -> a Bool,
-- removes a key, optionally checking that enough copies are stored
-- elsewhere
removeKey :: Key -> Maybe Int -> a Bool,
2010-10-14 19:31:44 +00:00
-- checks if a backend is storing the content of a key
hasKey :: Key -> a Bool,
-- called during fsck to check a key
-- (second parameter may be the number of copies that there should
-- be of the key)
fsckKey :: Key -> Maybe Int -> a Bool
2010-10-12 19:52:18 +00:00
}
2010-10-12 20:06:10 +00:00
instance Show (Backend a) where
show backend = "Backend { name =\"" ++ name backend ++ "\" }"
instance Eq (Backend a) where
a == b = name a == name b