Skip to content

Commit 82bd8cc

Browse files
committed
Abstract ByteString out of MIME rendering
1 parent 18b2ef4 commit 82bd8cc

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

servant/src/Servant/API/ContentTypes.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,14 @@ newtype AcceptHeader = AcceptHeader BS.ByteString
162162
-- > instance Accept MyContentType where
163163
-- > contentType _ = "example" // "prs.me.mine" /: ("charset", "utf-8")
164164
-- >
165-
-- > instance Show a => MimeRender MyContentType a where
165+
-- > instance MimeRender MyContentType String where
166166
-- > mimeRender _ val = pack ("This is MINE! " ++ show val)
167167
-- >
168168
-- > type MyAPI = "path" :> Get '[MyContentType] Int
169169
class Accept ctype => MimeRender ctype a where
170-
mimeRender :: Proxy ctype -> a -> ByteString
170+
type MimeRenderType a :: Type
171+
type MimeRenderType a = ByteString
172+
mimeRender :: Proxy ctype -> a -> MimeRenderType a
171173

172174
class AllMime list => AllCTRender (list :: [Type]) a where
173175
-- If the Accept header can be matched, returns (Just) a tuple of the
@@ -205,15 +207,17 @@ instance
205207
-- :}
206208
--
207209
-- >>> :{
208-
-- instance Read a => MimeUnrender MyContentType a where
210+
-- instance MimeUnrender MyContentType String where
209211
-- mimeUnrender _ bs = case BSC.take 12 bs of
210212
-- "MyContentType" -> return . read . BSC.unpack $ BSC.drop 12 bs
211213
-- _ -> Left "didn't start with the magic incantation"
212214
-- :}
213215
--
214216
-- >>> type MyAPI = "path" :> ReqBody '[MyContentType] Int :> Get '[JSON] Int
215217
class Accept ctype => MimeUnrender ctype a where
216-
mimeUnrender :: Proxy ctype -> ByteString -> Either String a
218+
type MimeUnrenderType a :: Type
219+
type MimeUnrenderType a = ByteString
220+
mimeUnrender :: Proxy ctype -> MimeUnrenderType a -> Either String a
217221
mimeUnrender p = mimeUnrenderWithType p (contentType p)
218222

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

0 commit comments

Comments
 (0)