@@ -4,30 +4,31 @@
44module Servant.Utils.Text where
55
66import Data.String.Conversions
7+import Data.Int
78import Data.Text
8−import Text.Read
9+import Data.Text.Read
10+import Data.Word
911
12+-- | For getting values from url captures, get parameters
1013class FromText a where
1114 fromText :: Text -> Maybe a
1215
16+-- | For putting values in paths, get parameters
1317class ToText a where
1418 toText :: a -> Text
1519
16−
1720instance FromText Text where
1821 fromText = Just
1922
2023instance ToText Text where
2124 toText = id
2225
23−
2426instance FromText String where
2527 fromText = Just . cs
2628
2729instance ToText String where
2830 toText = cs
2931
30−
3132instance FromText Bool where
3233 fromText "true" = Just True
3334 fromText "false" = Just False
@@ -37,9 +38,83 @@ instance ToText Bool where
3738 toText True = "true"
3839 toText False = "false"
3940
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
40100
41101instance FromText Integer where
42− fromText = readMaybe . cs
102+ fromText = runReader decimal
43103
44104instance ToText Integer where
45105 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