module Data.Codec.Testing
(
ParseResult(..)
, roundTrip, roundTripStorable
)
where
import Data.Aeson.Types (Result(..))
import Data.Codec.Codec
import Foreign
class ParseResult f where
toEither :: f a -> Either String a
instance ParseResult (Either String) where
toEither = id
instance ParseResult Maybe where
toEither = maybe (Left "Nothing") Right
instance ParseResult Result where
toEither = \case
Error err -> Left err
Success x -> Right x
roundTrip :: ParseResult f => ConcreteCodec c f a -> a -> Either String a
roundTrip cd
= toEither . parseVal cd . produceVal cd
roundTripStorable :: Storable a => a -> IO a
roundTripStorable x
= with x peek