@@ -6,13 +6,11 @@ module Web.PathPieces
6 , showToPathPiece
6 , showToPathPiece
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)
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
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
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 () = "_"
33−instance PathPiece String where
34− fromPathPiece = Just . S.unpack
37−instance PathPiece S.Text where
41−instance PathPiece L.Text where
42− fromPathPiece = Just . L.fromChunks . return
43− toPathPiece = S.concat . L.toChunks
45−parseIntegral :: (Integral a, Bounded a, Ord a) => S.Text -> Maybe a
48− n = case Data.Text.Read.signed Data.Text.Read.decimal s of
49− Right (i, "") | i <= top && i >= bot -> Just (fromInteger i)
52− top = toInteger (maxBound `asTypeOf` witness)
53− bot = toInteger (minBound `asTypeOf` witness)
55−instance PathPiece Integer where
57− case Data.Text.Read.signed Data.Text.Read.decimal s of
58− Right (i, "") -> Just i
60− toPathPiece = S.pack . show
62−instance PathPiece Int where
63− fromPathPiece = parseIntegral
64− toPathPiece = S.pack . show
66−instance PathPiece Int8 where
67− fromPathPiece = parseIntegral
68− toPathPiece = S.pack . show
70−instance PathPiece Int16 where
71− fromPathPiece = parseIntegral
72− toPathPiece = S.pack . show
74−instance PathPiece Int32 where
75− fromPathPiece = parseIntegral
76− toPathPiece = S.pack . show
78−instance PathPiece Int64 where
79− fromPathPiece = parseIntegral
80− toPathPiece = S.pack . show
82−instance PathPiece Word where
83− fromPathPiece = parseIntegral
84− toPathPiece = S.pack . show
86−instance PathPiece Word8 where
87− fromPathPiece = parseIntegral
88− toPathPiece = S.pack . show
90−instance PathPiece Word16 where
91− fromPathPiece = parseIntegral
92− toPathPiece = S.pack . show
94−instance PathPiece Word32 where
95− fromPathPiece = parseIntegral
96− toPathPiece = S.pack . show
98−instance PathPiece Word64 where
99− fromPathPiece = parseIntegral
100− toPathPiece = S.pack . show
102−instance PathPiece Bool where
104− case filter (null . snd) $ reads $ S.unpack t of
105− (a, s):_ -> assert (null s) (Just a)
107− toPathPiece = S.pack . show
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