]> Git — Sourcephile - haskell/symantic-http.git/blob - Language/Symantic/HTTP/Media.hs
init
[haskell/symantic-http.git] / Language / Symantic / HTTP / Media.hs
1 {-# LANGUAGE OverloadedStrings #-}
2 module Language.Symantic.HTTP.Media where
3
4 import Data.Function (($), (.), id)
5 import Data.Proxy (Proxy(..))
6 import Data.String (String)
7 import qualified Data.ByteString as BS
8 import qualified Data.ByteString.Lazy as BSL
9 import qualified Data.ByteString.Lazy.Char8 as BSLC
10 import qualified Data.Text as T
11 import qualified Data.Text.Encoding as T
12 import qualified Data.Text.Lazy as TL
13 import qualified Data.Text.Lazy.Encoding as TL
14 import qualified Network.HTTP.Media as Media
15
16 -- * Class 'MediaTypeable'
17 class MediaTypeable mt where
18 mediaType :: Proxy mt -> MediaType
19 mediaTypes :: Proxy mt -> [MediaType]
20 mediaTypes mt = [mediaType mt]
21 type MediaType = Media.MediaType
22
23 charsetUTF8 :: MediaType -> MediaType
24 charsetUTF8 = (Media./: ("charset", "utf-8"))
25
26 anyMediaType :: MediaType
27 anyMediaType = "*/*"
28
29 -- ** Type 'JSON'
30 data JSON
31 json :: Proxy JSON
32 json = Proxy
33 instance MediaTypeable JSON where
34 mediaType _mt = charsetUTF8 $ "application"Media.//"json"
35 mediaTypes mt = [mediaType mt, "application"Media.//"json"]
36
37 -- ** Type 'HTML'
38 data HTML
39 html :: Proxy HTML
40 html = Proxy
41 instance MediaTypeable HTML where
42 mediaType _mt = charsetUTF8 $ "text"Media.//"html"
43 mediaTypes mt = [mediaType mt, "text"Media.//"html"]
44
45 -- ** Type 'FormUrlEncoded'
46 data FormUrlEncoded
47 formUrlEncoded :: Proxy FormUrlEncoded
48 formUrlEncoded = Proxy
49 instance MediaTypeable FormUrlEncoded where
50 mediaType _mt = "application"Media.//"x-www-form-urlencoded"
51
52 -- ** Type 'OctetStream'
53 data OctetStream
54 octetStream :: Proxy OctetStream
55 octetStream = Proxy
56 instance MediaTypeable OctetStream where
57 mediaType _mt = "application"Media.//"octet-stream"
58
59 -- ** Type 'PlainText'
60 data PlainText
61 plainText :: Proxy PlainText
62 plainText = Proxy
63 instance MediaTypeable PlainText where
64 mediaType _mt = charsetUTF8 $ "text"Media.//"plain"
65 instance MediaTypeable () where
66 mediaType _mt = mediaType (Proxy::Proxy PlainText)
67
68 -- * Class 'ToMediaType'
69 class MediaTypeable mt => ToMediaType mt a where
70 toMediaType :: Proxy mt -> a -> BSL.ByteString
71 instance ToMediaType PlainText () where
72 toMediaType _mt () = BSL.empty
73 instance ToMediaType PlainText T.Text where
74 toMediaType _mt = BSL.fromStrict . T.encodeUtf8
75 instance ToMediaType PlainText TL.Text where
76 toMediaType _mt = TL.encodeUtf8
77 instance ToMediaType PlainText String where
78 toMediaType _mt = BSLC.pack
79 instance ToMediaType OctetStream BS.ByteString where
80 toMediaType _mt = BSL.fromStrict
81 instance ToMediaType OctetStream BSL.ByteString where
82 toMediaType _mt = id
83 {- TODO: uncomment when Web is imported
84 instance ToMediaType FormUrlEncoded a where
85 toMediaType _mt = Web.urlEncodeAsForm
86 -}