3 Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
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.
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.
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
22 module Ganeti.BasicTypes
33 -- | This is similar to the JSON library Result type - /very/ similar,
34 -- but we want to use it in multiple places, so we abstract it into a
37 -- The failure value for this monad is simply a string.
41 deriving (Show, Read, Eq)
43 instance Monad Result where
44 (>>=) (Bad x) _ = Bad x
45 (>>=) (Ok x) fn = fn x
49 instance MonadPlus Result where
50 mzero = Bad "zero Result when used as MonadPlus"
51 -- for mplus, when we 'add' two Bad values, we concatenate their
53 (Bad x) `mplus` (Bad y) = Bad (x ++ "; " ++ y)
55 x@(Ok _) `mplus` _ = x
57 -- | Simple checker for whether a 'Result' is OK.
58 isOk :: Result a -> Bool
62 -- | Simple checker for whether a 'Result' is a failure.
63 isBad :: Result a -> Bool
66 -- | Converter from Either String to 'Result'.
67 eitherToResult :: Either String a -> Result a
68 eitherToResult (Left s) = Bad s
69 eitherToResult (Right v) = Ok v
71 -- | Annotate a Result with an ownership information.
72 annotateResult :: String -> Result a -> Result a
73 annotateResult owner (Bad s) = Bad $ owner ++ ": " ++ s
74 annotateResult _ v = v
76 -- | Annotates and transforms IOErrors into a Result type. This can be
77 -- used in the error handler argument to 'catch', for example.
78 annotateIOError :: String -> IOError -> IO (Result a)
79 annotateIOError description exc =
80 return . Bad $ description ++ ": " ++ show exc