-- | CBOR Servant support and wrapper type

module Blockfrost.Types.Shared.CBOR
  where

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

data CBOR

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

instance Accept CBOR where
  contentType :: Proxy CBOR -> MediaType
contentType = 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 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ ByteString -> CBORString
CBORString ByteString
lbs

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