@@ -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
@@ -32,6 +33,16 @@ instance PathPiece L.Text where
3233 fromPathPiece = Just . L.fromChunks . return
3334 toPathPiece = S.concat . L.toChunks
3435
36+parseIntegral :: (Integral a, Bounded a, Ord a) => S.Text -> Maybe a
37+parseIntegral s = n
38+ where
39+ n = case Data.Text.Read.signed Data.Text.Read.decimal s of
40+ Right (i, "") | i <= top && i >= bot -> Just (fromInteger i)
41+ _ -> Nothing
42+ Just witness = n
43+ top = toInteger (maxBound `asTypeOf` witness)
44+ bot = toInteger (minBound `asTypeOf` witness)
45+
3546instance PathPiece Integer where
3647 fromPathPiece s =
3748 case Data.Text.Read.signed Data.Text.Read.decimal s of
@@ -40,10 +51,43 @@ instance PathPiece Integer where
4051 toPathPiece = S.pack . show
4152
4253instance PathPiece Int where
43− fromPathPiece s =
44− case Data.Text.Read.signed Data.Text.Read.decimal s of
45− Right (i, "") -> Just i
46− _ -> Nothing
54+ fromPathPiece = parseIntegral
55+ toPathPiece = S.pack . show
56+
57+instance PathPiece Int8 where
58+ fromPathPiece = parseIntegral
59+ toPathPiece = S.pack . show
60+
61+instance PathPiece Int16 where
62+ fromPathPiece = parseIntegral
63+ toPathPiece = S.pack . show
64+
65+instance PathPiece Int32 where
66+ fromPathPiece = parseIntegral
67+ toPathPiece = S.pack . show
68+
69+instance PathPiece Int64 where
70+ fromPathPiece = parseIntegral
71+ toPathPiece = S.pack . show
72+
73+instance PathPiece Word where
74+ fromPathPiece = parseIntegral
75+ toPathPiece = S.pack . show
76+
77+instance PathPiece Word8 where
78+ fromPathPiece = parseIntegral
79+ toPathPiece = S.pack . show
80+
81+instance PathPiece Word16 where
82+ fromPathPiece = parseIntegral
83+ toPathPiece = S.pack . show
84+
85+instance PathPiece Word32 where
86+ fromPathPiece = parseIntegral
87+ toPathPiece = S.pack . show
88+
89+instance PathPiece Word64 where
90+ fromPathPiece = parseIntegral
4791 toPathPiece = S.pack . show
4892
4993instance PathPiece Bool where
@@ -53,13 +97,6 @@ instance PathPiece Bool where
5397 _ -> Nothing
5498 toPathPiece = S.pack . show
5599
56−instance PathPiece Int64 where
57− fromPathPiece s =
58− case Data.Text.Read.signed Data.Text.Read.decimal s of
59− Right (i, "") -> Just i
60− _ -> Nothing
61− toPathPiece = S.pack . show
62−
63100instance PathPiece Day where
64101 fromPathPiece t =
65102 case reads $ S.unpack t of