aboutsummaryrefslogtreecommitdiff
path: root/Types.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2010-10-13 21:28:47 -0400
committerGravatar Joey Hess <joey@kitenet.net>2010-10-13 21:28:47 -0400
commitb1607485168e851f69fe3a5b74d73f3c36edf886 (patch)
tree496133383a3aa77ecc373c383c6655e50d71f9c9 /Types.hs
parente5c1db355f5fa31af14ed8474aee89872b934f1a (diff)
use a state monad
enormous reworking
Diffstat (limited to 'Types.hs')
-rw-r--r--Types.hs51
1 files changed, 45 insertions, 6 deletions
diff --git a/Types.hs b/Types.hs
index 9b0bb00fd..15c2ec89f 100644
--- a/Types.hs
+++ b/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