mirror of
https://github.com/haskell-nix/hnix-store.git
synced 2024-09-11 18:05:56 +03:00
remote: Serialize.Prim - add toEnumCheckBounds util
This commit is contained in:
parent
79310d5b7b
commit
4a4625883f
@ -50,12 +50,18 @@ putBool False = putInt (0 :: Int)
|
||||
|
||||
-- * Enum
|
||||
|
||||
-- | Utility toEnum version checking bounds using Bounded class
|
||||
toEnumCheckBounds :: Enum a => Int -> Either String a
|
||||
toEnumCheckBounds = \case
|
||||
x | x < minBound -> Left $ "enum out of min bound " ++ show x
|
||||
x | x > maxBound -> Left $ "enum out of max bound " ++ show x
|
||||
x | otherwise -> Right $ toEnum x
|
||||
|
||||
-- | Deserialize @Enum@ to integer
|
||||
getEnum :: Enum a => Get a
|
||||
getEnum = getInt >>= \case
|
||||
x | x < minBound -> fail $ "enum out of min bound " ++ show x
|
||||
x | x > maxBound -> fail $ "enum out of max bound " ++ show x
|
||||
x | otherwise -> pure $ toEnum x
|
||||
getEnum =
|
||||
toEnumCheckBounds <$> getInt
|
||||
>>= either fail pure
|
||||
|
||||
-- | Serialize @Enum@ to integer
|
||||
putEnum :: Enum a => Putter a
|
||||
|
Loading…
Reference in New Issue
Block a user