Skip to content

Commit de88b65

Browse files
authored
Merge pull request #284 from haskell-nix/srk/enumBounds
fix min/maxBound serializer checks
2 parents c105037 + 67687b7 commit de88b65

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

hnix-store-core/src/System/Nix/Build.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ data BuildMode
2424
= BuildMode_Normal -- ^ Perform normal build
2525
| BuildMode_Repair -- ^ Try to repair corrupted or missing paths by re-building or re-downloading them
2626
| BuildMode_Check -- ^ Check if the build is reproducible (rebuild and compare to previous build)
27-
deriving (Eq, Generic, Ord, Enum, Show)
27+
deriving (Bounded, Eq, Generic, Ord, Enum, Show)
2828

2929
-- | Build result status
3030
data BuildStatus =
@@ -43,7 +43,7 @@ data BuildStatus =
4343
| BuildStatus_NotDeterministic
4444
| BuildStatus_ResolvesToAlreadyValid
4545
| BuildStatus_NoSubstituters
46-
deriving (Eq, Generic, Ord, Enum, Show)
46+
deriving (Bounded, Eq, Generic, Ord, Enum, Show)
4747

4848
-- | Result of the build
4949
data BuildResult = BuildResult

hnix-store-remote/src/Data/Serializer/Example.hs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,24 @@ putBool True = putInt (1 :: Int8)
302302
putBool False = putInt (0 :: Int8)
303303

304304
-- | Utility toEnum version checking bounds using Bounded class
305-
toEnumCheckBounds :: Enum a => Int -> Either String a
305+
toEnumCheckBounds
306+
:: forall a
307+
. ( Bounded a
308+
, Enum a
309+
)
310+
=> Int
311+
-> Either String a
306312
toEnumCheckBounds = \case
307-
x | x < minBound -> Left $ "enum out of min bound " ++ show x
308-
x | x > maxBound -> Left $ "enum out of max bound " ++ show x
313+
x | x < fromEnum (minBound @a) -> Left $ "enum out of min bound " ++ show x
314+
x | x > fromEnum (maxBound @a) -> Left $ "enum out of max bound " ++ show x
309315
x | otherwise -> Right $ toEnum x
310316

311317
-- | Deserialize @Enum@ to integer
312-
getEnum :: Enum a => Get a
318+
getEnum
319+
:: ( Bounded a
320+
, Enum a
321+
)
322+
=> Get a
313323
getEnum =
314324
toEnumCheckBounds <$> getInt
315325
>>= either fail pure

hnix-store-remote/src/System/Nix/Store/Remote/Serializer.hs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,17 +345,23 @@ byteString = Serializer
345345

346346
-- | Utility toEnum version checking bounds using Bounded class
347347
toEnumCheckBoundsM
348-
:: ( Enum a
348+
:: forall a m
349+
. ( Bounded a
350+
, Enum a
349351
, MonadError SError m
350352
)
351353
=> Int
352354
-> m a
353355
toEnumCheckBoundsM = \case
354-
x | x < minBound -> throwError $ SError_EnumOutOfMinBound x
355-
x | x > maxBound -> throwError $ SError_EnumOutOfMaxBound x
356+
x | x < fromEnum (minBound @a) -> throwError $ SError_EnumOutOfMinBound x
357+
x | x > fromEnum (maxBound @a) -> throwError $ SError_EnumOutOfMaxBound x
356358
x | otherwise -> pure $ toEnum x
357359

358-
enum :: Enum a => NixSerializer r SError a
360+
enum
361+
:: ( Bounded a
362+
, Enum a
363+
)
364+
=> NixSerializer r SError a
359365
enum = Serializer
360366
{ getS = getS int >>= toEnumCheckBoundsM
361367
, putS = putS int . fromEnum

hnix-store-remote/tests/EnumSpec.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ spec :: Spec
2424
spec = do
2525
let
2626
itE
27-
:: ( Enum a
27+
:: ( Bounded a
28+
, Enum a
2829
, Show a
2930
)
3031
=> String

0 commit comments

Comments
 (0)