git.iko.soy/typeable/http-api-data
on master help sign in
Use default PathPiece instances for some basic types
3fc4ba5Nickolay Kudasov
Web/PathPieces.hs+19−80
@@ -6,13 +6,11 @@ module Web.PathPieces
6 , showToPathPiece
6 , showToPathPiece
7 ) where
7 ) where
8
8
9−import Data.Int (Int8, Int16, Int32, Int64)
9+import Data.Int
10−import Data.Word (Word, Word8, Word16, Word32, Word64)
10+import Data.Word
11import qualified Data.Text as S
11import qualified Data.Text as S
12import qualified Data.Text.Lazy as L
12import qualified Data.Text.Lazy as L
13−import qualified Data.Text.Read
14import Data.Time (Day)
13import Data.Time (Day)
15−import Control.Exception (assert)
16import Text.Read (readMaybe)
14import Text.Read (readMaybe)
17
15
18import Web.HttpApiData
16import Web.HttpApiData
@@ -26,86 +24,27 @@ class PathPiece s where
26 default toPathPiece :: ToHttpApiData s => s -> S.Text
24 default toPathPiece :: ToHttpApiData s => s -> S.Text
27 toPathPiece = toUrlPiece
25 toPathPiece = toUrlPiece
28
26
27+instance PathPiece Bool
28+instance PathPiece Double
29+instance PathPiece Float
30+instance PathPiece Int
31+instance PathPiece Int8
32+instance PathPiece Int16
33+instance PathPiece Int32
34+instance PathPiece Int64
35+instance PathPiece Word
36+instance PathPiece Word8
37+instance PathPiece Word16
38+instance PathPiece Word32
39+instance PathPiece Word64
40+instance PathPiece String
41+instance PathPiece S.Text
42+instance PathPiece L.Text
43+
29instance PathPiece () where
44instance PathPiece () where
30 fromPathPiece t = if t == "_" then Just () else Nothing
45 fromPathPiece t = if t == "_" then Just () else Nothing
31 toPathPiece () = "_"
46 toPathPiece () = "_"
32
47
33−instance PathPiece String where
34− fromPathPiece = Just . S.unpack
35− toPathPiece = S.pack
36−
37−instance PathPiece S.Text where
38− fromPathPiece = Just
39− toPathPiece = id
40−
41−instance PathPiece L.Text where
42− fromPathPiece = Just . L.fromChunks . return
43− toPathPiece = S.concat . L.toChunks
44−
45−parseIntegral :: (Integral a, Bounded a, Ord a) => S.Text -> Maybe a
46−parseIntegral s = n
47− where
48− n = case Data.Text.Read.signed Data.Text.Read.decimal s of
49− Right (i, "") | i <= top && i >= bot -> Just (fromInteger i)
50− _ -> Nothing
51− Just witness = n
52− top = toInteger (maxBound `asTypeOf` witness)
53− bot = toInteger (minBound `asTypeOf` witness)
54−
55−instance PathPiece Integer where
56− fromPathPiece s =
57− case Data.Text.Read.signed Data.Text.Read.decimal s of
58− Right (i, "") -> Just i
59− _ -> Nothing
60− toPathPiece = S.pack . show
61−
62−instance PathPiece Int where
63− fromPathPiece = parseIntegral
64− toPathPiece = S.pack . show
65−
66−instance PathPiece Int8 where
67− fromPathPiece = parseIntegral
68− toPathPiece = S.pack . show
69−
70−instance PathPiece Int16 where
71− fromPathPiece = parseIntegral
72− toPathPiece = S.pack . show
73−
74−instance PathPiece Int32 where
75− fromPathPiece = parseIntegral
76− toPathPiece = S.pack . show
77−
78−instance PathPiece Int64 where
79− fromPathPiece = parseIntegral
80− toPathPiece = S.pack . show
81−
82−instance PathPiece Word where
83− fromPathPiece = parseIntegral
84− toPathPiece = S.pack . show
85−
86−instance PathPiece Word8 where
87− fromPathPiece = parseIntegral
88− toPathPiece = S.pack . show
89−
90−instance PathPiece Word16 where
91− fromPathPiece = parseIntegral
92− toPathPiece = S.pack . show
93−
94−instance PathPiece Word32 where
95− fromPathPiece = parseIntegral
96− toPathPiece = S.pack . show
97−
98−instance PathPiece Word64 where
99− fromPathPiece = parseIntegral
100− toPathPiece = S.pack . show
101−
102−instance PathPiece Bool where
103− fromPathPiece t =
104− case filter (null . snd) $ reads $ S.unpack t of
105− (a, s):_ -> assert (null s) (Just a)
106− _ -> Nothing
107− toPathPiece = S.pack . show
108−
109instance PathPiece Day where
48instance PathPiece Day where
110 fromPathPiece t =
49 fromPathPiece t =
111 case reads $ S.unpack t of
50 case reads $ S.unpack t of