git.iko.soy/typeable/http-api-data
on master help sign in
Merge branch 'bounded' of github.com:singpolyma/path-pieces
5dbe5e4Michael Snoyman
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+
1Defines two typeclasses used for converting Haskell data types to and from route pieces.
3Defines two typeclasses used for converting Haskell data types to and from route pieces.
2Used in Yesod to automatically marshall data in the request path.
4Used in Yesod to automatically marshall data in the request path.
3
5
Web/PathPieces.hs+49−12
@@ -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
@@ -32,6 +33,16 @@ instance PathPiece L.Text where
32 fromPathPiece = Just . L.fromChunks . return
33 fromPathPiece = Just . L.fromChunks . return
33 toPathPiece = S.concat . L.toChunks
34 toPathPiece = S.concat . L.toChunks
34
35
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+
35instance PathPiece Integer where
46instance PathPiece Integer where
36 fromPathPiece s =
47 fromPathPiece s =
37 case Data.Text.Read.signed Data.Text.Read.decimal s of
48 case Data.Text.Read.signed Data.Text.Read.decimal s of
@@ -40,10 +51,43 @@ instance PathPiece Integer where
40 toPathPiece = S.pack . show
51 toPathPiece = S.pack . show
41
52
42instance PathPiece Int where
53instance PathPiece Int where
43− fromPathPiece s =
54+ fromPathPiece = parseIntegral
44− case Data.Text.Read.signed Data.Text.Read.decimal s of
55+ toPathPiece = S.pack . show
45− Right (i, "") -> Just i
56+
46− _ -> Nothing
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
47 toPathPiece = S.pack . show
91 toPathPiece = S.pack . show
48
92
49instance PathPiece Bool where
93instance PathPiece Bool where
@@ -53,13 +97,6 @@ instance PathPiece Bool where
53 _ -> Nothing
97 _ -> Nothing
54 toPathPiece = S.pack . show
98 toPathPiece = S.pack . show
55
99
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−
63instance PathPiece Day where
100instance PathPiece Day where
64 fromPathPiece t =
101 fromPathPiece t =
65 case reads $ S.unpack t of
102 case reads $ S.unpack t of
path-pieces.cabal+4−1
@@ -1,16 +1,19 @@
1name: path-pieces
1name: path-pieces
2−version: 0.1.4
2+version: 0.1.5
3license: BSD3
3license: BSD3
4license-file: LICENSE
4license-file: LICENSE
5author: Michael Snoyman <michael@snoyman.com>
5author: Michael Snoyman <michael@snoyman.com>
6maintainer: Michael Snoyman <michael@snoyman.com>
6maintainer: Michael Snoyman <michael@snoyman.com>
7synopsis: Components of paths.
7synopsis: 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>.
8category: Web, Yesod
9category: Web, Yesod
9stability: unstable
10stability: unstable
10cabal-version: >= 1.8
11cabal-version: >= 1.8
11build-type: Simple
12build-type: Simple
12extra-source-files:
13extra-source-files:
13 test/main.hs
14 test/main.hs
15+ ChangeLog.md
16+ README.md
14
17
15library
18library
16 build-depends: base >= 4 && < 5
19 build-depends: base >= 4 && < 5