path-pieces 0.1.4 → 0.2.1
raw patch · 4 files changed
Files
- ChangeLog.md +7/−0
- README.md +6/−0
- Web/PathPieces.hs +78/−21
- path-pieces.cabal +4/−1
+ ChangeLog.md view
@@ -0,0 +1,7 @@+## 0.2.0++* Make `PathMultiPiece` instance for lists based on `PathPiece` [Yesod issue #932](https://github.com/yesodweb/yesod/issues/932)++## 0.1.5++Added more integral instances, and check bounds on them [#5](https://github.com/yesodweb/path-pieces/pull/5).
+ README.md view
@@ -0,0 +1,6 @@+## path-pieces++Defines two typeclasses used for converting Haskell data types to and from route pieces.+Used in Yesod to automatically marshall data in the request path.++see http://www.yesodweb.com/ for more information.
Web/PathPieces.hs view
@@ -2,6 +2,8 @@ module Web.PathPieces ( PathPiece (..) , PathMultiPiece (..)+ , readFromPathPiece+ , showToPathPiece -- * Deprecated , toSinglePiece , toMultiPiece@@ -9,17 +11,23 @@ , fromMultiPiece ) where -import Data.Int (Int64)+import Data.Int (Int8, Int16, Int32, Int64)+import Data.Word (Word, Word8, Word16, Word32, Word64) import qualified Data.Text as S import qualified Data.Text.Lazy as L import qualified Data.Text.Read import Data.Time (Day) import Control.Exception (assert)+import Text.Read (readMaybe) class PathPiece s where fromPathPiece :: S.Text -> Maybe s toPathPiece :: s -> S.Text +instance PathPiece () where+ fromPathPiece t = if t == "_" then Just () else Nothing+ toPathPiece () = "_"+ instance PathPiece String where fromPathPiece = Just . S.unpack toPathPiece = S.pack@@ -32,6 +40,16 @@ fromPathPiece = Just . L.fromChunks . return toPathPiece = S.concat . L.toChunks +parseIntegral :: (Integral a, Bounded a, Ord a) => S.Text -> Maybe a+parseIntegral s = n+ where+ n = case Data.Text.Read.signed Data.Text.Read.decimal s of+ Right (i, "") | i <= top && i >= bot -> Just (fromInteger i)+ _ -> Nothing+ Just witness = n+ top = toInteger (maxBound `asTypeOf` witness)+ bot = toInteger (minBound `asTypeOf` witness)+ instance PathPiece Integer where fromPathPiece s = case Data.Text.Read.signed Data.Text.Read.decimal s of@@ -40,12 +58,45 @@ toPathPiece = S.pack . show instance PathPiece Int where- fromPathPiece s =- case Data.Text.Read.signed Data.Text.Read.decimal s of- Right (i, "") -> Just i- _ -> Nothing+ fromPathPiece = parseIntegral toPathPiece = S.pack . show +instance PathPiece Int8 where+ fromPathPiece = parseIntegral+ toPathPiece = S.pack . show++instance PathPiece Int16 where+ fromPathPiece = parseIntegral+ toPathPiece = S.pack . show++instance PathPiece Int32 where+ fromPathPiece = parseIntegral+ toPathPiece = S.pack . show++instance PathPiece Int64 where+ fromPathPiece = parseIntegral+ toPathPiece = S.pack . show++instance PathPiece Word where+ fromPathPiece = parseIntegral+ toPathPiece = S.pack . show++instance PathPiece Word8 where+ fromPathPiece = parseIntegral+ toPathPiece = S.pack . show++instance PathPiece Word16 where+ fromPathPiece = parseIntegral+ toPathPiece = S.pack . show++instance PathPiece Word32 where+ fromPathPiece = parseIntegral+ toPathPiece = S.pack . show++instance PathPiece Word64 where+ fromPathPiece = parseIntegral+ toPathPiece = S.pack . show+ instance PathPiece Bool where fromPathPiece t = case filter (null . snd) $ reads $ S.unpack t of@@ -53,13 +104,6 @@ _ -> Nothing toPathPiece = S.pack . show -instance PathPiece Int64 where- fromPathPiece s =- case Data.Text.Read.signed Data.Text.Read.decimal s of- Right (i, "") -> Just i- _ -> Nothing- toPathPiece = S.pack . show- instance PathPiece Day where fromPathPiece t = case reads $ S.unpack t of@@ -81,17 +125,30 @@ fromPathMultiPiece :: [S.Text] -> Maybe s toPathMultiPiece :: s -> [S.Text] -instance PathMultiPiece [String] where- fromPathMultiPiece = Just . map S.unpack- toPathMultiPiece = map S.pack+instance PathPiece a => PathMultiPiece [a] where+ fromPathMultiPiece = mapM fromPathPiece+ toPathMultiPiece = map toPathPiece -instance PathMultiPiece [S.Text] where- fromPathMultiPiece = Just- toPathMultiPiece = id+-- | A function for helping generate free 'PathPiece'+-- instances for enumeration data types +-- that have derived 'Read' and 'Show' instances.+-- Intended to be used like this:+--+-- > data MyData = Foo | Bar | Baz+-- > deriving (Read,Show)+-- > instance PathPiece MyData where+-- > fromPathPiece = readFromPathPiece+-- > toPathPiece = showToPathPiece+--+-- Since 0.2.1. +readFromPathPiece :: Read s => S.Text -> Maybe s+readFromPathPiece = readMaybe . S.unpack -instance PathMultiPiece [L.Text] where- fromPathMultiPiece = Just . map (L.fromChunks . return)- toPathMultiPiece = map $ S.concat . L.toChunks+-- | See the documentation for 'readFromPathPiece'.+--+-- Since 0.2.1. +showToPathPiece :: Show s => s -> S.Text+showToPathPiece = S.pack . show {-# DEPRECATED toSinglePiece "Use toPathPiece instead of toSinglePiece" #-} toSinglePiece :: PathPiece p => p -> S.Text
path-pieces.cabal view
@@ -1,16 +1,19 @@ name: path-pieces-version: 0.1.4+version: 0.2.1 license: BSD3 license-file: LICENSE author: Michael Snoyman <michael@snoyman.com> maintainer: Michael Snoyman <michael@snoyman.com> synopsis: Components of paths.+description: Hackage documentation generation is not reliable. For up to date documentation, please see: <http://www.stackage.org/package/path-pieces>. category: Web, Yesod stability: unstable cabal-version: >= 1.8 build-type: Simple extra-source-files: test/main.hs+ ChangeLog.md+ README.md library build-depends: base >= 4 && < 5