path-pieces-0.2.1/0000755000000000000000000000000012614065445012111 5ustar0000000000000000path-pieces-0.2.1/LICENSE0000644000000000000000000000253012614065445013116 0ustar0000000000000000The following license covers this documentation, and the source code, except where otherwise indicated. Copyright 2009, Michael Snoyman. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. path-pieces-0.2.1/README.md0000644000000000000000000000033712614065445013373 0ustar0000000000000000## 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. path-pieces-0.2.1/Setup.lhs0000644000000000000000000000016212614065445013720 0ustar0000000000000000#!/usr/bin/env runhaskell > module Main where > import Distribution.Simple > main :: IO () > main = defaultMain path-pieces-0.2.1/ChangeLog.md0000644000000000000000000000040412614065445014260 0ustar0000000000000000## 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). path-pieces-0.2.1/path-pieces.cabal0000644000000000000000000000223312614065445015277 0ustar0000000000000000name: path-pieces version: 0.2.1 license: BSD3 license-file: LICENSE author: Michael Snoyman maintainer: Michael Snoyman synopsis: Components of paths. description: Hackage documentation generation is not reliable. For up to date documentation, please see: . 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 , text >= 0.5 , time exposed-modules: Web.PathPieces ghc-options: -Wall test-suite test type: exitcode-stdio-1.0 main-is: main.hs hs-source-dirs: test ghc-options: -Wall build-depends: HUnit , hspec >= 1.3 , base >= 4 && < 5 , QuickCheck , path-pieces , text source-repository head type: git location: https://github.com/yesodweb/path-pieces path-pieces-0.2.1/test/0000755000000000000000000000000012614065445013070 5ustar0000000000000000path-pieces-0.2.1/test/main.hs0000644000000000000000000000513512614065445014354 0ustar0000000000000000{-# Language ScopedTypeVariables #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Main where import Test.Hspec import Test.Hspec.QuickCheck(prop) import Test.QuickCheck import Web.PathPieces import qualified Data.Text as T import Data.Maybe (fromJust) -- import FileLocation (debug) instance Arbitrary T.Text where arbitrary = fmap T.pack arbitrary main :: IO () main = hspec spec spec :: Spec spec = do describe "PathPiece" $ do prop "toPathPiece <=> fromPathPiece String" $ \(p::String) -> case (fromPathPiece . toPathPiece) p of Nothing -> null p Just pConverted -> p == pConverted prop "toPathPiece <=> fromPathPiece Text" $ \(p::T.Text) -> case (fromPathPiece . toPathPiece) p of Nothing -> T.null p Just pConverted -> p == pConverted prop "toPathPiece <=> fromPathPiece Int" $ \(p::Int) -> case (fromPathPiece . toPathPiece) p of Nothing -> False Just pConverted -> p == pConverted prop "toPathPiece <=> fromPathPiece Bool" $ \(p::Bool) -> case (fromPathPiece . toPathPiece) p of Nothing -> False Just pConverted -> p == pConverted prop "toPathPiece <=> fromPathPiece Maybe String" $ \(p::Maybe String) -> case (fromPathPiece . toPathPiece) p of Nothing -> False Just pConverted -> p == pConverted describe "PathMultiPiece" $ do prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[String]) -> p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p prop "toPathMultiPiece <=> fromPathMultiPiece Text" $ \(p::[T.Text]) -> p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p describe "SinglePiece" $ do prop "toPathPiece <=> fromPathPiece String" $ \(p::String) -> case (fromPathPiece . toPathPiece) p of Nothing -> null p Just pConverted -> p == pConverted prop "toPathPiece <=> fromPathPiece Text" $ \(p::T.Text) -> case (fromPathPiece . toPathPiece) p of Nothing -> T.null p Just pConverted -> p == pConverted prop "toPathPiece <=> fromPathPiece Int" $ \(p::Int) -> case (fromPathPiece . toPathPiece) p of Nothing -> p < 0 Just pConverted -> p == pConverted describe "MultiPiece" $ do prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[String]) -> p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p prop "toPathMultiPiece <=> fromPathMultiPiece Text" $ \(p::[T.Text]) -> p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p it "bad ints are rejected" $ fromPathPiece (T.pack "123hello") `shouldBe` (Nothing :: Maybe Int) path-pieces-0.2.1/Web/0000755000000000000000000000000012614065445012626 5ustar0000000000000000path-pieces-0.2.1/Web/PathPieces.hs0000644000000000000000000001152212614065445015210 0ustar0000000000000000{-# LANGUAGE FlexibleInstances, TypeSynonymInstances, OverloadedStrings #-} module Web.PathPieces ( PathPiece (..) , PathMultiPiece (..) , readFromPathPiece , showToPathPiece -- * Deprecated , toSinglePiece , toMultiPiece , fromSinglePiece , fromMultiPiece ) where 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 instance PathPiece S.Text where fromPathPiece = Just toPathPiece = id instance PathPiece L.Text where 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 Right (i, "") -> Just i _ -> Nothing toPathPiece = S.pack . show instance PathPiece Int where 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 (a, s):_ -> assert (null s) (Just a) _ -> Nothing toPathPiece = S.pack . show instance PathPiece Day where fromPathPiece t = case reads $ S.unpack t of [(a,"")] -> Just a _ -> Nothing toPathPiece = S.pack . show instance (PathPiece a) => PathPiece (Maybe a) where fromPathPiece s = case S.stripPrefix "Just " s of Just r -> Just `fmap` fromPathPiece r _ -> case s of "Nothing" -> Just Nothing _ -> Nothing toPathPiece m = case m of Just s -> "Just " `S.append` toPathPiece s _ -> "Nothing" class PathMultiPiece s where fromPathMultiPiece :: [S.Text] -> Maybe s toPathMultiPiece :: s -> [S.Text] instance PathPiece a => PathMultiPiece [a] where fromPathMultiPiece = mapM fromPathPiece toPathMultiPiece = map toPathPiece -- | 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 -- | 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 toSinglePiece = toPathPiece {-# DEPRECATED fromSinglePiece "Use fromPathPiece instead of fromSinglePiece" #-} fromSinglePiece :: PathPiece p => S.Text -> Maybe p fromSinglePiece = fromPathPiece {-# DEPRECATED toMultiPiece "Use toPathMultiPiece instead of toMultiPiece" #-} toMultiPiece :: PathMultiPiece ps => ps -> [S.Text] toMultiPiece = toPathMultiPiece {-# DEPRECATED fromMultiPiece "Use fromPathMultiPiece instead of fromMultiPiece" #-} fromMultiPiece :: PathMultiPiece ps => [S.Text] -> Maybe ps fromMultiPiece = fromPathMultiPiece