From 1c7c4578ec021ec1bbf4ea4efe5be6a7ed5f0cde Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Tue, 20 Sep 2011 17:28:49 +0900
Subject: [PATCH] htools: add a MonadPlus instance for Result
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This will be used to implement more easily 'choice' parsing of input
data, without resorting to syntax (case … of Bad _ -> …).

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Agata Murawska <agatamurawska@google.com>
---
 htools/Ganeti/HTools/Types.hs | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/htools/Ganeti/HTools/Types.hs b/htools/Ganeti/HTools/Types.hs
index 6452f5b6d..ec774b012 100644
--- a/htools/Ganeti/HTools/Types.hs
+++ b/htools/Ganeti/HTools/Types.hs
@@ -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
-- 
GitLab