{-# 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 p app req =
if p == intercalate "/" (pathInfo req)
then app $ req {pathInfo = []}
else reject notFoundDefaultRejection
pathSegment :: Text -> GenericApplication r -> GenericApplication r
pathSegment expectedSegment app req =
case pathInfo req of
(p:rest) | p == expectedSegment -> app $ req {pathInfo = rest}
_ -> reject notFoundDefaultRejection
pathVar :: FromUri a => (a -> GenericApplication r) -> GenericApplication r
pathVar f req =
case pathInfo req of
[] -> reject notFoundDefaultRejection
[""] -> reject notFoundDefaultRejection
(p:rest) -> f (fromText p) (req{pathInfo = rest})
pathEnd :: GenericApplication r -> GenericApplication r
pathEnd f req =
case pathInfo req of
[] -> f req
[""] -> f req{pathInfo = []}
_ -> reject notFoundDefaultRejection