Types.hs 1.7 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1
2
3
4
{-| Some common types.

-}

Iustin Pop's avatar
Iustin Pop committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{-

Copyright (C) 2009 Google Inc.

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

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

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

-}

Iustin Pop's avatar
Iustin Pop committed
26
module Ganeti.HTools.Types
27
28
29
30
31
32
    ( Idx
    , Ndx
    , NameAssoc
    , Result(..)
    , Element(..)
    ) where
Iustin Pop's avatar
Iustin Pop committed
33

Iustin Pop's avatar
Iustin Pop committed
34
-- | The instance index type.
35
36
type Idx = Int

Iustin Pop's avatar
Iustin Pop committed
37
-- | The node index type.
38
39
type Ndx = Int

Iustin Pop's avatar
Iustin Pop committed
40
-- | The type used to hold name-to-idx mappings.
Iustin Pop's avatar
Iustin Pop committed
41
42
type NameAssoc = [(String, Int)]

43
{-|
Iustin Pop's avatar
Iustin Pop committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

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

-}
data Result a
    = Bad String
    | Ok a
    deriving (Show)

instance Monad Result where
    (>>=) (Bad x) _ = Bad x
    (>>=) (Ok x) fn = fn x
    return = Ok
    fail = Bad
60

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