{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
module Network.Wai.Routing.Purescheme.Core.Path
( path
, pathSegment
, pathVar
, pathEnd
)
where
import Network.Wai.Routing.Purescheme.Core.Basic
import Network.Wai.Routing.Purescheme.Core.Internal
import Data.Text (Text, intercalate)
import Network.Wai (pathInfo)
path :: Text -> GenericApplication r -> GenericApplication r
path :: Text -> GenericApplication r -> GenericApplication r
path Text
p GenericApplication r
app Request
req =
if Text
p Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> [Text] -> Text
intercalate Text
"/" (Request -> [Text]
pathInfo Request
req)
then GenericApplication r
app GenericApplication r -> GenericApplication r
forall a b. (a -> b) -> a -> b
$ Request
req {pathInfo :: [Text]
pathInfo = []}
else Rejection -> (r -> IO ResponseReceived) -> IO ResponseReceived
forall r.
Rejection -> (r -> IO ResponseReceived) -> IO ResponseReceived
reject Rejection
notFoundDefaultRejection
pathSegment :: Text -> GenericApplication r -> GenericApplication r
pathSegment :: Text -> GenericApplication r -> GenericApplication r
pathSegment Text
expectedSegment GenericApplication r
app Request
req =
case Request -> [Text]
pathInfo Request
req of
(Text
p:[Text]
rest) | Text
p Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
expectedSegment -> GenericApplication r
app GenericApplication r -> GenericApplication r
forall a b. (a -> b) -> a -> b
$ Request
req {pathInfo :: [Text]
pathInfo = [Text]
rest}
[Text]
_ -> Rejection -> (r -> IO ResponseReceived) -> IO ResponseReceived
forall r.
Rejection -> (r -> IO ResponseReceived) -> IO ResponseReceived
reject Rejection
notFoundDefaultRejection
pathVar :: FromUri a => (a -> GenericApplication r) -> GenericApplication r
pathVar :: (a -> GenericApplication r) -> GenericApplication r
pathVar a -> GenericApplication r
f Request
req =
case Request -> [Text]
pathInfo Request
req of
[] -> Rejection -> (r -> IO ResponseReceived) -> IO ResponseReceived
forall r.
Rejection -> (r -> IO ResponseReceived) -> IO ResponseReceived
reject Rejection
notFoundDefaultRejection
[Text
""] -> Rejection -> (r -> IO ResponseReceived) -> IO ResponseReceived
forall r.
Rejection -> (r -> IO ResponseReceived) -> IO ResponseReceived
reject Rejection
notFoundDefaultRejection
(Text
p:[Text]
rest) -> a -> GenericApplication r
f (Text -> a
forall a. FromUri a => Text -> a
fromText Text
p) (Request
req{pathInfo :: [Text]
pathInfo = [Text]
rest})
pathEnd :: GenericApplication r -> GenericApplication r
pathEnd :: GenericApplication r -> GenericApplication r
pathEnd GenericApplication r
f Request
req =
case Request -> [Text]
pathInfo Request
req of
[] -> GenericApplication r
f Request
req
[Text
""] -> GenericApplication r
f Request
req{pathInfo :: [Text]
pathInfo = []}
[Text]
_ -> Rejection -> (r -> IO ResponseReceived) -> IO ResponseReceived
forall r.
Rejection -> (r -> IO ResponseReceived) -> IO ResponseReceived
reject Rejection
notFoundDefaultRejection