{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wno-duplicate-exports #-}

module Cardano.Api.HasTypeProxy
  ( HasTypeProxy (AsType, proxyToAsType)
  , asType
  , AsType (..)
  , Proxy (..)
  , FromSomeType (..)
  )
where

import Data.ByteString qualified as BS
import Data.ByteString.Lazy qualified as BSL
import Data.Kind (Constraint, Type)
import Data.Typeable
import Data.Word (Word16, Word8)
import Numeric.Natural (Natural)

class Typeable t => HasTypeProxy t where
  -- | A family of singleton types used in this API to indicate which type to
  -- use where it would otherwise be ambiguous or merely unclear.
  --
  -- Values of this type are passed to deserialisation functions for example.
  data AsType t

  proxyToAsType :: Proxy t -> AsType t

-- | Generalised show instance for all singletons of 'AsType' displaying the type.
instance Typeable t => Show (AsType t) where
  show :: AsType t -> String
show = TypeRep -> String
forall a. Show a => a -> String
show (TypeRep -> String) -> (AsType t -> TypeRep) -> AsType t -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AsType t -> TypeRep
forall a. Typeable a => a -> TypeRep
typeOf

instance HasTypeProxy Word8 where
  data AsType Word8 = AsWord8
  proxyToAsType :: Proxy Word8 -> AsType Word8
proxyToAsType Proxy Word8
_ = AsType Word8
AsWord8

instance HasTypeProxy Word16 where
  data AsType Word16 = AsWord16
  proxyToAsType :: Proxy Word16 -> AsType Word16
proxyToAsType Proxy Word16
_ = AsType Word16
AsWord16

instance HasTypeProxy Natural where
  data AsType Natural = AsNatural
  proxyToAsType :: Proxy Natural -> AsType Natural
proxyToAsType Proxy Natural
_ = AsType Natural
AsNatural

instance HasTypeProxy BS.ByteString where
  data AsType BS.ByteString = AsByteString
  proxyToAsType :: Proxy ByteString -> AsType ByteString
proxyToAsType Proxy ByteString
_ = AsType ByteString
AsByteString

instance HasTypeProxy BSL.ByteString where
  data AsType BSL.ByteString = AsByteStringLazy
  proxyToAsType :: Proxy ByteString -> AsType ByteString
proxyToAsType Proxy ByteString
_ = AsType ByteString
AsByteStringLazy

data FromSomeType (c :: Type -> Constraint) b where
  FromSomeType :: c a => AsType a -> (a -> b) -> FromSomeType c b

-- | Provide type proxy from the already existing 'HasTypeProxy' instance
asType :: HasTypeProxy t => AsType t
asType :: forall t. HasTypeProxy t => AsType t
asType = Proxy t -> AsType t
forall t. HasTypeProxy t => Proxy t -> AsType t
proxyToAsType Proxy t
forall {k} (t :: k). Proxy t
Proxy