{-# OPTIONS_HADDOCK prune not-home #-}

{- |
Copyright   : (c) 2022 Tim Emiola
Maintainer  : Tim Emiola <adetokunbo@emio.la>
SPDX-License-Identifier: BSD3
-}
module KeyedVals.Handle.Codec.HttpApiData (
  -- * newtypes
  HttpApiDataOf (..),
) where

import KeyedVals.Handle.Codec (DecodeKV (..), EncodeKV (..))
import Web.HttpApiData (
  FromHttpApiData (..),
  ToHttpApiData (..),
 )


{- | A deriving-via helper type for types that implement 'DecodeKV' and 'EncodeKV'
 using 'FromHttpApiData' and 'ToHttpApiData' type classes.
-}
newtype HttpApiDataOf a = HttpApiDataOf {forall a. HttpApiDataOf a -> a
fromHttpApiDataOf :: a}


instance FromHttpApiData a => DecodeKV (HttpApiDataOf a) where
  decodeKV :: Val -> Either Text (HttpApiDataOf a)
decodeKV = (a -> HttpApiDataOf a)
-> Either Text a -> Either Text (HttpApiDataOf a)
forall a b. (a -> b) -> Either Text a -> Either Text b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> HttpApiDataOf a
forall a. a -> HttpApiDataOf a
HttpApiDataOf (Either Text a -> Either Text (HttpApiDataOf a))
-> (Val -> Either Text a) -> Val -> Either Text (HttpApiDataOf a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Val -> Either Text a
forall a. FromHttpApiData a => Val -> Either Text a
parseHeader


instance ToHttpApiData a => EncodeKV (HttpApiDataOf a) where
  encodeKV :: HttpApiDataOf a -> Val
encodeKV = a -> Val
forall a. ToHttpApiData a => a -> Val
toHeader (a -> Val) -> (HttpApiDataOf a -> a) -> HttpApiDataOf a -> Val
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HttpApiDataOf a -> a
forall a. HttpApiDataOf a -> a
fromHttpApiDataOf