@@ -6,13 +6,11 @@ module Web.PathPieces
66 , showToPathPiece
77 ) where
88
9−import Data.Int (Int8, Int16, Int32, Int64)
10−import Data.Word (Word, Word8, Word16, Word32, Word64)
9+import Data.Int
10+import Data.Word
1111import qualified Data.Text as S
1212import qualified Data.Text.Lazy as L
13−import qualified Data.Text.Read
1413import Data.Time (Day)
15−import Control.Exception (assert)
1614import Text.Read (readMaybe)
1715
1816import Web.HttpApiData
@@ -26,86 +24,27 @@ class PathPiece s where
2624 default toPathPiece :: ToHttpApiData s => s -> S.Text
2725 toPathPiece = toUrlPiece
2826
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+
2944instance PathPiece () where
3045 fromPathPiece t = if t == "_" then Just () else Nothing
3146 toPathPiece () = "_"
3247
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−
10948instance PathPiece Day where
11049 fromPathPiece t =
11150 case reads $ S.unpack t of