@@ -1,162 +1,9 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE DefaultSignatures #-}
3−{-# LANGUAGE FlexibleInstances #-}
4−{-# LANGUAGE OverloadedStrings #-}
5−{-# LANGUAGE Rank2Types #-}
6−{-# LANGUAGE ScopedTypeVariables #-}
7−{-# LANGUAGE TypeSynonymInstances #-}
81-- |
92-- Convert Haskell values to and from route pieces.
103module Web.PathPieces (
114 PathPiece (..),
125 PathMultiPiece (..),
13− readFromPathPiece,
14− showToPathPiece,
15− WrappedPathPiece(..),
166) where
177
18−import Data.Monoid
19−import Data.Int
20−import Data.Word
21−import qualified Data.Text as S
22−import qualified Data.Text.Lazy as L
23−import Data.Version (Version)
24−import Data.Time (Day)
25−import Text.Read (readMaybe)
26−
27−import Unsafe.Coerce
28−
29−#if MIN_VERSION_base(4,8,0)
30−import Data.Void (Void)
31−#endif
32−
33−import Web.HttpApiData
34−import Web.HttpApiData.Internal (parseMaybeTextData)
35−
36−-- | Convert Haskell values to and from route piece.
37−class PathPiece s where
38− -- | Convert from route piece.
39− fromPathPiece :: S.Text -> Maybe s
40− default fromPathPiece :: FromHttpApiData s => S.Text -> Maybe s
41− fromPathPiece = either (const Nothing) Just . parseUrlPiece
42−
43− -- | Convert to route piece.
44− toPathPiece :: s -> S.Text
45− default toPathPiece :: ToHttpApiData s => s -> S.Text
46− toPathPiece = toUrlPiece
47−
48−instance PathPiece ()
49−instance PathPiece Char
50−instance PathPiece Bool
51−instance PathPiece Ordering
52−instance PathPiece Double
53−instance PathPiece Float
54−instance PathPiece Int
55−instance PathPiece Int8
56−instance PathPiece Int16
57−instance PathPiece Int32
58−instance PathPiece Int64
59−instance PathPiece Integer
60−instance PathPiece Word
61−instance PathPiece Word8
62−instance PathPiece Word16
63−instance PathPiece Word32
64−instance PathPiece Word64
65−instance PathPiece String
66−instance PathPiece S.Text
67−instance PathPiece L.Text
68−instance PathPiece Day
69−instance PathPiece Version
70−
71−#if MIN_VERSION_base(4,8,0)
72−instance PathPiece Void
73−#endif
74−
75−instance PathPiece All
76−instance PathPiece Any
77−
78−instance PathPiece a => PathPiece (Dual a) where
79− toPathPiece = toPathPiece1
80− fromPathPiece = fromPathPiece1
81−
82−instance PathPiece a => PathPiece (Sum a) where
83− toPathPiece = toPathPiece1
84− fromPathPiece = fromPathPiece1
85−
86−instance PathPiece a => PathPiece (Product a) where
87− toPathPiece = toPathPiece1
88− fromPathPiece = fromPathPiece1
89−
90−instance PathPiece a => PathPiece (First a) where
91− toPathPiece = toPathPiece1
92− fromPathPiece = fromPathPiece1
93−
94−instance PathPiece a => PathPiece (Last a) where
95− toPathPiece = toPathPiece1
96− fromPathPiece = fromPathPiece1
97−
98−instance PathPiece a => PathPiece (Maybe a) where
99− toPathPiece = toPathPiece1
100− fromPathPiece = fromPathPiece1
101−
102−instance (PathPiece a, PathPiece b) => PathPiece (Either a b) where
103− toPathPiece = toPathPiece2
104− fromPathPiece = fromPathPiece2
105−
106−-- | Wrapped @'PathPiece'@ value.
107−newtype WrappedPathPiece a = WrappedPathPiece { unwrapPathPiece :: a }
108−
109−instance PathPiece a => ToHttpApiData (WrappedPathPiece a) where
110− toUrlPiece = toPathPiece . unwrapPathPiece
111−
112−instance PathPiece a => FromHttpApiData (WrappedPathPiece a) where
113− parseUrlPiece = fmap WrappedPathPiece . parseMaybeTextData fromPathPiece
114−
115−-- | Convert value to route piece using @'PathPiece'@ instance for its parameter.
116−toPathPiece1 :: forall f a. (PathPiece a, ToHttpApiData (f (WrappedPathPiece a))) => f a -> S.Text
117−toPathPiece1 = toUrlPiece . (unsafeCoerce :: f a -> f (WrappedPathPiece a))
118−
119−-- | Parse value from route piece using @'PathPiece'@ instance for its parameter.
120−fromPathPiece1 :: forall f a. (PathPiece a, FromHttpApiData (f (WrappedPathPiece a))) => S.Text -> Maybe (f a)
121−fromPathPiece1 = either (const Nothing) (Just . (unsafeCoerce :: f (WrappedPathPiece a) -> f a)) . parseUrlPiece
122−
123−-- | Convert value to route piece using @'PathPiece'@ instance for its parameters.
124−toPathPiece2 :: forall f a b. (PathPiece a, PathPiece b, ToHttpApiData (f (WrappedPathPiece a) (WrappedPathPiece b))) => f a b -> S.Text
125−toPathPiece2 = toUrlPiece . (unsafeCoerce :: f a b -> f (WrappedPathPiece a) (WrappedPathPiece b))
126−
127−-- | Parse value from route piece using @'PathPiece'@ instance for its parameters.
128−fromPathPiece2 :: forall f a b. (PathPiece a, PathPiece b, FromHttpApiData (f (WrappedPathPiece a) (WrappedPathPiece b))) => S.Text -> Maybe (f a b)
129−fromPathPiece2 = either (const Nothing) (Just . (unsafeCoerce :: f (WrappedPathPiece a) (WrappedPathPiece b) -> f a b)) . parseUrlPiece
130−
131−-- | Convert Haskell values to and from sequence of route pieces.
132−class PathMultiPiece s where
133− -- | Convert from sequence of route pieces.
134− fromPathMultiPiece :: [S.Text] -> Maybe s
135− -- | Convert to sequence of route pieces.
136− toPathMultiPiece :: s -> [S.Text]
137−
138−instance PathPiece a => PathMultiPiece [a] where
139− fromPathMultiPiece = mapM fromPathPiece
140− toPathMultiPiece = map toPathPiece
141−
142−-- | A function for helping generate free 'PathPiece'
143−-- instances for enumeration data types
144−-- that have derived 'Read' and 'Show' instances.
145−-- Intended to be used like this:
146−--
147−-- > data MyData = Foo | Bar | Baz
148−-- > deriving (Read,Show)
149−-- > instance PathPiece MyData where
150−-- > fromPathPiece = readFromPathPiece
151−-- > toPathPiece = showToPathPiece
152−--
153−-- Since 0.2.1.
154−readFromPathPiece :: Read s => S.Text -> Maybe s
155−readFromPathPiece = readMaybe . S.unpack
156−
157−-- | See the documentation for 'readFromPathPiece'.
158−--
159−-- Since 0.2.1.
160−showToPathPiece :: Show s => s -> S.Text
161−showToPathPiece = S.pack . show
8+import Web.PathPieces.Internal
1629