@@ -1,164 +0,0 @@
2−{-# LANGUAGE DefaultSignatures #-}
3−{-# LANGUAGE FlexibleInstances #-}
4−{-# LANGUAGE OverloadedStrings #-}
5−{-# LANGUAGE Rank2Types #-}
6−{-# LANGUAGE ScopedTypeVariables #-}
7−{-# LANGUAGE TypeSynonymInstances #-}
9−-- Convert Haskell values to and from route pieces.
10−module Web.PathPieces.Internal where
15−import qualified Data.Text as S
16−import qualified Data.Text.Lazy as L
17−import Data.Version (Version)
18−import Data.Time (Day)
19−import Text.Read (readMaybe)
23−#if MIN_VERSION_base(4,8,0)
24−import Data.Void (Void)
27−import Web.HttpApiData
28−import Web.HttpApiData.Internal (parseMaybeTextData, parseUrlPieceMaybe)
30−-- | Convert Haskell values to and from route piece.
31−class PathPiece s where
32− -- | Convert from route piece.
33− fromPathPiece :: S.Text -> Maybe s
34− default fromPathPiece :: FromHttpApiData s => S.Text -> Maybe s
35− fromPathPiece = defaultFromPathPiece
37− -- | Convert to route piece.
38− toPathPiece :: s -> S.Text
39− default toPathPiece :: ToHttpApiData s => s -> S.Text
40− toPathPiece = defaultToPathPiece
43−instance PathPiece Char
44−instance PathPiece Bool
45−instance PathPiece Ordering
46−instance PathPiece Double
47−instance PathPiece Float
48−instance PathPiece Int
49−instance PathPiece Int8
50−instance PathPiece Int16
51−instance PathPiece Int32
52−instance PathPiece Int64
53−instance PathPiece Integer
54−instance PathPiece Word
55−instance PathPiece Word8
56−instance PathPiece Word16
57−instance PathPiece Word32
58−instance PathPiece Word64
59−instance PathPiece String
60−instance PathPiece S.Text
61−instance PathPiece L.Text
62−instance PathPiece Day
63−instance PathPiece Version
65−#if MIN_VERSION_base(4,8,0)
66−instance PathPiece Void
69−instance PathPiece All
70−instance PathPiece Any
72−instance PathPiece a => PathPiece (Dual a) where
73− toPathPiece = defaultToPathPiece1
74− fromPathPiece = defaultFromPathPiece1
76−instance PathPiece a => PathPiece (Sum a) where
77− toPathPiece = defaultToPathPiece1
78− fromPathPiece = defaultFromPathPiece1
80−instance PathPiece a => PathPiece (Product a) where
81− toPathPiece = defaultToPathPiece1
82− fromPathPiece = defaultFromPathPiece1
84−instance PathPiece a => PathPiece (First a) where
85− toPathPiece = defaultToPathPiece1
86− fromPathPiece = defaultFromPathPiece1
88−instance PathPiece a => PathPiece (Last a) where
89− toPathPiece = defaultToPathPiece1
90− fromPathPiece = defaultFromPathPiece1
92−instance PathPiece a => PathPiece (Maybe a) where
93− toPathPiece = defaultToPathPiece1
94− fromPathPiece = defaultFromPathPiece1
96−instance (PathPiece a, PathPiece b) => PathPiece (Either a b) where
97− toPathPiece = defaultToPathPiece2
98− fromPathPiece = defaultFromPathPiece2
100−-- | Wrapped @'PathPiece'@ value.
101−newtype WrappedPathPiece a = WrappedPathPiece { unwrapPathPiece :: a }
103−instance PathPiece a => ToHttpApiData (WrappedPathPiece a) where
104− toUrlPiece = toPathPiece . unwrapPathPiece
106−instance PathPiece a => FromHttpApiData (WrappedPathPiece a) where
107− parseUrlPiece = fmap WrappedPathPiece . parseMaybeTextData fromPathPiece
109−-- | Convert value to route piece using @'ToHttpApiData'@ implementation.
110−defaultToPathPiece :: ToHttpApiData a => a -> S.Text
111−defaultToPathPiece = toUrlPiece
113−-- | Convert value from route piece using @'FromHttpApiData'@ implementation.
114−defaultFromPathPiece :: FromHttpApiData a => S.Text -> Maybe a
115−defaultFromPathPiece = parseUrlPieceMaybe
117−-- | Convert value to route piece using @'PathPiece'@ instance for its parameter.
118−defaultToPathPiece1 :: (PathPiece a, ToHttpApiData (f (WrappedPathPiece a))) => f a -> S.Text
119−defaultToPathPiece1 = toUrlPiece . (unsafeCoerce :: f a -> f (WrappedPathPiece a))
121−-- | Parse value from route piece using @'PathPiece'@ instance for its parameter.
122−defaultFromPathPiece1 :: (PathPiece a, FromHttpApiData (f (WrappedPathPiece a))) => S.Text -> Maybe (f a)
123−defaultFromPathPiece1 = fmap (unsafeCoerce :: f (WrappedPathPiece a) -> f a) . defaultFromPathPiece
125−-- | Convert value to route piece using @'PathPiece'@ instance for its parameters.
126−defaultToPathPiece2 :: (PathPiece a, PathPiece b, ToHttpApiData (f (WrappedPathPiece a) (WrappedPathPiece b))) => f a b -> S.Text
127−defaultToPathPiece2 = toUrlPiece . (unsafeCoerce :: f a b -> f (WrappedPathPiece a) (WrappedPathPiece b))
129−-- | Parse value from route piece using @'PathPiece'@ instance for its parameters.
130−defaultFromPathPiece2 :: (PathPiece a, PathPiece b, FromHttpApiData (f (WrappedPathPiece a) (WrappedPathPiece b))) => S.Text -> Maybe (f a b)
131−defaultFromPathPiece2 = fmap (unsafeCoerce :: f (WrappedPathPiece a) (WrappedPathPiece b) -> f a b) . defaultFromPathPiece
133−-- | Convert Haskell values to and from sequence of route pieces.
134−class PathMultiPiece s where
135− -- | Convert from sequence of route pieces.
136− fromPathMultiPiece :: [S.Text] -> Maybe s
137− -- | Convert to sequence of route pieces.
138− toPathMultiPiece :: s -> [S.Text]
140−instance PathPiece a => PathMultiPiece [a] where
141− fromPathMultiPiece = mapM fromPathPiece
142− toPathMultiPiece = map toPathPiece
144−-- | A function for helping generate free 'PathPiece'
145−-- instances for enumeration data types
146−-- that have derived 'Read' and 'Show' instances.
147−-- Intended to be used like this:
149−-- > data MyData = Foo | Bar | Baz
150−-- > deriving (Read,Show)
151−-- > instance PathPiece MyData where
152−-- > fromPathPiece = readFromPathPiece
153−-- > toPathPiece = showToPathPiece
156−readFromPathPiece :: Read s => S.Text -> Maybe s
157−readFromPathPiece = readMaybe . S.unpack
159−-- | See the documentation for 'readFromPathPiece'.
162−showToPathPiece :: Show s => s -> S.Text
163−showToPathPiece = S.pack . show