use a state monad
enormous reworking
This commit is contained in:
parent
e5c1db355f
commit
b160748516
11 changed files with 251 additions and 157 deletions
51
Types.hs
51
Types.hs
|
@ -1,20 +1,59 @@
|
|||
{- git-annex core data types -}
|
||||
|
||||
module Types (
|
||||
State(..),
|
||||
Annex(..),
|
||||
makeAnnexState,
|
||||
runAnnexState,
|
||||
gitAnnex,
|
||||
gitAnnexChange,
|
||||
backendsAnnex,
|
||||
backendsAnnexChange,
|
||||
|
||||
AnnexState(..),
|
||||
Key(..),
|
||||
Backend(..)
|
||||
) where
|
||||
|
||||
import Control.Monad.State
|
||||
import Data.String.Utils
|
||||
import GitRepo
|
||||
|
||||
-- git-annex's runtime state
|
||||
data State = State {
|
||||
data AnnexState = AnnexState {
|
||||
repo :: GitRepo,
|
||||
backends :: [Backend]
|
||||
} deriving (Show)
|
||||
|
||||
-- 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 ()
|
||||
|
||||
-- annexed filenames are mapped into keys
|
||||
data Key = Key String deriving (Eq)
|
||||
|
||||
|
@ -27,13 +66,13 @@ data Backend = Backend {
|
|||
-- name of this backend
|
||||
name :: String,
|
||||
-- converts a filename to a key
|
||||
getKey :: State -> FilePath -> IO (Maybe Key),
|
||||
getKey :: FilePath -> Annex (Maybe Key),
|
||||
-- stores a file's contents to a key
|
||||
storeFileKey :: State -> FilePath -> Key -> IO Bool,
|
||||
storeFileKey :: FilePath -> Key -> Annex Bool,
|
||||
-- retrieves a key's contents to a file
|
||||
retrieveKeyFile :: State -> Key -> FilePath -> IO Bool,
|
||||
retrieveKeyFile :: Key -> FilePath -> Annex Bool,
|
||||
-- removes a key
|
||||
removeKey :: State -> Key -> IO Bool
|
||||
removeKey :: Key -> Annex Bool
|
||||
}
|
||||
|
||||
instance Show Backend where
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue