git.iko.soy/typeable/http-api-data
on master help sign in
Force bounded integral types into their sane range
9b36264Stephen Paul Weber
Web/PathPieces.hs+49−13
@@ -9,7 +9,8 @@ module Web.PathPieces
9 , fromMultiPiece
9 , fromMultiPiece
10 ) where
10 ) where
11
11
12−import Data.Int (Int64)
12+import Data.Int (Int8, Int16, Int32, Int64)
13+import Data.Word (Word, Word8, Word16, Word32, Word64)
13import qualified Data.Text as S
14import qualified Data.Text as S
14import qualified Data.Text.Lazy as L
15import qualified Data.Text.Lazy as L
15import qualified Data.Text.Read
16import qualified Data.Text.Read
@@ -31,25 +32,60 @@ instance PathPiece L.Text where
31 fromPathPiece = Just . L.fromChunks . return
32 fromPathPiece = Just . L.fromChunks . return
32 toPathPiece = S.concat . L.toChunks
33 toPathPiece = S.concat . L.toChunks
33
34
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+
34instance PathPiece Integer where
45instance PathPiece Integer where
35− fromPathPiece s =
36− case Data.Text.Read.signed Data.Text.Read.decimal s of
46+ fromPathPiece s = case Data.Text.Read.signed Data.Text.Read.decimal s of
37− Right (i, _) -> Just i
47+ Right (i, _) -> Just i
38− Left _ -> Nothing
48+ Left _ -> Nothing
39 toPathPiece = S.pack . show
49 toPathPiece = S.pack . show
40
50
41instance PathPiece Int where
51instance PathPiece Int where
42− fromPathPiece s =
52+ fromPathPiece = parseIntegral
43− case Data.Text.Read.signed Data.Text.Read.decimal s of
53+ toPathPiece = S.pack . show
44− Right (i, _) -> Just i
54+
45− Left _ -> Nothing
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
46 toPathPiece = S.pack . show
65 toPathPiece = S.pack . show
47
66
48instance PathPiece Int64 where
67instance PathPiece Int64 where
49− fromPathPiece s =
68+ fromPathPiece = parseIntegral
50− case Data.Text.Read.signed Data.Text.Read.decimal s of
69+ toPathPiece = S.pack . show
51− Right (i, _) -> Just i
70+
52− Left _ -> Nothing
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
53 toPathPiece = S.pack . show
89 toPathPiece = S.pack . show
54
90
55instance PathPiece Day where
91instance PathPiece Day where