scale-1.0.1.0: src/Codec/Scale/Class.hs
{-# 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 = gPut . 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 = to <$> gGet
-- | A class for decoding generically composed datatypes from SCALE format.
class GDecode a where
gGet :: Get a