Skip to content

Commit

Permalink
serialise: add tryDeserialise for a feature-complete API
Browse files Browse the repository at this point in the history
  • Loading branch information
infinity0 committed Apr 21, 2021
1 parent 9be3fd5 commit f20931b
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions serialise/src/Codec/Serialise.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Codec.Serialise
serialise
, deserialise
, deserialiseOrFail
, tryDeserialise

-- * Deserialisation exceptions
, CBOR.Read.DeserialiseFailure(..)
Expand Down Expand Up @@ -115,6 +116,9 @@ serialise = CBOR.Write.toLazyByteString . encode
-- representation is invalid or does not correspond to a value of the
-- expected type.
--
-- The representation may have leftover content that is ignored by this
-- function; use 'tryDeserialise' if you need to check this.
--
-- @since 0.2.0.0
deserialise :: Serialise a => BS.ByteString -> a
deserialise bs0 =
Expand All @@ -130,6 +134,9 @@ deserialise bs0 =
-- | Deserialise a Haskell value from the external binary representation,
-- or get back a @'DeserialiseFailure'@.
--
-- The representation may have leftover content that is ignored by this
-- function; use 'tryDeserialise' if you need to check this.
--
-- @since 0.2.0.0
deserialiseOrFail :: Serialise a => BS.ByteString -> Either CBOR.Read.DeserialiseFailure a
deserialiseOrFail bs0 =
Expand All @@ -142,6 +149,15 @@ deserialiseOrFail bs0 =
BS.Empty -> k Nothing >>= supplyAllInput BS.Empty
supplyAllInput _ (CBOR.Read.Fail _ _ exn) = return (Left exn)

-- | Deserialise a Haskell value from the external binary representation,
-- checking that the representation has been consumed fully with no leftovers,
-- or get back a @'Either' 'DeserialiseFailure' 'ByteString'@.
--
-- @since 0.2.4.0
tryDeserialise :: Serialise a => BS.ByteString -> Either CBOR.Read.DeserialiseFailure (BS.ByteString, a)
tryDeserialise = CBOR.Read.deserialiseFromBytes decode


--------------------------------------------------------------------------------
-- File-based API

Expand Down

0 comments on commit f20931b

Please sign in to comment.