Add entire ConfigData serialisation tests
[ganeti-local] / htools / Ganeti / BasicTypes.hs
index 55bab28..61a7e56 100644 (file)
@@ -36,6 +36,7 @@ module Ganeti.BasicTypes
   , compareNameComponent
   ) where
 
+import Control.Applicative
 import Control.Monad
 import Data.Function
 import Data.List
@@ -56,6 +57,10 @@ instance Monad Result where
   return = Ok
   fail = Bad
 
+instance Functor Result where
+  fmap _ (Bad msg) = Bad msg
+  fmap fn (Ok val) = Ok (fn val)
+
 instance MonadPlus Result where
   mzero = Bad "zero Result when used as MonadPlus"
   -- for mplus, when we 'add' two Bad values, we concatenate their
@@ -64,6 +69,12 @@ instance MonadPlus Result where
   (Bad _) `mplus` x = x
   x@(Ok _) `mplus` _ = x
 
+instance Applicative Result where
+  pure = Ok
+  (Bad f) <*> _       = Bad f
+  _       <*> (Bad x) = Bad x
+  (Ok f)  <*> (Ok x)  = Ok $ f x
+
 -- | Simple checker for whether a 'Result' is OK.
 isOk :: Result a -> Bool
 isOk (Ok _) = True