Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions servant/src/Servant/API/ContentTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,19 @@ newtype AcceptHeader = AcceptHeader BS.ByteString
-- Example:
--
-- > data MyContentType
-- > newtype MyData = MyData String
-- >
-- > instance Accept MyContentType where
-- > contentType _ = "example" // "prs.me.mine" /: ("charset", "utf-8")
-- >
-- > instance Show a => MimeRender MyContentType a where
-- > mimeRender _ val = pack ("This is MINE! " ++ show val)
-- > instance MimeRender MyContentType MyData where
-- > mimeRender _ (MyData val) = pack ("This is MINE! " ++ val)
-- >
-- > type MyAPI = "path" :> Get '[MyContentType] Int
class Accept ctype => MimeRender ctype a where
mimeRender :: Proxy ctype -> a -> ByteString
type MimeRenderType a :: Type
type MimeRenderType a = ByteString
mimeRender :: Proxy ctype -> a -> MimeRenderType a

class AllMime list => AllCTRender (list :: [Type]) a where
-- If the Accept header can be matched, returns (Just) a tuple of the
Expand Down Expand Up @@ -197,23 +200,26 @@ instance
--
-- >>> import Network.HTTP.Media hiding (Accept)
-- >>> import qualified Data.ByteString.Lazy.Char8 as BSC
-- >>> data MyContentType = MyContentType String
-- >>> data MyContentType
-- >>> newtype MyData = MyData String
--
-- >>> :{
-- instance Accept MyContentType where
-- contentType _ = "example" // "prs.me.mine" /: ("charset", "utf-8")
-- :}
--
-- >>> :{
-- instance Read a => MimeUnrender MyContentType a where
-- instance MimeUnrender MyContentType MyData where
-- mimeUnrender _ bs = case BSC.take 12 bs of
-- "MyContentType" -> return . read . BSC.unpack $ BSC.drop 12 bs
-- "MyContentType" -> return . MyData . BSC.unpack $ BSC.drop 12 bs
-- _ -> Left "didn't start with the magic incantation"
-- :}
--
-- >>> type MyAPI = "path" :> ReqBody '[MyContentType] Int :> Get '[JSON] Int
class Accept ctype => MimeUnrender ctype a where
mimeUnrender :: Proxy ctype -> ByteString -> Either String a
type MimeUnrenderType a :: Type
type MimeUnrenderType a = ByteString
mimeUnrender :: Proxy ctype -> MimeUnrenderType a -> Either String a
mimeUnrender p = mimeUnrenderWithType p (contentType p)

-- | Variant which is given the actual 'M.MediaType' provided by the other party.
Expand Down
Loading