@@ -9,7 +9,8 @@ module Web.PathPieces
99 , fromMultiPiece
1010 ) where
1111
12−import Data.Int (Int64)
12+import Data.Int (Int8, Int16, Int32, Int64)
13+import Data.Word (Word, Word8, Word16, Word32, Word64)
1314import qualified Data.Text as S
1415import qualified Data.Text.Lazy as L
1516import qualified Data.Text.Read
@@ -31,25 +32,60 @@ instance PathPiece L.Text where
3132 fromPathPiece = Just . L.fromChunks . return
3233 toPathPiece = S.concat . L.toChunks
3334
35+parseIntegral :: (Integral a, Bounded a, Ord a) => S.Text -> Maybe a
36+parseIntegral s = n
37+ where
38+ n = case Data.Text.Read.signed Data.Text.Read.decimal s of
39+ Right (i, _) | i <= top && i >= bot -> Just (fromInteger i)
40+ _ -> Nothing
41+ Just witness = n
42+ top = toInteger (maxBound `asTypeOf` witness)
43+ bot = toInteger (minBound `asTypeOf` witness)
44+
3445instance PathPiece Integer where
35− fromPathPiece s =
36− case Data.Text.Read.signed Data.Text.Read.decimal s of
37− Right (i, _) -> Just i
38− Left _ -> Nothing
46+ fromPathPiece s = case Data.Text.Read.signed Data.Text.Read.decimal s of
47+ Right (i, _) -> Just i
48+ Left _ -> Nothing
3949 toPathPiece = S.pack . show
4050
4151instance PathPiece Int where
42− fromPathPiece s =
43− case Data.Text.Read.signed Data.Text.Read.decimal s of
44− Right (i, _) -> Just i
45− Left _ -> Nothing
52+ fromPathPiece = parseIntegral
53+ toPathPiece = S.pack . show
54+
55+instance PathPiece Int8 where
56+ fromPathPiece = parseIntegral
57+ toPathPiece = S.pack . show
58+
59+instance PathPiece Int16 where
60+ fromPathPiece = parseIntegral
61+ toPathPiece = S.pack . show
62+
63+instance PathPiece Int32 where
64+ fromPathPiece = parseIntegral
4665 toPathPiece = S.pack . show
4766
4867instance PathPiece Int64 where
49− fromPathPiece s =
50− case Data.Text.Read.signed Data.Text.Read.decimal s of
51− Right (i, _) -> Just i
52− Left _ -> Nothing
68+ fromPathPiece = parseIntegral
69+ toPathPiece = S.pack . show
70+
71+instance PathPiece Word where
72+ fromPathPiece = parseIntegral
73+ toPathPiece = S.pack . show
74+
75+instance PathPiece Word8 where
76+ fromPathPiece = parseIntegral
77+ toPathPiece = S.pack . show
78+
79+instance PathPiece Word16 where
80+ fromPathPiece = parseIntegral
81+ toPathPiece = S.pack . show
82+
83+instance PathPiece Word32 where
84+ fromPathPiece = parseIntegral
85+ toPathPiece = S.pack . show
86+
87+instance PathPiece Word64 where
88+ fromPathPiece = parseIntegral
5389 toPathPiece = S.pack . show
5490
5591instance PathPiece Day where