@@ -4,30 +4,31 @@
4module Servant.Utils.Text where
4module Servant.Utils.Text where
6import Data.String.Conversions
6import Data.String.Conversions
7import Data.Text
8import Data.Text
8−import Text.Read
9+import Data.Text.Read
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
16+-- | For putting values in paths, get parameters
13class ToText a where
17class ToText a where
14 toText :: a -> Text
18 toText :: a -> Text
17instance FromText Text where
20instance FromText Text where
18 fromText = Just
21 fromText = Just
20instance ToText Text where
23instance ToText Text where
21 toText = id
24 toText = id
24instance FromText String where
26instance FromText String where
25 fromText = Just . cs
27 fromText = Just . cs
27instance ToText String where
29instance ToText String where
28 toText = cs
30 toText = cs
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"
41+instance FromText Int where
42+ fromText = runReader (signed decimal)
44+instance ToText Int where
47+instance FromText Int8 where
48+ fromText = runReader (signed decimal)
50+instance ToText Int8 where
53+instance FromText Int16 where
54+ fromText = runReader (signed decimal)
56+instance ToText Int16 where
59+instance FromText Int32 where
60+ fromText = runReader (signed decimal)
62+instance ToText Int32 where
65+instance FromText Int64 where
66+ fromText = runReader (signed decimal)
68+instance ToText Int64 where
71+instance FromText Word where
72+ fromText = runReader decimal
74+instance ToText Word where
77+instance FromText Word8 where
78+ fromText = runReader decimal
80+instance ToText Word8 where
83+instance FromText Word16 where
84+ fromText = runReader decimal
86+instance ToText Word16 where
89+instance FromText Word32 where
90+ fromText = runReader decimal
92+instance ToText Word32 where
95+instance FromText Word64 where
96+ fromText = runReader decimal
98+instance ToText Word64 where
41instance FromText Integer where
101instance FromText Integer where
42− fromText = readMaybe . cs
102+ fromText = runReader decimal
44instance ToText Integer where
104instance ToText Integer where
45 toText = cs . show
105 toText = cs . show
107+instance FromText Double where
108+ fromText = runReader rational
110+instance ToText Double where
113+instance FromText Float where
114+ fromText = runReader rational
116+instance ToText Float where
119+runReader :: Reader a -> Text -> Maybe a
120+runReader reader t = either (const Nothing) (Just . fst) $ reader t