-- | CBOR Servant support and wrapper type

module Blockfrost.Types.Shared.CBOR
  where

import Data.Aeson (FromJSON (..), ToJSON (..), withText)
import Data.ByteString.Lazy (ByteString)
import Servant.API (Accept (..), MimeRender (..), MimeUnrender (..))
import Servant.Docs (ToSample (..), singleSample)

import qualified Data.ByteString.Char8
import qualified Data.ByteString.Lazy
import qualified Data.Text

data CBOR

-- | Wrapper for CBOR encoded `ByteString`s
-- used for submitting a transaction
newtype CBORString = CBORString ByteString
  deriving stock (CBORString -> CBORString -> Bool
(CBORString -> CBORString -> Bool)
-> (CBORString -> CBORString -> Bool) -> Eq CBORString
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CBORString -> CBORString -> Bool
== :: CBORString -> CBORString -> Bool
$c/= :: CBORString -> CBORString -> Bool
/= :: CBORString -> CBORString -> Bool
Eq, Int -> CBORString -> ShowS
[CBORString] -> ShowS
CBORString -> String
(Int -> CBORString -> ShowS)
-> (CBORString -> String)
-> ([CBORString] -> ShowS)
-> Show CBORString
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CBORString -> ShowS
showsPrec :: Int -> CBORString -> ShowS
$cshow :: CBORString -> String
show :: CBORString -> String
$cshowList :: [CBORString] -> ShowS
showList :: [CBORString] -> ShowS
Show)

instance ToJSON CBORString where
  toJSON :: CBORString -> Value
toJSON (CBORString ByteString
bs) =
    Text -> Value
forall a. ToJSON a => a -> Value
toJSON
      (Text -> Value) -> (String -> Text) -> String -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
Data.Text.pack
      (String -> Value) -> String -> Value
forall a b. (a -> b) -> a -> b
$ ByteString -> String
Data.ByteString.Char8.unpack
      (ByteString -> String) -> ByteString -> String
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
Data.ByteString.Lazy.toStrict ByteString
bs

instance FromJSON CBORString where
  parseJSON :: Value -> Parser CBORString
parseJSON = String -> (Text -> Parser CBORString) -> Value -> Parser CBORString
forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"CBORString" ((Text -> Parser CBORString) -> Value -> Parser CBORString)
-> (Text -> Parser CBORString) -> Value -> Parser CBORString
forall a b. (a -> b) -> a -> b
$ \Text
t ->
    CBORString -> Parser CBORString
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
      (CBORString -> Parser CBORString)
-> CBORString -> Parser CBORString
forall a b. (a -> b) -> a -> b
$ ByteString -> CBORString
CBORString
      (ByteString -> CBORString)
-> (String -> ByteString) -> String -> CBORString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> ByteString
Data.ByteString.Lazy.fromStrict
        (ByteString -> ByteString)
-> (String -> ByteString) -> String -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
Data.ByteString.Char8.pack
        (String -> CBORString) -> String -> CBORString
forall a b. (a -> b) -> a -> b
$ Text -> String
Data.Text.unpack Text
t

instance Accept CBOR where
  contentType :: Proxy CBOR -> MediaType
contentType = MediaType -> Proxy CBOR -> MediaType
forall a. a -> Proxy CBOR -> a
forall (f :: * -> *) a. Applicative f => a -> f a
pure MediaType
"application/cbor"

instance MimeRender CBOR CBORString where
  mimeRender :: Proxy CBOR -> CBORString -> ByteString
mimeRender Proxy CBOR
_ (CBORString ByteString
cs) = ByteString
cs

instance MimeUnrender CBOR CBORString where
  mimeUnrender :: Proxy CBOR -> ByteString -> Either String CBORString
mimeUnrender Proxy CBOR
_ ByteString
lbs = CBORString -> Either String CBORString
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CBORString -> Either String CBORString)
-> CBORString -> Either String CBORString
forall a b. (a -> b) -> a -> b
$ ByteString -> CBORString
CBORString ByteString
lbs

instance ToSample CBORString where
  toSamples :: Proxy CBORString -> [(Text, CBORString)]
toSamples = [(Text, CBORString)] -> Proxy CBORString -> [(Text, CBORString)]
forall a. a -> Proxy CBORString -> a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(Text, CBORString)] -> Proxy CBORString -> [(Text, CBORString)])
-> [(Text, CBORString)] -> Proxy CBORString -> [(Text, CBORString)]
forall a b. (a -> b) -> a -> b
$ CBORString -> [(Text, CBORString)]
forall a. a -> [(Text, a)]
singleSample (CBORString -> [(Text, CBORString)])
-> CBORString -> [(Text, CBORString)]
forall a b. (a -> b) -> a -> b
$ ByteString -> CBORString
CBORString ByteString
"adef"