git.iko.soy/typeable/servant
on master help sign in
add a lot of FromText/ToText instances
c480770Alp Mestanogullari
src/Servant/Utils/Text.hs+80−5
@@ -4,30 +4,31 @@
4module Servant.Utils.Text where
4module Servant.Utils.Text where
5
5
6import Data.String.Conversions
6import Data.String.Conversions
7+import Data.Int
7import Data.Text
8import Data.Text
8−import Text.Read
9+import Data.Text.Read
10+import Data.Word
9
11
12+-- | For getting values from url captures, get parameters
10class FromText a where
13class FromText a where
11 fromText :: Text -> Maybe a
14 fromText :: Text -> Maybe a
12
15
16+-- | For putting values in paths, get parameters
13class ToText a where
17class ToText a where
14 toText :: a -> Text
18 toText :: a -> Text
15
19
16−
17instance FromText Text where
20instance FromText Text where
18 fromText = Just
21 fromText = Just
19
22
20instance ToText Text where
23instance ToText Text where
21 toText = id
24 toText = id
22
25
23−
24instance FromText String where
26instance FromText String where
25 fromText = Just . cs
27 fromText = Just . cs
26
28
27instance ToText String where
29instance ToText String where
28 toText = cs
30 toText = cs
29
31
30−
31instance FromText Bool where
32instance FromText Bool where
32 fromText "true" = Just True
33 fromText "true" = Just True
33 fromText "false" = Just False
34 fromText "false" = Just False
@@ -37,9 +38,83 @@ instance ToText Bool where
37 toText True = "true"
38 toText True = "true"
38 toText False = "false"
39 toText False = "false"
39
40
41+instance FromText Int where
42+ fromText = runReader (signed decimal)
43+
44+instance ToText Int where
45+ toText = cs . show
46+
47+instance FromText Int8 where
48+ fromText = runReader (signed decimal)
49+
50+instance ToText Int8 where
51+ toText = cs . show
52+
53+instance FromText Int16 where
54+ fromText = runReader (signed decimal)
55+
56+instance ToText Int16 where
57+ toText = cs . show
58+
59+instance FromText Int32 where
60+ fromText = runReader (signed decimal)
61+
62+instance ToText Int32 where
63+ toText = cs . show
64+
65+instance FromText Int64 where
66+ fromText = runReader (signed decimal)
67+
68+instance ToText Int64 where
69+ toText = cs . show
70+
71+instance FromText Word where
72+ fromText = runReader decimal
73+
74+instance ToText Word where
75+ toText = cs . show
76+
77+instance FromText Word8 where
78+ fromText = runReader decimal
79+
80+instance ToText Word8 where
81+ toText = cs . show
82+
83+instance FromText Word16 where
84+ fromText = runReader decimal
85+
86+instance ToText Word16 where
87+ toText = cs . show
88+
89+instance FromText Word32 where
90+ fromText = runReader decimal
91+
92+instance ToText Word32 where
93+ toText = cs . show
94+
95+instance FromText Word64 where
96+ fromText = runReader decimal
97+
98+instance ToText Word64 where
99+ toText = cs . show
40
100
41instance FromText Integer where
101instance FromText Integer where
42− fromText = readMaybe . cs
102+ fromText = runReader decimal
43
103
44instance ToText Integer where
104instance ToText Integer where
45 toText = cs . show
105 toText = cs . show
106+
107+instance FromText Double where
108+ fromText = runReader rational
109+
110+instance ToText Double where
111+ toText = cs . show
112+
113+instance FromText Float where
114+ fromText = runReader rational
115+
116+instance ToText Float where
117+ toText = cs . show
118+
119+runReader :: Reader a -> Text -> Maybe a
120+runReader reader t = either (const Nothing) (Just . fst) $ reader t