From 0c37d1e4053ee8f3465eb9fca962128253f18178 Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Mon, 21 Nov 2011 12:44:30 +0100 Subject: [PATCH] htools: split parts of HTools/Types.hs into BasicTypes.hs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 'Result' type is common and: - might be used outside of HTools-specific code too - is better split as we need these basic types for building the more complex ones in Types.hs Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: RenΓ© Nussbaumer <rn@google.com> --- Makefile.am | 1 + htools/Ganeti/BasicTypes.hs | 67 +++++++++++++++++++++++++++++++++++ htools/Ganeti/HTools/Types.hs | 40 +-------------------- 3 files changed, 69 insertions(+), 39 deletions(-) create mode 100644 htools/Ganeti/BasicTypes.hs diff --git a/Makefile.am b/Makefile.am index d538a8479..759f71719 100644 --- a/Makefile.am +++ b/Makefile.am @@ -387,6 +387,7 @@ HS_LIB_SRCS = \ htools/Ganeti/HTools/Program/Hbal.hs \ htools/Ganeti/HTools/Program/Hscan.hs \ htools/Ganeti/HTools/Program/Hspace.hs \ + htools/Ganeti/BasicTypes.hs \ htools/Ganeti/Jobs.hs \ htools/Ganeti/Luxi.hs \ htools/Ganeti/OpCodes.hs \ diff --git a/htools/Ganeti/BasicTypes.hs b/htools/Ganeti/BasicTypes.hs new file mode 100644 index 000000000..031e3c298 --- /dev/null +++ b/htools/Ganeti/BasicTypes.hs @@ -0,0 +1,67 @@ +{- + +Copyright (C) 2009, 2010, 2011 Google Inc. + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301, USA. + +-} + +module Ganeti.BasicTypes + ( Result(..) + , isOk + , isBad + , eitherToResult + ) where + +import Control.Monad + +-- | This is similar to the JSON library Result type - /very/ similar, +-- but we want to use it in multiple places, so we abstract it into a +-- mini-library here. +-- +-- The failure value for this monad is simply a string. +data Result a + = Bad String + | Ok a + deriving (Show, Read, Eq) + +instance Monad Result where + (>>=) (Bad x) _ = Bad x + (>>=) (Ok x) fn = fn x + 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 +isOk _ = False + +-- | Simple checker for whether a 'Result' is a failure. +isBad :: Result a -> Bool +isBad = not . isOk + +-- | Converter from Either String to 'Result'. +eitherToResult :: Either String a -> Result a +eitherToResult (Left s) = Bad s +eitherToResult (Right v) = Ok v diff --git a/htools/Ganeti/HTools/Types.hs b/htools/Ganeti/HTools/Types.hs index 208686bfe..644d4d979 100644 --- a/htools/Ganeti/HTools/Types.hs +++ b/htools/Ganeti/HTools/Types.hs @@ -72,12 +72,12 @@ module Ganeti.HTools.Types , EvacMode(..) ) where -import Control.Monad import qualified Data.Map as M import qualified Text.JSON as JSON import qualified Ganeti.Constants as C import qualified Ganeti.THH as THH +import Ganeti.BasicTypes -- | The instance index type. type Idx = Int @@ -226,44 +226,6 @@ unitDsk = 256 unitCpu :: Int unitCpu = 1 --- | This is similar to the JSON library Result type - /very/ similar, --- but we want to use it in multiple places, so we abstract it into a --- mini-library here. --- --- The failure value for this monad is simply a string. -data Result a - = Bad String - | Ok a - deriving (Show, Read, Eq) - -instance Monad Result where - (>>=) (Bad x) _ = Bad x - (>>=) (Ok x) fn = fn x - 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 -isOk _ = False - --- | Simple checker for whether a 'Result' is a failure. -isBad :: Result a -> Bool -isBad = not . isOk - --- | Converter from Either String to 'Result'. -eitherToResult :: Either String a -> Result a -eitherToResult (Left s) = Bad s -eitherToResult (Right v) = Ok v - -- | Reason for an operation's falure. data FailMode = FailMem -- ^ Failed due to not enough RAM | FailDisk -- ^ Failed due to not enough disk -- GitLab