Revision a8038349 htools/Ganeti/HTools/Types.hs

b/htools/Ganeti/HTools/Types.hs
65 65
  , Element(..)
66 66
  , FailMode(..)
67 67
  , FailStats
68
  , OpResult(..)
68
  , OpResult
69 69
  , opToResult
70 70
  , EvacMode(..)
71 71
  , ISpec(..)
......
344 344
-- The failure values for this monad track the specific allocation
345 345
-- failures, so this is not a general error-monad (compare with the
346 346
-- 'Result' data type). One downside is that this type cannot encode a
347
-- generic failure mode, hence 'fail' for this monad is not defined
348
-- and will cause an exception.
349
data OpResult a = OpFail FailMode -- ^ Failed operation
350
                | OpGood a        -- ^ Success operation
351
                  deriving (Show, Read)
347
-- generic failure mode, hence our way to build a FailMode from string
348
-- will instead raise an exception.
349
type OpResult = GenericResult FailMode
352 350

  
353
instance Monad OpResult where
354
  (OpGood x) >>= fn = fn x
355
  (OpFail y) >>= _ = OpFail y
356
  return = OpGood
351
-- | 'FromString' instance for 'FailMode' designed to catch unintended
352
-- use as a general monad.
353
instance FromString FailMode where
354
  mkFromString v = error $ "Programming error: OpResult used as generic monad"
355
                           ++ v
357 356

  
358 357
-- | Conversion from 'OpResult' to 'Result'.
359 358
opToResult :: OpResult a -> Result a
360
opToResult (OpFail f) = Bad $ show f
361
opToResult (OpGood v) = Ok v
359
opToResult (Bad f) = Bad $ show f
360
opToResult (Ok v) = Ok v
362 361

  
363 362
-- | A generic class for items that have updateable names and indices.
364 363
class Element a where

Also available in: Unified diff