module Codec.Serialise.Properties
(
serialiseIdentity
, flatTermIdentity
, hasValidFlatTerm
) where
import Codec.CBOR.FlatTerm
import Codec.Serialise (deserialise, serialise)
import Codec.Serialise.Class
serialiseIdentity :: (Serialise a, Eq a) => a -> Bool
serialiseIdentity :: a -> Bool
serialiseIdentity a
a = a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== (ByteString -> a
forall a. Serialise a => ByteString -> a
deserialise (ByteString -> a) -> (a -> ByteString) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ByteString
forall a. Serialise a => a -> ByteString
serialise) a
a
flatTermIdentity :: (Serialise a, Eq a) => a -> Bool
flatTermIdentity :: a -> Bool
flatTermIdentity a
a = a -> Either String a
forall a b. b -> Either a b
Right a
a Either String a -> Either String a -> Bool
forall a. Eq a => a -> a -> Bool
== (FlatTerm -> Either String a
fromFlat (FlatTerm -> Either String a)
-> (a -> FlatTerm) -> a -> Either String a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> FlatTerm
toFlat) a
a
where
toFlat :: a -> FlatTerm
toFlat = Encoding -> FlatTerm
toFlatTerm (Encoding -> FlatTerm) -> (a -> Encoding) -> a -> FlatTerm
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Encoding
forall a. Serialise a => a -> Encoding
encode
fromFlat :: FlatTerm -> Either String a
fromFlat = (forall s. Decoder s a) -> FlatTerm -> Either String a
forall a. (forall s. Decoder s a) -> FlatTerm -> Either String a
fromFlatTerm forall s. Decoder s a
forall a s. Serialise a => Decoder s a
decode
hasValidFlatTerm :: Serialise a => a -> Bool
hasValidFlatTerm :: a -> Bool
hasValidFlatTerm = FlatTerm -> Bool
validFlatTerm (FlatTerm -> Bool) -> (a -> FlatTerm) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Encoding -> FlatTerm
toFlatTerm (Encoding -> FlatTerm) -> (a -> Encoding) -> a -> FlatTerm
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Encoding
forall a. Serialise a => a -> Encoding
encode