Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cardano-api/src/Cardano/Api/Serialise/TextEnvelope.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ module Cardano.Api.Serialise.TextEnvelope
-- ** Reading one of several key types
, FromSomeType (..)
, deserialiseFromTextEnvelopeAnyOf
, decodeTextEnvelopeJSON
, deserialiseFromTextEnvelopeJSON
, deserialiseFromTextEnvelopeJSONAnyOf
, readFileTextEnvelopeAnyOf

-- ** Data family instances
Expand Down
42 changes: 36 additions & 6 deletions cardano-api/src/Cardano/Api/Serialise/TextEnvelope/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ module Cardano.Api.Serialise.TextEnvelope.Internal
-- * Reading one of several key types
, FromSomeType (..)
, deserialiseFromTextEnvelopeAnyOf
, decodeTextEnvelopeJSON
, deserialiseFromTextEnvelopeJSON
, deserialiseFromTextEnvelopeJSONAnyOf
, readFileTextEnvelopeAnyOf

-- * Data family instances
Expand Down Expand Up @@ -250,6 +253,33 @@ deserialiseFromTextEnvelopeAnyOf types te =

matching (FromSomeType ttoken _f) = textEnvelopeType ttoken `legacyComparison` actualType

-- | Decode a JSON-encoded 'TextEnvelope' from a strict 'ByteString' (UTF-8).
-- Returns 'TextEnvelopeAesonDecodeError' if the JSON parsing fails.
decodeTextEnvelopeJSON :: ByteString -> Either TextEnvelopeError TextEnvelope
decodeTextEnvelopeJSON bs =
first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' bs

-- | Deserialise a value from a JSON-encoded text envelope 'ByteString' (UTF-8).
-- This performs no file I\/O. Returns 'TextEnvelopeAesonDecodeError' for JSON
-- parse failures, or downstream errors from 'deserialiseFromTextEnvelope' for
-- type mismatches and CBOR decoding failures.
deserialiseFromTextEnvelopeJSON
:: HasTextEnvelope a
=> ByteString -> Either TextEnvelopeError a
deserialiseFromTextEnvelopeJSON bs =
decodeTextEnvelopeJSON bs >>= deserialiseFromTextEnvelope

-- | Like 'deserialiseFromTextEnvelopeJSON' but accepts multiple target types.
-- This performs no file I\/O. Returns 'TextEnvelopeAesonDecodeError' for JSON
-- parse failures, or downstream errors from 'deserialiseFromTextEnvelopeAnyOf'
-- for type mismatches and CBOR decoding failures.
deserialiseFromTextEnvelopeJSONAnyOf
:: [FromSomeType HasTextEnvelope b]
-> ByteString
-> Either TextEnvelopeError b
deserialiseFromTextEnvelopeJSONAnyOf types bs =
decodeTextEnvelopeJSON bs >>= deserialiseFromTextEnvelopeAnyOf types

writeFileTextEnvelope
:: HasTextEnvelope a
=> File content Out
Expand All @@ -274,9 +304,9 @@ readFileTextEnvelope
readFileTextEnvelope path =
runExceptT $ do
content <- fileIOExceptT (unFile path) readFileBlocking
firstExceptT (FileError (unFile path)) $ hoistEither $ do
te <- first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' content
deserialiseFromTextEnvelope te
firstExceptT (FileError (unFile path)) $
hoistEither $
deserialiseFromTextEnvelopeJSON content

readFileTextEnvelopeAnyOf
:: [FromSomeType HasTextEnvelope b]
Expand All @@ -285,9 +315,9 @@ readFileTextEnvelopeAnyOf
readFileTextEnvelopeAnyOf types path =
runExceptT $ do
content <- fileIOExceptT (unFile path) readFileBlocking
firstExceptT (FileError (unFile path)) $ hoistEither $ do
te <- first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' content
deserialiseFromTextEnvelopeAnyOf types te
firstExceptT (FileError (unFile path)) $
hoistEither $
deserialiseFromTextEnvelopeJSONAnyOf types content

readTextEnvelopeFromFile
:: FilePath
Expand Down
Loading