path-pieces 0.1.3.1 → 0.1.4
raw patch · 3 files changed
+45/−14 lines, 3 files
Files
- Web/PathPieces.hs +25/−7
- path-pieces.cabal +1/−1
- test/main.hs +19/−6
Web/PathPieces.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}+{-# LANGUAGE FlexibleInstances, TypeSynonymInstances, OverloadedStrings #-} module Web.PathPieces ( PathPiece (..) , PathMultiPiece (..)@@ -14,6 +14,7 @@ import qualified Data.Text.Lazy as L import qualified Data.Text.Read import Data.Time (Day)+import Control.Exception (assert) class PathPiece s where fromPathPiece :: S.Text -> Maybe s@@ -34,22 +35,29 @@ instance PathPiece Integer where fromPathPiece s = case Data.Text.Read.signed Data.Text.Read.decimal s of- Right (i, _) -> Just i- Left _ -> Nothing+ Right (i, "") -> Just i+ _ -> Nothing 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- Left _ -> Nothing+ Right (i, "") -> Just i+ _ -> Nothing 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 Int64 where fromPathPiece s = case Data.Text.Read.signed Data.Text.Read.decimal s of- Right (i, _) -> Just i- Left _ -> Nothing+ Right (i, "") -> Just i+ _ -> Nothing toPathPiece = S.pack . show instance PathPiece Day where@@ -58,6 +66,16 @@ [(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
path-pieces.cabal view
@@ -1,5 +1,5 @@ name: path-pieces-version: 0.1.3.1+version: 0.1.4 license: BSD3 license-file: LICENSE author: Michael Snoyman <michael@snoyman.com>
test/main.hs view
@@ -27,21 +27,31 @@ Nothing -> null p Just pConverted -> p == pConverted - prop "toPathPiece <=> fromPathPiece String" $ \(p::T.Text) ->+ prop "toPathPiece <=> fromPathPiece Text" $ \(p::T.Text) -> case (fromPathPiece . toPathPiece) p of Nothing -> T.null p Just pConverted -> p == pConverted - prop "toPathPiece <=> fromPathPiece String" $ \(p::Int) ->+ 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 String" $ \(p::[T.Text]) ->+ prop "toPathMultiPiece <=> fromPathMultiPiece Text" $ \(p::[T.Text]) -> p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p @@ -51,12 +61,12 @@ Nothing -> null p Just pConverted -> p == pConverted - prop "toPathPiece <=> fromPathPiece String" $ \(p::T.Text) ->+ prop "toPathPiece <=> fromPathPiece Text" $ \(p::T.Text) -> case (fromPathPiece . toPathPiece) p of Nothing -> T.null p Just pConverted -> p == pConverted - prop "toPathPiece <=> fromPathPiece String" $ \(p::Int) ->+ prop "toPathPiece <=> fromPathPiece Int" $ \(p::Int) -> case (fromPathPiece . toPathPiece) p of Nothing -> p < 0 Just pConverted -> p == pConverted@@ -65,5 +75,8 @@ prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[String]) -> p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p - prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[T.Text]) ->+ 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)