Skip to content

Commit

Permalink
remote: better errors for bool and enum serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
sorki committed Nov 29, 2023
1 parent c9ab7f5 commit 756bd40
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
24 changes: 20 additions & 4 deletions hnix-store-remote/src/System/Nix/Store/Remote/Serializer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import Data.Map (Map)
import Data.Set (Set)
import Data.Text (Text)
import Data.Time (UTCTime)
import Data.Word (Word64)
import GHC.Generics (Generic)

import qualified Control.Monad
Expand Down Expand Up @@ -111,6 +112,9 @@ type NixSerializer r e = Serializer (SerialT r e)

data GetError
= GetError
| GetError_EnumOutOfMinBound Int
| GetError_EnumOutOfMaxBound Int
| GetError_IllegalBool Word64
| GetError_Path InvalidPathError
deriving (Eq, Ord, Generic, Show)

Expand Down Expand Up @@ -149,14 +153,26 @@ runP serializer r =
int :: Integral a => NixSerializer r e a
int = lift2 getInt putInt

bool :: NixSerializer r e Bool
bool = lift2 getBool putBool
bool :: NixSerializer r GetError Bool
bool = Serializer
{ getS = getS (int @Word64) >>= \case
0 -> pure False
1 -> pure True
x -> throwError $ GetError_IllegalBool x
, putS = lift . putBool
}

byteString :: NixSerializer r e ByteString
byteString = lift2 getByteString putByteString

enum :: Enum a => NixSerializer r e a
enum = lift2 getEnum putEnum
enum :: Enum a => NixSerializer r GetError a
enum = Serializer
{ getS = getS int >>= \case
x | x < minBound -> throwError $ GetError_EnumOutOfMinBound x
x | x > maxBound -> throwError $ GetError_EnumOutOfMaxBound x
x | otherwise -> pure $ toEnum x
, putS = lift . putEnum
}

text :: NixSerializer r e Text
text = liftSerialize
Expand Down
2 changes: 1 addition & 1 deletion hnix-store-remote/tests/NixSerializerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ roundtripS
:: ( Eq a
, Show a
)
=> NixSerializer () () a
=> NixSerializer () GetError a
-> a
-> Expectation
roundtripS serializer = roundtripSReader serializer ()
Expand Down

0 comments on commit 756bd40

Please sign in to comment.