git.iko.soy/typeable/http-api-data
on master help sign in
Merge branch 'bounded' of github.com:singpolyma/path-pieces

Conflicts: Web/PathPieces.hs

commit5dbe5e48b2167643e408498c6890e9c0e668174b authorMichael Snoyman <michael@snoyman.com> dateWed 17 Dec 2014, 07:58 (12 years ago) parentc0d27fc9b36264
4 files changed+58−13
ChangeLog.md+3 README → README.md+2 Web/PathPieces.hs+49−12 path-pieces.cabal+4−1
ChangeLog.mdnew file+3
@@ -0,0 +1,3 @@
1+## 0.1.5
2+
3+Added more integral instances, and check bounds on them [#5](https://github.com/yesodweb/path-pieces/pull/5).
README → README.mdrenamed+2
@@ -1,3 +1,5 @@
1+## path-pieces
2+
13Defines two typeclasses used for converting Haskell data types to and from route pieces.
24Used in Yesod to automatically marshall data in the request path.
35
Web/PathPieces.hs+49−12
@@ -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
path-pieces.cabal+4−1
@@ -1,16 +1,19 @@
11name: path-pieces
2−version: 0.1.4
2+version: 0.1.5
33license: BSD3
44license-file: LICENSE
55author: Michael Snoyman <michael@snoyman.com>
66maintainer: Michael Snoyman <michael@snoyman.com>
77synopsis: Components of paths.
8+description: Hackage documentation generation is not reliable. For up to date documentation, please see: <http://www.stackage.org/package/path-pieces>.
89category: Web, Yesod
910stability: unstable
1011cabal-version: >= 1.8
1112build-type: Simple
1213extra-source-files:
1314 test/main.hs
15+ ChangeLog.md
16+ README.md
1417
1518library
1619 build-depends: base >= 4 && < 5