module Web.Routes.Site where
import Data.ByteString
import Data.Monoid
import Data.Text (Text)
import Web.Routes.Base (decodePathInfo, encodePathInfo)
data Site url a
= Site {
forall url a.
Site url a -> (url -> [(Text, Maybe Text)] -> Text) -> url -> a
handleSite :: (url -> [(Text, Maybe Text)] -> Text) -> url -> a
, forall url a. Site url a -> url -> ([Text], [(Text, Maybe Text)])
formatPathSegments :: url -> ([Text], [(Text, Maybe Text)])
, forall url a. Site url a -> [Text] -> Either String url
parsePathSegments :: [Text] -> Either String url
}
setDefault :: url -> Site url a -> Site url a
setDefault :: forall url a. url -> Site url a -> Site url a
setDefault url
defUrl (Site (url -> [(Text, Maybe Text)] -> Text) -> url -> a
handle url -> ([Text], [(Text, Maybe Text)])
format [Text] -> Either String url
parse) =
forall url a.
((url -> [(Text, Maybe Text)] -> Text) -> url -> a)
-> (url -> ([Text], [(Text, Maybe Text)]))
-> ([Text] -> Either String url)
-> Site url a
Site (url -> [(Text, Maybe Text)] -> Text) -> url -> a
handle url -> ([Text], [(Text, Maybe Text)])
format [Text] -> Either String url
parse'
where
parse' :: [Text] -> Either String url
parse' [] = forall a b. b -> Either a b
Right url
defUrl
parse' [Text]
x = [Text] -> Either String url
parse [Text]
x
instance Functor (Site url) where
fmap :: forall a b. (a -> b) -> Site url a -> Site url b
fmap a -> b
f Site url a
site = Site url a
site { handleSite :: (url -> [(Text, Maybe Text)] -> Text) -> url -> b
handleSite = \url -> [(Text, Maybe Text)] -> Text
showFn url
u -> a -> b
f (forall url a.
Site url a -> (url -> [(Text, Maybe Text)] -> Text) -> url -> a
handleSite Site url a
site url -> [(Text, Maybe Text)] -> Text
showFn url
u) }
runSite :: Text
-> Site url a
-> [Text]
-> (Either String a)
runSite :: forall url a. Text -> Site url a -> [Text] -> Either String a
runSite Text
approot Site url a
site [Text]
pathInfo =
case forall url a. Site url a -> [Text] -> Either String url
parsePathSegments Site url a
site [Text]
pathInfo of
(Left String
errs) -> (forall a b. a -> Either a b
Left String
errs)
(Right url
url) -> forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ forall url a.
Site url a -> (url -> [(Text, Maybe Text)] -> Text) -> url -> a
handleSite Site url a
site url -> [(Text, Maybe Text)] -> Text
showFn url
url
where
showFn :: url -> [(Text, Maybe Text)] -> Text
showFn url
url [(Text, Maybe Text)]
qs =
let ([Text]
pieces, [(Text, Maybe Text)]
qs') = forall url a. Site url a -> url -> ([Text], [(Text, Maybe Text)])
formatPathSegments Site url a
site url
url
in Text
approot forall a. Monoid a => a -> a -> a
`mappend` ([Text] -> [(Text, Maybe Text)] -> Text
encodePathInfo [Text]
pieces ([(Text, Maybe Text)]
qs forall a. [a] -> [a] -> [a]
++ [(Text, Maybe Text)]
qs'))