{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Servant.API.ContentTypes.Cereal where
import Data.Proxy (Proxy(..))
import Network.HTTP.Media ((//))
import qualified Data.List.NonEmpty as NonEmpty
import Servant.API.ContentTypes
import Data.Serialize
data CerealFmt
instance Accept CerealFmt where
contentTypes :: Proxy CerealFmt -> NonEmpty MediaType
contentTypes Proxy CerealFmt
Proxy = forall a. [a] -> NonEmpty a
NonEmpty.fromList
[ ByteString
"application" ByteString -> ByteString -> MediaType
// ByteString
"x-hackage-cereal"
, ByteString
"application" ByteString -> ByteString -> MediaType
// ByteString
"vnd.hackage.cereal"
]
instance Serialize a => MimeRender CerealFmt a where
mimeRender :: Proxy CerealFmt -> a -> ByteString
mimeRender Proxy CerealFmt
Proxy = forall a. Serialize a => a -> ByteString
encodeLazy
instance Serialize a => MimeUnrender CerealFmt a where
mimeUnrender :: Proxy CerealFmt -> ByteString -> Either String a
mimeUnrender Proxy CerealFmt
Proxy = forall {a} {b} {b}. (a -> b) -> Either a b -> Either b b
mapLeft (String
"Data.Serialize.decodeLazy: " forall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Serialize a => ByteString -> Either String a
decodeLazy
where
mapLeft :: (a -> b) -> Either a b -> Either b b
mapLeft a -> b
f = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> Either a b
Left forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f) forall a b. b -> Either a b
Right