@@ -2,6 +2,8 @@
2module Web.PathPieces
2module Web.PathPieces
3 ( PathPiece (..)
3 ( PathPiece (..)
4 , PathMultiPiece (..)
4 , PathMultiPiece (..)
5 -- * Deprecated
7 -- * Deprecated
6 , toSinglePiece
8 , toSinglePiece
7 , toMultiPiece
9 , toMultiPiece
@@ -16,6 +18,7 @@ import qualified Data.Text.Lazy as L
16import qualified Data.Text.Read
18import qualified Data.Text.Read
17import Data.Time (Day)
19import Data.Time (Day)
18import Control.Exception (assert)
20import Control.Exception (assert)
21+import Text.Read (readMaybe)
20class PathPiece s where
23class PathPiece s where
21 fromPathPiece :: S.Text -> Maybe s
24 fromPathPiece :: S.Text -> Maybe s
@@ -122,6 +125,27 @@ instance PathPiece a => PathMultiPiece [a] where
122 fromPathMultiPiece = mapM fromPathPiece
125 fromPathMultiPiece = mapM fromPathPiece
123 toPathMultiPiece = map toPathPiece
126 toPathMultiPiece = map toPathPiece
128+-- | A function for helping generate free 'PathPiece'
129+-- instances for enumeration data types
130+-- that have derived 'Read' and 'Show' instances.
131+-- Intended to be used like this:
133+-- > data MyData = Foo | Bar | Baz
134+-- > deriving (Read,Show)
135+-- > instance PathPiece MyData where
136+-- > fromPathPiece = readFromPathPiece
137+-- > toPathPiece = showToPathPiece
140+readFromPathPiece :: Read s => S.Text -> Maybe s
141+readFromPathPiece = readMaybe . S.unpack
143+-- | See the documentation for 'readFromPathPiece'.
146+showToPathPiece :: Show s => s -> S.Text
147+showToPathPiece = S.pack . show
125{-# DEPRECATED toSinglePiece "Use toPathPiece instead of toSinglePiece" #-}
149{-# DEPRECATED toSinglePiece "Use toPathPiece instead of toSinglePiece" #-}
126toSinglePiece :: PathPiece p => p -> S.Text
150toSinglePiece :: PathPiece p => p -> S.Text
127toSinglePiece = toPathPiece
151toSinglePiece = toPathPiece