@@ -2,6 +2,8 @@
2{-# LANGUAGE DefaultSignatures #-}
2{-# LANGUAGE DefaultSignatures #-}
3{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE OverloadedStrings #-}
5+{-# LANGUAGE Rank2Types #-}
6+{-# LANGUAGE ScopedTypeVariables #-}
5{-# LANGUAGE TypeSynonymInstances #-}
7{-# LANGUAGE TypeSynonymInstances #-}
7-- Convert Haskell values to and from route pieces.
9-- Convert Haskell values to and from route pieces.
@@ -13,6 +15,7 @@ module Web.PathPieces (
13 WrappedPathPiece(..),
15 WrappedPathPiece(..),
16import Data.Int
19import Data.Int
17import Data.Word
20import Data.Word
18import qualified Data.Text as S
21import qualified Data.Text as S
@@ -21,6 +24,8 @@ import Data.Version (Version)
21import Data.Time (Day)
24import Data.Time (Day)
22import Text.Read (readMaybe)
25import Text.Read (readMaybe)
24#if MIN_VERSION_base(4,8,0)
29#if MIN_VERSION_base(4,8,0)
25import Data.Void (Void)
30import Data.Void (Void)
@@ -65,6 +70,33 @@ instance PathPiece Version
65instance PathPiece Void
70instance PathPiece Void
73+instance PathPiece All
74+instance PathPiece Any
76+instance PathPiece a => PathPiece (Dual a) where
77+ toPathPiece = unsafeToPathPiece1
78+ fromPathPiece = unsafeFromPathPiece1
80+instance PathPiece a => PathPiece (Sum a) where
81+ toPathPiece = unsafeToPathPiece1
82+ fromPathPiece = unsafeFromPathPiece1
84+instance PathPiece a => PathPiece (Product a) where
85+ toPathPiece = unsafeToPathPiece1
86+ fromPathPiece = unsafeFromPathPiece1
88+instance PathPiece a => PathPiece (First a) where
89+ toPathPiece = unsafeToPathPiece1
90+ fromPathPiece = unsafeFromPathPiece1
92+instance PathPiece a => PathPiece (Last a) where
93+ toPathPiece = unsafeToPathPiece1
94+ fromPathPiece = unsafeFromPathPiece1
96+instance PathPiece a => PathPiece (Maybe a) where
97+ toPathPiece = unsafeToPathPiece1
98+ fromPathPiece = unsafeFromPathPiece1
68-- | Wrapped @'PathPiece'@ value.
100-- | Wrapped @'PathPiece'@ value.
69newtype WrappedPathPiece a = WrappedPathPiece { unwrapPathPiece :: a }
101newtype WrappedPathPiece a = WrappedPathPiece { unwrapPathPiece :: a }
@@ -74,9 +106,11 @@ instance PathPiece a => ToHttpApiData (WrappedPathPiece a) where
74instance PathPiece a => FromHttpApiData (WrappedPathPiece a) where
106instance PathPiece a => FromHttpApiData (WrappedPathPiece a) where
75 parseUrlPiece = fmap WrappedPathPiece . parseMaybeHttpApiData fromPathPiece
107 parseUrlPiece = fmap WrappedPathPiece . parseMaybeHttpApiData fromPathPiece
77−instance PathPiece a => PathPiece (Maybe a) where
109+unsafeToPathPiece1 :: forall f a. (PathPiece a, ToHttpApiData (f (WrappedPathPiece a))) => f a -> S.Text
78− toPathPiece = toUrlPiece . fmap WrappedPathPiece
110+unsafeToPathPiece1 = toUrlPiece . (unsafeCoerce :: f a -> f (WrappedPathPiece a))
112+unsafeFromPathPiece1 :: forall f a. (PathPiece a, FromHttpApiData (f (WrappedPathPiece a))) => S.Text -> Maybe (f a)
79− fromPathPiece = either (const Nothing) (Just . fmap unwrapPathPiece) . parseUrlPiece
113+unsafeFromPathPiece1 = either (const Nothing) (Just . (unsafeCoerce :: f (WrappedPathPiece a) -> f a)) . parseUrlPiece
81-- | Convert Haskell values to and from sequence of route pieces.
115-- | Convert Haskell values to and from sequence of route pieces.
82class PathMultiPiece s where
116class PathMultiPiece s where