aboutsummaryrefslogtreecommitdiff
path: root/Types.hs
blob: ce377dda11d961b338c5bdf36637fd3039a2bccd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{- git-annex core data types -}

module Types (
	Annex,
	AnnexState,
	makeAnnexState,
	runAnnexState,
	gitAnnex,
	gitAnnexChange,
	backendsAnnex,
	backendsAnnexChange,

	Key(..),
	Backend(..)
) where

import Control.Monad.State
import Data.String.Utils
import GitRepo

-- git-annex's runtime 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

-- Annex monad 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)

-- show a key to convert it to a string
instance Show Key where
	show (Key v) = v

-- this structure represents a key/value backend
data Backend = Backend {
	-- name of this backend
	name :: String,
	-- converts a filename to a key
	getKey :: FilePath -> Annex (Maybe Key),
	-- stores a file's contents to a key
	storeFileKey :: FilePath -> Key -> Annex Bool,
	-- retrieves a key's contents to a file
	retrieveKeyFile :: Key -> FilePath -> Annex Bool,
	-- removes a key
	removeKey :: Key -> Annex Bool
}

instance Show Backend where
	show backend = "Backend { name =\"" ++ (name backend) ++ "\" }"