@@ -1,55 +1,12 @@
1−{-# LANGUAGE DataKinds #-}
21{-# LANGUAGE DeriveDataTypeable #-}
3−{-# LANGUAGE FlexibleInstances #-}
4−{-# LANGUAGE MultiParamTypeClasses #-}
5−{-# LANGUAGE OverloadedStrings #-}
6−{-# LANGUAGE PolyKinds #-}
7−{-# LANGUAGE ScopedTypeVariables #-}
8−{-# LANGUAGE TypeOperators #-}
92module Servant.API.ContentTypes where
103
11−import Data.ByteString (ByteString)
12−import Data.Proxy (Proxy(..))
13−import Data.Typeable (Typeable)
4+import Data.Typeable
5+
146
157data XML deriving Typeable
168data HTML deriving Typeable
179data JSON deriving Typeable
18−
19−type ContentTypeBS = ByteString
20−
21−class Accept ctype where
22− isContentType :: Proxy ctype -> ByteString -> Bool
23− contentType :: Proxy ctype -> ContentTypeBS
24− isContentType p bs = bs == contentType p
25−
26−instance Accept HTML where
27− contentType _ = "text/html"
28−
29−instance Accept JSON where
30− contentType _ = "application/json"
31−
32−instance Accept XML where
33− contentType _ = "application/xml"
34−
35−-- | Instantiate this class to register a way of serializing a type based
36−-- on the @Accept@ header.
37−class Accept ctype => MimeRender ctype a where
38− toByteString :: Proxy ctype -> a -> ByteString
39−
40−
41−class AllCTRender list a where
42− handleAcceptH :: Proxy list -> ContentTypeBS -> a -> (ByteString, ContentTypeBS)
43−
44−instance MimeRender ctyp a => AllCTRender '[ctyp] a where
45− handleAcceptH _ accept val = (toByteString pctyp val, accept)
46− where pctyp = Proxy :: Proxy ctyp
47−
48−instance ( MimeRender ctyp a
49− , AllCTRender ctyps a
50− ) => AllCTRender (ctyp ': ctyps) a where
51− handleAcceptH _ accept val
52− | isContentType pctyp accept = (toByteString pctyp val, accept)
53− | otherwise = handleAcceptH pctyps accept val
54− where pctyp = Proxy :: Proxy ctyp
55− pctyps = Proxy :: Proxy ctyps
10+data JavaScript deriving Typeable
11+data CSS deriving Typeable
12+data PlainText deriving Typeable