@@ -1,149 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE FlexibleInstances #-}
3−{-# LANGUAGE OverloadedStrings #-}
4−{-# LANGUAGE TypeSynonymInstances #-}
5−module Servant.Common.Text
6− ( FromText(..)
7− , ToText(..)
8− ) where
9−
10−#if !MIN_VERSION_base(4,8,0)
11−import Control.Applicative ((<$>))
12−#endif
13−import Data.Int (Int16, Int32, Int64, Int8)
14−import Data.String.Conversions (cs)
15−import Data.Text (Text)
16−import Data.Text.Read (Reader, decimal, rational, signed)
17−import Data.Word (Word16, Word32, Word64, Word8
18−#if !MIN_VERSION_base(4,8,0)
19− , Word
20−#endif
21− )
22−
23−-- | For getting values from url captures and query string parameters
24−-- Instances should obey:
25−-- > fromText (toText a) == Just a
26−class FromText a where
27− fromText :: Text -> Maybe a
28−
29−-- | For putting values in paths and query string parameters
30−-- Instances should obey:
31−-- > fromText (toText a) == Just a
32−class ToText a where
33− toText :: a -> Text
34−
35−instance FromText Text where
36− fromText = Just
37−
38−instance ToText Text where
39− toText = id
40−
41−instance FromText String where
42− fromText = Just . cs
43−
44−instance ToText String where
45− toText = cs
46−
47−-- |
48−-- >>> fromText ("true"::Text) :: Maybe Bool
49−-- Just True
50−-- >>> fromText ("false"::Text) :: Maybe Bool
51−-- Just False
52−-- >>> fromText ("anything else"::Text) :: Maybe Bool
53−-- Nothing
54−instance FromText Bool where
55− fromText "true" = Just True
56− fromText "false" = Just False
57− fromText _ = Nothing
58−
59−-- |
60−-- >>> toText True
61−-- "true"
62−-- >>> toText False
63−-- "false"
64−instance ToText Bool where
65− toText True = "true"
66− toText False = "false"
67−
68−instance FromText Int where
69− fromText = runReader (signed decimal)
70−
71−instance ToText Int where
72− toText = cs . show
73−
74−instance FromText Int8 where
75− fromText = runReader (signed decimal)
76−
77−instance ToText Int8 where
78− toText = cs . show
79−
80−instance FromText Int16 where
81− fromText = runReader (signed decimal)
82−
83−instance ToText Int16 where
84− toText = cs . show
85−
86−instance FromText Int32 where
87− fromText = runReader (signed decimal)
88−
89−instance ToText Int32 where
90− toText = cs . show
91−
92−instance FromText Int64 where
93− fromText = runReader (signed decimal)
94−
95−instance ToText Int64 where
96− toText = cs . show
97−
98−instance FromText Word where
99− fromText = runReader decimal
100−
101−instance ToText Word where
102− toText = cs . show
103−
104−instance FromText Word8 where
105− fromText = runReader decimal
106−
107−instance ToText Word8 where
108− toText = cs . show
109−
110−instance FromText Word16 where
111− fromText = runReader decimal
112−
113−instance ToText Word16 where
114− toText = cs . show
115−
116−instance FromText Word32 where
117− fromText = runReader decimal
118−
119−instance ToText Word32 where
120− toText = cs . show
121−
122−instance FromText Word64 where
123− fromText = runReader decimal
124−
125−instance ToText Word64 where
126− toText = cs . show
127−
128−instance FromText Integer where
129− fromText = runReader (signed decimal)
130−
131−instance ToText Integer where
132− toText = cs . show
133−
134−instance FromText Double where
135− fromText x = fromRational <$> runReader rational x
136−
137−instance ToText Double where
138− toText = cs . show
139−
140−instance FromText Float where
141− -- Double is more practically accurate due to weird rounding when using
142− -- rational. We convert to double and then convert to Float.
143− fromText x = fromRational <$> runReader rational x
144−
145−instance ToText Float where
146− toText = cs . show
147−
148−runReader :: Reader a -> Text -> Maybe a
149−runReader reader t = either (const Nothing) (Just . fst) $ reader t