Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serialise: add deserialiseFullyOrFail #251

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 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,18 @@ 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,
-- or get back a @'DeserialiseFailure'@. In addition to the decoded value
-- return any remaining input content and the number of bytes consumed.
--
-- @since 0.2.4.0
tryDeserialise :: Serialise a
=> BS.ByteString
-> Either CBOR.Read.DeserialiseFailure
(BS.ByteString, CBOR.Read.ByteOffset, a)
tryDeserialise = CBOR.Read.deserialiseFromBytesWithSize decode


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

Expand Down