remote: better errors for bool and enum serializers

This commit is contained in:
sorki 2023-11-29 06:04:13 +01:00
parent 2e9ab694fa
commit da8eb42367
2 changed files with 21 additions and 5 deletions

View File

@ -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
@ -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)
@ -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

View File

@ -41,7 +41,7 @@ roundtripS
:: ( Eq a
, Show a
)
=> NixSerializer () () a
=> NixSerializer () GetError a
-> a
-> Expectation
roundtripS serializer = roundtripSReader serializer ()