{- This file is part of Vervis. - - Written 2019 by fr33domlover . - - ♡ Copying is an act of love. Please copy, reuse and share. - - The author(s) have dedicated all copyright and related and neighboring - rights to this software to the public domain worldwide. This software is - distributed without any warranty. - - You should have received a copy of the CC0 Public Domain Dedication along - with this software. If not, see - . -} module Yesod.FedURI ( getEncodeRouteFed , getEncodeRouteLocal , decodeRouteLocal ) where import Prelude import Control.Monad import Data.Text.Encoding import Network.HTTP.Types.URI import Yesod.Core import Yesod.Core.Handler import qualified Data.Text as T import Network.FedURI getEncodeRouteFed :: MonadHandler m => m (Route (HandlerSite m) -> FedURI) getEncodeRouteFed = toFed <$> getUrlRender where toFed renderUrl route = case parseFedURI $ renderUrl route of Left e -> error $ "getUrlRender produced invalid FedURI: " ++ e Right u -> u getEncodeRouteLocal :: MonadHandler m => m (Route (HandlerSite m) -> LocalURI) getEncodeRouteLocal = (\ f -> snd . f2l . f) <$> getEncodeRouteFed decodeRouteLocal :: ParseRoute site => LocalURI -> Maybe (Route site) decodeRouteLocal = parseRoute . (,[]) . decodePathSegments . encodeUtf8 . luriPath <=< noFrag where noFrag lu = if T.null $ luriFragment lu then Just lu else Nothing