Revision 0c37d1e4
b/Makefile.am | ||
---|---|---|
387 | 387 |
htools/Ganeti/HTools/Program/Hbal.hs \ |
388 | 388 |
htools/Ganeti/HTools/Program/Hscan.hs \ |
389 | 389 |
htools/Ganeti/HTools/Program/Hspace.hs \ |
390 |
htools/Ganeti/BasicTypes.hs \ |
|
390 | 391 |
htools/Ganeti/Jobs.hs \ |
391 | 392 |
htools/Ganeti/Luxi.hs \ |
392 | 393 |
htools/Ganeti/OpCodes.hs \ |
b/htools/Ganeti/BasicTypes.hs | ||
---|---|---|
1 |
{- |
|
2 |
|
|
3 |
Copyright (C) 2009, 2010, 2011 Google Inc. |
|
4 |
|
|
5 |
This program is free software; you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU General Public License as published by |
|
7 |
the Free Software Foundation; either version 2 of the License, or |
|
8 |
(at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, but |
|
11 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU General Public License |
|
16 |
along with this program; if not, write to the Free Software |
|
17 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 |
02110-1301, USA. |
|
19 |
|
|
20 |
-} |
|
21 |
|
|
22 |
module Ganeti.BasicTypes |
|
23 |
( Result(..) |
|
24 |
, isOk |
|
25 |
, isBad |
|
26 |
, eitherToResult |
|
27 |
) where |
|
28 |
|
|
29 |
import Control.Monad |
|
30 |
|
|
31 |
-- | This is similar to the JSON library Result type - /very/ similar, |
|
32 |
-- but we want to use it in multiple places, so we abstract it into a |
|
33 |
-- mini-library here. |
|
34 |
-- |
|
35 |
-- The failure value for this monad is simply a string. |
|
36 |
data Result a |
|
37 |
= Bad String |
|
38 |
| Ok a |
|
39 |
deriving (Show, Read, Eq) |
|
40 |
|
|
41 |
instance Monad Result where |
|
42 |
(>>=) (Bad x) _ = Bad x |
|
43 |
(>>=) (Ok x) fn = fn x |
|
44 |
return = Ok |
|
45 |
fail = Bad |
|
46 |
|
|
47 |
instance MonadPlus Result where |
|
48 |
mzero = Bad "zero Result when used as MonadPlus" |
|
49 |
-- for mplus, when we 'add' two Bad values, we concatenate their |
|
50 |
-- error descriptions |
|
51 |
(Bad x) `mplus` (Bad y) = Bad (x ++ "; " ++ y) |
|
52 |
(Bad _) `mplus` x = x |
|
53 |
x@(Ok _) `mplus` _ = x |
|
54 |
|
|
55 |
-- | Simple checker for whether a 'Result' is OK. |
|
56 |
isOk :: Result a -> Bool |
|
57 |
isOk (Ok _) = True |
|
58 |
isOk _ = False |
|
59 |
|
|
60 |
-- | Simple checker for whether a 'Result' is a failure. |
|
61 |
isBad :: Result a -> Bool |
|
62 |
isBad = not . isOk |
|
63 |
|
|
64 |
-- | Converter from Either String to 'Result'. |
|
65 |
eitherToResult :: Either String a -> Result a |
|
66 |
eitherToResult (Left s) = Bad s |
|
67 |
eitherToResult (Right v) = Ok v |
b/htools/Ganeti/HTools/Types.hs | ||
---|---|---|
72 | 72 |
, EvacMode(..) |
73 | 73 |
) where |
74 | 74 |
|
75 |
import Control.Monad |
|
76 | 75 |
import qualified Data.Map as M |
77 | 76 |
import qualified Text.JSON as JSON |
78 | 77 |
|
79 | 78 |
import qualified Ganeti.Constants as C |
80 | 79 |
import qualified Ganeti.THH as THH |
80 |
import Ganeti.BasicTypes |
|
81 | 81 |
|
82 | 82 |
-- | The instance index type. |
83 | 83 |
type Idx = Int |
... | ... | |
226 | 226 |
unitCpu :: Int |
227 | 227 |
unitCpu = 1 |
228 | 228 |
|
229 |
-- | This is similar to the JSON library Result type - /very/ similar, |
|
230 |
-- but we want to use it in multiple places, so we abstract it into a |
|
231 |
-- mini-library here. |
|
232 |
-- |
|
233 |
-- The failure value for this monad is simply a string. |
|
234 |
data Result a |
|
235 |
= Bad String |
|
236 |
| Ok a |
|
237 |
deriving (Show, Read, Eq) |
|
238 |
|
|
239 |
instance Monad Result where |
|
240 |
(>>=) (Bad x) _ = Bad x |
|
241 |
(>>=) (Ok x) fn = fn x |
|
242 |
return = Ok |
|
243 |
fail = Bad |
|
244 |
|
|
245 |
instance MonadPlus Result where |
|
246 |
mzero = Bad "zero Result when used as MonadPlus" |
|
247 |
-- for mplus, when we 'add' two Bad values, we concatenate their |
|
248 |
-- error descriptions |
|
249 |
(Bad x) `mplus` (Bad y) = Bad (x ++ "; " ++ y) |
|
250 |
(Bad _) `mplus` x = x |
|
251 |
x@(Ok _) `mplus` _ = x |
|
252 |
|
|
253 |
-- | Simple checker for whether a 'Result' is OK. |
|
254 |
isOk :: Result a -> Bool |
|
255 |
isOk (Ok _) = True |
|
256 |
isOk _ = False |
|
257 |
|
|
258 |
-- | Simple checker for whether a 'Result' is a failure. |
|
259 |
isBad :: Result a -> Bool |
|
260 |
isBad = not . isOk |
|
261 |
|
|
262 |
-- | Converter from Either String to 'Result'. |
|
263 |
eitherToResult :: Either String a -> Result a |
|
264 |
eitherToResult (Left s) = Bad s |
|
265 |
eitherToResult (Right v) = Ok v |
|
266 |
|
|
267 | 229 |
-- | Reason for an operation's falure. |
268 | 230 |
data FailMode = FailMem -- ^ Failed due to not enough RAM |
269 | 231 |
| FailDisk -- ^ Failed due to not enough disk |
Also available in: Unified diff