Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / Types.hs @ 0a8dd21d

History | View | Annotate | Download (1.7 kB)

1
{-| Some common types.
2

    
3
-}
4

    
5
{-
6

    
7
Copyright (C) 2009 Google Inc.
8

    
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
13

    
14
This program is distributed in the hope that it will be useful, but
15
WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
General Public License for more details.
18

    
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
02110-1301, USA.
23

    
24
-}
25

    
26
module Ganeti.HTools.Types
27
    ( Idx
28
    , Ndx
29
    , NameAssoc
30
    , Result(..)
31
    , Element(..)
32
    ) where
33

    
34
-- | The instance index type.
35
type Idx = Int
36

    
37
-- | The node index type.
38
type Ndx = Int
39

    
40
-- | The type used to hold name-to-idx mappings.
41
type NameAssoc = [(String, Int)]
42

    
43
{-|
44

    
45
This is similar to the JSON library Result type - *very* similar, but
46
we want to use it in multiple places, so we abstract it into a
47
mini-library here
48

    
49
-}
50
data Result a
51
    = Bad String
52
    | Ok a
53
    deriving (Show)
54

    
55
instance Monad Result where
56
    (>>=) (Bad x) _ = Bad x
57
    (>>=) (Ok x) fn = fn x
58
    return = Ok
59
    fail = Bad
60

    
61
-- | A generic class for items that have updateable names and indices.
62
class Element a where
63
    -- | Returns the name of the element
64
    nameOf  :: a -> String
65
    -- | Returns the index of the element
66
    idxOf   :: a -> Int
67
    -- | Updates the name of the element
68
    setName :: a -> String -> a
69
    -- | Updates the index of the element
70
    setIdx  :: a -> Int -> a