root / htools / Ganeti / BasicTypes.hs @ d953a965
History | View | Annotate | Download (2 kB)
1 | 0c37d1e4 | Iustin Pop | {- |
---|---|---|---|
2 | 0c37d1e4 | Iustin Pop | |
3 | 0c37d1e4 | Iustin Pop | Copyright (C) 2009, 2010, 2011 Google Inc. |
4 | 0c37d1e4 | Iustin Pop | |
5 | 0c37d1e4 | Iustin Pop | This program is free software; you can redistribute it and/or modify |
6 | 0c37d1e4 | Iustin Pop | it under the terms of the GNU General Public License as published by |
7 | 0c37d1e4 | Iustin Pop | the Free Software Foundation; either version 2 of the License, or |
8 | 0c37d1e4 | Iustin Pop | (at your option) any later version. |
9 | 0c37d1e4 | Iustin Pop | |
10 | 0c37d1e4 | Iustin Pop | This program is distributed in the hope that it will be useful, but |
11 | 0c37d1e4 | Iustin Pop | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | 0c37d1e4 | Iustin Pop | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | 0c37d1e4 | Iustin Pop | General Public License for more details. |
14 | 0c37d1e4 | Iustin Pop | |
15 | 0c37d1e4 | Iustin Pop | You should have received a copy of the GNU General Public License |
16 | 0c37d1e4 | Iustin Pop | along with this program; if not, write to the Free Software |
17 | 0c37d1e4 | Iustin Pop | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 | 0c37d1e4 | Iustin Pop | 02110-1301, USA. |
19 | 0c37d1e4 | Iustin Pop | |
20 | 0c37d1e4 | Iustin Pop | -} |
21 | 0c37d1e4 | Iustin Pop | |
22 | 0c37d1e4 | Iustin Pop | module Ganeti.BasicTypes |
23 | 0c37d1e4 | Iustin Pop | ( Result(..) |
24 | 0c37d1e4 | Iustin Pop | , isOk |
25 | 0c37d1e4 | Iustin Pop | , isBad |
26 | 0c37d1e4 | Iustin Pop | , eitherToResult |
27 | f3f76ccc | Iustin Pop | , annotateResult |
28 | 0c37d1e4 | Iustin Pop | ) where |
29 | 0c37d1e4 | Iustin Pop | |
30 | 0c37d1e4 | Iustin Pop | import Control.Monad |
31 | 0c37d1e4 | Iustin Pop | |
32 | 0c37d1e4 | Iustin Pop | -- | This is similar to the JSON library Result type - /very/ similar, |
33 | 0c37d1e4 | Iustin Pop | -- but we want to use it in multiple places, so we abstract it into a |
34 | 0c37d1e4 | Iustin Pop | -- mini-library here. |
35 | 0c37d1e4 | Iustin Pop | -- |
36 | 0c37d1e4 | Iustin Pop | -- The failure value for this monad is simply a string. |
37 | 0c37d1e4 | Iustin Pop | data Result a |
38 | 0c37d1e4 | Iustin Pop | = Bad String |
39 | 0c37d1e4 | Iustin Pop | | Ok a |
40 | 0c37d1e4 | Iustin Pop | deriving (Show, Read, Eq) |
41 | 0c37d1e4 | Iustin Pop | |
42 | 0c37d1e4 | Iustin Pop | instance Monad Result where |
43 | 0c37d1e4 | Iustin Pop | (>>=) (Bad x) _ = Bad x |
44 | 0c37d1e4 | Iustin Pop | (>>=) (Ok x) fn = fn x |
45 | 0c37d1e4 | Iustin Pop | return = Ok |
46 | 0c37d1e4 | Iustin Pop | fail = Bad |
47 | 0c37d1e4 | Iustin Pop | |
48 | 0c37d1e4 | Iustin Pop | instance MonadPlus Result where |
49 | 0c37d1e4 | Iustin Pop | mzero = Bad "zero Result when used as MonadPlus" |
50 | 0c37d1e4 | Iustin Pop | -- for mplus, when we 'add' two Bad values, we concatenate their |
51 | 0c37d1e4 | Iustin Pop | -- error descriptions |
52 | 0c37d1e4 | Iustin Pop | (Bad x) `mplus` (Bad y) = Bad (x ++ "; " ++ y) |
53 | 0c37d1e4 | Iustin Pop | (Bad _) `mplus` x = x |
54 | 0c37d1e4 | Iustin Pop | x@(Ok _) `mplus` _ = x |
55 | 0c37d1e4 | Iustin Pop | |
56 | 0c37d1e4 | Iustin Pop | -- | Simple checker for whether a 'Result' is OK. |
57 | 0c37d1e4 | Iustin Pop | isOk :: Result a -> Bool |
58 | 0c37d1e4 | Iustin Pop | isOk (Ok _) = True |
59 | 0c37d1e4 | Iustin Pop | isOk _ = False |
60 | 0c37d1e4 | Iustin Pop | |
61 | 0c37d1e4 | Iustin Pop | -- | Simple checker for whether a 'Result' is a failure. |
62 | 0c37d1e4 | Iustin Pop | isBad :: Result a -> Bool |
63 | 0c37d1e4 | Iustin Pop | isBad = not . isOk |
64 | 0c37d1e4 | Iustin Pop | |
65 | 0c37d1e4 | Iustin Pop | -- | Converter from Either String to 'Result'. |
66 | 0c37d1e4 | Iustin Pop | eitherToResult :: Either String a -> Result a |
67 | 0c37d1e4 | Iustin Pop | eitherToResult (Left s) = Bad s |
68 | 0c37d1e4 | Iustin Pop | eitherToResult (Right v) = Ok v |
69 | f3f76ccc | Iustin Pop | |
70 | f3f76ccc | Iustin Pop | -- | Annotate a Result with an ownership information. |
71 | f3f76ccc | Iustin Pop | annotateResult :: String -> Result a -> Result a |
72 | f3f76ccc | Iustin Pop | annotateResult owner (Bad s) = Bad $ owner ++ ": " ++ s |
73 | f3f76ccc | Iustin Pop | annotateResult _ v = v |