git-annex/Types.hs

80 lines
1.7 KiB
Haskell
Raw Normal View History

2010-10-12 19:52:18 +00:00
{- git-annex core data types -}
module Types (
2010-10-14 01:28:47 +00:00
Annex(..),
makeAnnexState,
runAnnexState,
gitAnnex,
gitAnnexChange,
backendsAnnex,
backendsAnnexChange,
AnnexState(..),
2010-10-12 20:06:10 +00:00
Key(..),
Backend(..)
2010-10-12 19:52:18 +00:00
) where
2010-10-14 01:28:47 +00:00
import Control.Monad.State
2010-10-13 00:04:36 +00:00
import Data.String.Utils
2010-10-12 19:52:18 +00:00
import GitRepo
-- git-annex's runtime state
2010-10-14 01:28:47 +00:00
data AnnexState = AnnexState {
2010-10-12 19:52:18 +00:00
repo :: GitRepo,
backends :: [Backend]
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
-- constructor
makeAnnexState :: GitRepo -> AnnexState
makeAnnexState g = AnnexState { repo = g, backends = [] }
-- performs an action in the Annex monad
runAnnexState state action = runStateT (action) state
-- state accessors
gitAnnex :: Annex GitRepo
gitAnnex = do
state <- get
return (repo state)
gitAnnexChange :: GitRepo -> Annex ()
gitAnnexChange r = do
state <- get
put state { repo = r }
return ()
backendsAnnex :: Annex [Backend]
backendsAnnex = do
state <- get
return (backends state)
backendsAnnexChange :: [Backend] -> Annex ()
backendsAnnexChange b = do
state <- get
put state { backends = b }
return ()
2010-10-12 20:06:10 +00:00
-- annexed filenames are mapped into keys
data Key = Key String deriving (Eq)
-- show a key to convert it to a string
instance Show Key where
show (Key v) = v
2010-10-12 20:06:10 +00:00
-- this structure represents a key/value backend
data Backend = Backend {
-- name of this backend
name :: String,
-- converts a filename to a key
2010-10-14 01:28:47 +00:00
getKey :: FilePath -> Annex (Maybe Key),
2010-10-12 20:06:10 +00:00
-- stores a file's contents to a key
2010-10-14 01:28:47 +00:00
storeFileKey :: FilePath -> Key -> Annex Bool,
2010-10-12 20:06:10 +00:00
-- retrieves a key's contents to a file
2010-10-14 01:28:47 +00:00
retrieveKeyFile :: Key -> FilePath -> Annex Bool,
2010-10-12 20:06:10 +00:00
-- removes a key
2010-10-14 01:28:47 +00:00
removeKey :: Key -> Annex Bool
2010-10-12 19:52:18 +00:00
}
2010-10-12 20:06:10 +00:00
instance Show Backend where
show backend = "Backend { name =\"" ++ (name backend) ++ "\" }"