Commit 1c7c4578 authored by Iustin Pop's avatar Iustin Pop Committed by Michael Hanselmann
Browse files

htools: add a MonadPlus instance for Result



This will be used to implement more easily 'choice' parsing of input
data, without resorting to syntax (case … of Bad _ -> …).
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarAgata Murawska <agatamurawska@google.com>
parent 5916e61a
......@@ -67,6 +67,7 @@ module Ganeti.HTools.Types
, EvacMode(..)
) where
import Control.Monad
import qualified Data.Map as M
import qualified Text.JSON as JSON
......@@ -272,6 +273,14 @@ instance Monad Result where
return = Ok
fail = Bad
instance MonadPlus Result where
mzero = Bad "zero Result when used as MonadPlus"
-- for mplus, when we 'add' two Bad values, we concatenate their
-- error descriptions
(Bad x) `mplus` (Bad y) = Bad (x ++ "; " ++ y)
(Bad _) `mplus` x = x
x@(Ok _) `mplus` _ = x
-- | Simple checker for whether a 'Result' is OK.
isOk :: Result a -> Bool
isOk (Ok _) = True
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment