Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / Types.hs @ e2fa2baf

History | View | Annotate | Download (1.7 kB)

1 e4c5beaf Iustin Pop
{-| Some common types.
2 e4c5beaf Iustin Pop
3 e4c5beaf Iustin Pop
-}
4 e4c5beaf Iustin Pop
5 e2fa2baf Iustin Pop
{-
6 e2fa2baf Iustin Pop
7 e2fa2baf Iustin Pop
Copyright (C) 2009 Google Inc.
8 e2fa2baf Iustin Pop
9 e2fa2baf Iustin Pop
This program is free software; you can redistribute it and/or modify
10 e2fa2baf Iustin Pop
it under the terms of the GNU General Public License as published by
11 e2fa2baf Iustin Pop
the Free Software Foundation; either version 2 of the License, or
12 e2fa2baf Iustin Pop
(at your option) any later version.
13 e2fa2baf Iustin Pop
14 e2fa2baf Iustin Pop
This program is distributed in the hope that it will be useful, but
15 e2fa2baf Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
16 e2fa2baf Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 e2fa2baf Iustin Pop
General Public License for more details.
18 e2fa2baf Iustin Pop
19 e2fa2baf Iustin Pop
You should have received a copy of the GNU General Public License
20 e2fa2baf Iustin Pop
along with this program; if not, write to the Free Software
21 e2fa2baf Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 e2fa2baf Iustin Pop
02110-1301, USA.
23 e2fa2baf Iustin Pop
24 e2fa2baf Iustin Pop
-}
25 e2fa2baf Iustin Pop
26 e4c5beaf Iustin Pop
module Ganeti.HTools.Types
27 19f38ee8 Iustin Pop
    ( Idx
28 19f38ee8 Iustin Pop
    , Ndx
29 19f38ee8 Iustin Pop
    , NameAssoc
30 19f38ee8 Iustin Pop
    , Result(..)
31 19f38ee8 Iustin Pop
    , Element(..)
32 19f38ee8 Iustin Pop
    ) where
33 e4c5beaf Iustin Pop
34 9188aeef Iustin Pop
-- | The instance index type.
35 608efcce Iustin Pop
type Idx = Int
36 608efcce Iustin Pop
37 9188aeef Iustin Pop
-- | The node index type.
38 608efcce Iustin Pop
type Ndx = Int
39 608efcce Iustin Pop
40 9188aeef Iustin Pop
-- | The type used to hold name-to-idx mappings.
41 e4c5beaf Iustin Pop
type NameAssoc = [(String, Int)]
42 e4c5beaf Iustin Pop
43 262a08a2 Iustin Pop
{-|
44 e4c5beaf Iustin Pop
45 e4c5beaf Iustin Pop
This is similar to the JSON library Result type - *very* similar, but
46 e4c5beaf Iustin Pop
we want to use it in multiple places, so we abstract it into a
47 e4c5beaf Iustin Pop
mini-library here
48 e4c5beaf Iustin Pop
49 e4c5beaf Iustin Pop
-}
50 e4c5beaf Iustin Pop
data Result a
51 e4c5beaf Iustin Pop
    = Bad String
52 e4c5beaf Iustin Pop
    | Ok a
53 e4c5beaf Iustin Pop
    deriving (Show)
54 e4c5beaf Iustin Pop
55 e4c5beaf Iustin Pop
instance Monad Result where
56 e4c5beaf Iustin Pop
    (>>=) (Bad x) _ = Bad x
57 e4c5beaf Iustin Pop
    (>>=) (Ok x) fn = fn x
58 e4c5beaf Iustin Pop
    return = Ok
59 e4c5beaf Iustin Pop
    fail = Bad
60 497e30a1 Iustin Pop
61 9188aeef Iustin Pop
-- | A generic class for items that have updateable names and indices.
62 497e30a1 Iustin Pop
class Element a where
63 9188aeef Iustin Pop
    -- | Returns the name of the element
64 262a08a2 Iustin Pop
    nameOf  :: a -> String
65 9188aeef Iustin Pop
    -- | Returns the index of the element
66 262a08a2 Iustin Pop
    idxOf   :: a -> Int
67 9188aeef Iustin Pop
    -- | Updates the name of the element
68 497e30a1 Iustin Pop
    setName :: a -> String -> a
69 9188aeef Iustin Pop
    -- | Updates the index of the element
70 497e30a1 Iustin Pop
    setIdx  :: a -> Int -> a