{-# LANGUAGE TypeFamilies #-}

-- |
-- Module      :  Data.Solidity.Abi.Codec
-- Copyright   :  Aleksandr Krupenkin 2016-2021
-- License     :  Apache-2.0
--
-- Maintainer  :  mail@akru.me
-- Stability   :  experimental
-- Portability :  noportable
--
-- Solidity contract ABI encoding functions.
--

module Data.Solidity.Abi.Codec
    (
    -- * @AbiPut@/@AbiGet@ type class encoding
      encode
    , decode

    -- * Generic encoding
    , encode'
    , decode'

    -- * Generic type re-export
    , 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 datatype to Ethereum Abi-encoding
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

-- | Generic driven version of 'encode'
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 datatype from Ethereum Abi-encoding
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

-- | Generic driven version of 'decode'
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