{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE TypeFamilies      #-}
{-# LANGUAGE TypeOperators     #-}

-- |
-- Module      :  Codec.Scale.Class
-- Copyright   :  Aleksandr Krupenkin 2016-2024
-- License     :  Apache-2.0
--
-- Maintainer  :  mail@akru.me
-- Stability   :  experimental
-- Portability :  unportable
--
--
--

module Codec.Scale.Class where

import           Data.Serialize (Get, Putter)
import           Generics.SOP   (Generic, Rep, from, to)

-- | A class for encoding datatypes to SCALE format.
--
-- If your compiler has support for the @DeriveGeneric@ and
-- @DefaultSignatures@ language extensions (@ghc >= 7.2.1@),
-- the 'put' method will have default generic implementations.
--
-- To use this option, simply add a @deriving 'Generic'@ clause
-- to your datatype and declare a 'Encode' instance for it without
-- giving a definition for 'put'.
--
class Encode a where
    put :: Putter a

    default put :: (Generic a, Rep a ~ rep, GEncode rep) => Putter a
    put = Putter (Rep a)
forall a. GEncode a => Putter a
gPut Putter (Rep a) -> (a -> Rep a) -> Putter a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Rep a
forall a. Generic a => a -> Rep a
from

-- | A class for encoding generically composed datatypes to SCALE format.
class GEncode a where
    gPut :: Putter a

-- | A class for decoding datatypes from SCALE format.
--
-- If your compiler has support for the @DeriveGeneric@ and
-- @DefaultSignatures@ language extensions (@ghc >= 7.2.1@),
-- the 'get' method will have default generic implementations.
--
-- To use this option, simply add a @deriving 'Generic'@ clause
-- to your datatype and declare a 'Decode' instance for it without
-- giving a definition for 'get'.
--
class Decode a where
    get :: Get a

    default get :: (Generic a, Rep a ~ rep, GDecode rep) => Get a
    get = Rep a -> a
forall a. Generic a => Rep a -> a
to (Rep a -> a) -> Get (Rep a) -> Get a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (Rep a)
forall a. GDecode a => Get a
gGet

-- | A class for decoding generically composed datatypes from SCALE format.
class GDecode a where
    gGet :: Get a