git.iko.soy/typeable/http-api-data
on master help sign in
Merge pull request #8 from andrewthad/read_and_show_defaults

Added helpers for generating free PathPiece instances from data type …

commit19b5a769152ac5468868a1842c9d61a6ca83d6ef authorGreg Weber <greg@gregweber.info> dateSun 19 Jul 2015, 16:15 (11 years ago) parent102e2304dc6829
1 file changed+24
Web/PathPieces.hs+24
Web/PathPieces.hs+24
@@ -2,6 +2,8 @@
22module Web.PathPieces
33 ( PathPiece (..)
44 , PathMultiPiece (..)
5+ , readFromPathPiece
6+ , showToPathPiece
57 -- * Deprecated
68 , toSinglePiece
79 , toMultiPiece
@@ -16,6 +18,7 @@ import qualified Data.Text.Lazy as L
1618import qualified Data.Text.Read
1719import Data.Time (Day)
1820import Control.Exception (assert)
21+import Text.Read (readMaybe)
1922
2023class PathPiece s where
2124 fromPathPiece :: S.Text -> Maybe s
@@ -122,6 +125,27 @@ instance PathPiece a => PathMultiPiece [a] where
122125 fromPathMultiPiece = mapM fromPathPiece
123126 toPathMultiPiece = map toPathPiece
124127
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:
132+--
133+-- > data MyData = Foo | Bar | Baz
134+-- > deriving (Read,Show)
135+-- > instance PathPiece MyData where
136+-- > fromPathPiece = readFromPathPiece
137+-- > toPathPiece = showToPathPiece
138+--
139+-- Since 0.2.1.
140+readFromPathPiece :: Read s => S.Text -> Maybe s
141+readFromPathPiece = readMaybe . S.unpack
142+
143+-- | See the documentation for 'readFromPathPiece'.
144+--
145+-- Since 0.2.1.
146+showToPathPiece :: Show s => s -> S.Text
147+showToPathPiece = S.pack . show
148+
125149{-# DEPRECATED toSinglePiece "Use toPathPiece instead of toSinglePiece" #-}
126150toSinglePiece :: PathPiece p => p -> S.Text
127151toSinglePiece = toPathPiece