Revision a30b473c htools/Ganeti/HTools/Types.hs
b/htools/Ganeti/HTools/Types.hs | ||
---|---|---|
56 | 56 |
, Result(..) |
57 | 57 |
, isOk |
58 | 58 |
, isBad |
59 |
, eitherToResult |
|
59 | 60 |
, Element(..) |
60 | 61 |
, FailMode(..) |
61 | 62 |
, FailStats |
62 | 63 |
, OpResult(..) |
64 |
, opToResult |
|
63 | 65 |
, connTimeout |
64 | 66 |
, queryTimeout |
65 | 67 |
, EvacMode(..) |
... | ... | |
254 | 256 |
unitCpu :: Int |
255 | 257 |
unitCpu = 1 |
256 | 258 |
|
257 |
{-| |
|
258 |
|
|
259 |
This is similar to the JSON library Result type - /very/ similar, but |
|
260 |
we want to use it in multiple places, so we abstract it into a |
|
261 |
mini-library here |
|
262 |
|
|
263 |
-} |
|
259 |
-- | This is similar to the JSON library Result type - /very/ similar, |
|
260 |
-- but we want to use it in multiple places, so we abstract it into a |
|
261 |
-- mini-library here. |
|
262 |
-- |
|
263 |
-- The failure value for this monad is simply a string. |
|
264 | 264 |
data Result a |
265 | 265 |
= Bad String |
266 | 266 |
| Ok a |
... | ... | |
281 | 281 |
isBad :: Result a -> Bool |
282 | 282 |
isBad = not . isOk |
283 | 283 |
|
284 |
-- | Converter from Either String to 'Result' |
|
285 |
eitherToResult :: Either String a -> Result a |
|
286 |
eitherToResult (Left s) = Bad s |
|
287 |
eitherToResult (Right v) = Ok v |
|
288 |
|
|
284 | 289 |
-- | Reason for an operation's falure. |
285 | 290 |
data FailMode = FailMem -- ^ Failed due to not enough RAM |
286 | 291 |
| FailDisk -- ^ Failed due to not enough disk |
... | ... | |
293 | 298 |
type FailStats = [(FailMode, Int)] |
294 | 299 |
|
295 | 300 |
-- | Either-like data-type customized for our failure modes. |
301 |
-- |
|
302 |
-- The failure values for this monad track the specific allocation |
|
303 |
-- failures, so this is not a general error-monad (compare with the |
|
304 |
-- 'Result' data type). One downside is that this type cannot encode a |
|
305 |
-- generic failure mode, hence 'fail' for this monad is not defined |
|
306 |
-- and will cause an exception. |
|
296 | 307 |
data OpResult a = OpFail FailMode -- ^ Failed operation |
297 | 308 |
| OpGood a -- ^ Success operation |
298 | 309 |
deriving (Show, Read) |
... | ... | |
302 | 313 |
(OpFail y) >>= _ = OpFail y |
303 | 314 |
return = OpGood |
304 | 315 |
|
316 |
-- | Conversion from 'OpResult' to 'Result'. |
|
317 |
opToResult :: OpResult a -> Result a |
|
318 |
opToResult (OpFail f) = Bad $ show f |
|
319 |
opToResult (OpGood v) = Ok v |
|
320 |
|
|
305 | 321 |
-- | A generic class for items that have updateable names and indices. |
306 | 322 |
class Element a where |
307 | 323 |
-- | Returns the name of the element |
Also available in: Unified diff