@@ -1,149 +0,0 @@
2−{-# LANGUAGE FlexibleInstances #-}
3−{-# LANGUAGE OverloadedStrings #-}
4−{-# LANGUAGE TypeSynonymInstances #-}
5−module Servant.Common.Text
10−#if !MIN_VERSION_base(4,8,0)
11−import Control.Applicative ((<$>))
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)
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
29−-- | For putting values in paths and query string parameters
30−-- Instances should obey:
31−-- > fromText (toText a) == Just a
35−instance FromText Text where
38−instance ToText Text where
41−instance FromText String where
44−instance ToText String where
48−-- >>> fromText ("true"::Text) :: Maybe Bool
50−-- >>> fromText ("false"::Text) :: Maybe Bool
52−-- >>> fromText ("anything else"::Text) :: Maybe Bool
54−instance FromText Bool where
55− fromText "true" = Just True
56− fromText "false" = Just False
64−instance ToText Bool where
66− toText False = "false"
68−instance FromText Int where
69− fromText = runReader (signed decimal)
71−instance ToText Int where
74−instance FromText Int8 where
75− fromText = runReader (signed decimal)
77−instance ToText Int8 where
80−instance FromText Int16 where
81− fromText = runReader (signed decimal)
83−instance ToText Int16 where
86−instance FromText Int32 where
87− fromText = runReader (signed decimal)
89−instance ToText Int32 where
92−instance FromText Int64 where
93− fromText = runReader (signed decimal)
95−instance ToText Int64 where
98−instance FromText Word where
99− fromText = runReader decimal
101−instance ToText Word where
104−instance FromText Word8 where
105− fromText = runReader decimal
107−instance ToText Word8 where
110−instance FromText Word16 where
111− fromText = runReader decimal
113−instance ToText Word16 where
116−instance FromText Word32 where
117− fromText = runReader decimal
119−instance ToText Word32 where
122−instance FromText Word64 where
123− fromText = runReader decimal
125−instance ToText Word64 where
128−instance FromText Integer where
129− fromText = runReader (signed decimal)
131−instance ToText Integer where
134−instance FromText Double where
135− fromText x = fromRational <$> runReader rational x
137−instance ToText Double where
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
145−instance ToText Float where
148−runReader :: Reader a -> Text -> Maybe a
149−runReader reader t = either (const Nothing) (Just . fst) $ reader t