{-# LANGUAGE ScopedTypeVariables #-}
module Bcc.Api.SerialiseRaw
( SerialiseAsRawBytes(..)
, serialiseToRawBytesHex
, deserialiseFromRawBytesHex
, serialiseToRawBytesHexText
) where
import Prelude
import Data.ByteString (ByteString)
import qualified Data.ByteString.Base16 as Base16
import Data.Text (Text)
import qualified Data.Text.Encoding as Text
import Bcc.Api.HasTypeProxy
class HasTypeProxy a => SerialiseAsRawBytes a where
serialiseToRawBytes :: a -> ByteString
deserialiseFromRawBytes :: AsType a -> ByteString -> Maybe a
serialiseToRawBytesHex :: SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytesHex :: a -> ByteString
serialiseToRawBytesHex = ByteString -> ByteString
Base16.encode (ByteString -> ByteString) -> (a -> ByteString) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes
serialiseToRawBytesHexText :: SerialiseAsRawBytes a => a -> Text
serialiseToRawBytesHexText :: a -> Text
serialiseToRawBytesHexText = ByteString -> Text
Text.decodeUtf8 (ByteString -> Text) -> (a -> ByteString) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytesHex
deserialiseFromRawBytesHex :: SerialiseAsRawBytes a
=> AsType a -> ByteString -> Maybe a
deserialiseFromRawBytesHex :: AsType a -> ByteString -> Maybe a
deserialiseFromRawBytesHex AsType a
proxy ByteString
hex =
case ByteString -> Either String ByteString
Base16.decode ByteString
hex of
Right ByteString
raw -> AsType a -> ByteString -> Maybe a
forall a.
SerialiseAsRawBytes a =>
AsType a -> ByteString -> Maybe a
deserialiseFromRawBytes AsType a
proxy ByteString
raw
Left String
_msg -> Maybe a
forall a. Maybe a
Nothing