{-# LANGUAGE TypeFamilies #-}
module Data.Solidity.Abi.Codec
(
encode
, decode
, encode'
, decode'
, Generic
) where
import Data.ByteArray (ByteArray, ByteArrayAccess, convert)
import Data.Serialize (runGet, runPut)
import Generics.SOP (Generic, Rep, from, to)
import Data.Solidity.Abi (AbiGet (..), AbiPut (..),
GenericAbiGet (..),
GenericAbiPut (..))
import Data.Solidity.Abi.Generic ()
encode :: (AbiPut a, ByteArray ba)
=> a
-> ba
{-# INLINE encode #-}
encode :: a -> ba
encode = ByteString -> ba
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert (ByteString -> ba) -> (a -> ByteString) -> a -> ba
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Put -> ByteString
runPut (Put -> ByteString) -> (a -> Put) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Put
forall a. AbiPut a => Putter a
abiPut
encode' :: (Generic a,
Rep a ~ rep,
GenericAbiPut rep,
ByteArray ba)
=> a
-> ba
{-# INLINE encode' #-}
encode' :: a -> ba
encode' = ByteString -> ba
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert (ByteString -> ba) -> (a -> ByteString) -> a -> ba
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Put -> ByteString
runPut (Put -> ByteString) -> (a -> Put) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Putter (SOP I (Code a))
forall a. GenericAbiPut a => Putter a
gAbiPut Putter (SOP I (Code a)) -> (a -> SOP I (Code a)) -> a -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> SOP I (Code a)
forall a. Generic a => a -> Rep a
from
decode :: (ByteArrayAccess ba, AbiGet a)
=> ba
-> Either String a
{-# INLINE decode #-}
decode :: ba -> Either String a
decode = Get a -> ByteString -> Either String a
forall a. Get a -> ByteString -> Either String a
runGet Get a
forall a. AbiGet a => Get a
abiGet (ByteString -> Either String a)
-> (ba -> ByteString) -> ba -> Either String a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ba -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert
decode' :: (Generic a,
Rep a ~ rep,
GenericAbiGet rep,
ByteArrayAccess ba)
=> ba
-> Either String a
{-# INLINE decode' #-}
decode' :: ba -> Either String a
decode' = Get a -> ByteString -> Either String a
forall a. Get a -> ByteString -> Either String a
runGet (SOP I (Code a) -> a
forall a. Generic a => Rep a -> a
to (SOP I (Code a) -> a) -> Get (SOP I (Code a)) -> Get a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (SOP I (Code a))
forall a. GenericAbiGet a => Get a
gAbiGet) (ByteString -> Either String a)
-> (ba -> ByteString) -> ba -> Either String a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ba -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert