module Miso.Router
( runRoute
, RoutingError (..)
, HasURI (..)
, getURI
, setURI
, makeLens
) where
import qualified Data.ByteString.Char8 as BS
import Data.Proxy
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Encoding
import GHC.TypeLits
import Network.HTTP.Types
import Network.URI
import Servant.API
import Web.HttpApiData
import Miso.Html hiding (text)
import Miso.Lens
data Location = Location
{ locPath :: [Text]
, locQuery :: Query
} deriving (Show, Eq, Ord)
data RoutingError = Fail
deriving (Show, Eq, Ord)
data Router a where
RChoice :: Router a -> Router a -> Router a
RCapture :: FromHttpApiData x => (x -> Router a) -> Router a
RQueryParam :: (FromHttpApiData x, KnownSymbol sym)
=> Proxy sym -> (Maybe x -> Router a) -> Router a
RQueryParams :: (FromHttpApiData x, KnownSymbol sym)
=> Proxy sym -> ([x] -> Router a) -> Router a
RQueryFlag :: KnownSymbol sym
=> Proxy sym -> (Bool -> Router a) -> Router a
RPath :: KnownSymbol sym => Proxy sym -> Router a -> Router a
RPage :: a -> Router a
class HasRouter model layout where
type RouteT model layout a :: *
route :: Proxy layout -> Proxy a -> RouteT model layout a -> model -> Router a
instance (HasRouter m x, HasRouter m y) => HasRouter m (x :<|> y) where
type RouteT m (x :<|> y) a = RouteT m x a :<|> RouteT m y a
route _ (a :: Proxy a) ((x :: RouteT m x a) :<|> (y :: RouteT m y a)) m
= RChoice (route (Proxy :: Proxy x) a x m) (route (Proxy :: Proxy y) a y m)
instance (HasRouter m sublayout, FromHttpApiData x) =>
HasRouter m (Capture sym x :> sublayout) where
type RouteT m (Capture sym x :> sublayout) a = x -> RouteT m sublayout a
route _ a f m = RCapture (\x -> route (Proxy :: Proxy sublayout) a (f x) m)
instance (HasRouter m sublayout, FromHttpApiData x, KnownSymbol sym)
=> HasRouter m (QueryParam sym x :> sublayout) where
type RouteT m (QueryParam sym x :> sublayout) a = Maybe x -> RouteT m sublayout a
route _ a f m = RQueryParam (Proxy :: Proxy sym)
(\x -> route (Proxy :: Proxy sublayout) a (f x) m)
instance (HasRouter m sublayout, FromHttpApiData x, KnownSymbol sym)
=> HasRouter m (QueryParams sym x :> sublayout) where
type RouteT m (QueryParams sym x :> sublayout) a = [x] -> RouteT m sublayout a
route _ a f m = RQueryParams
(Proxy :: Proxy sym)
(\x -> route (Proxy :: Proxy sublayout) a (f x) m)
instance (HasRouter m sublayout, KnownSymbol sym)
=> HasRouter m (QueryFlag sym :> sublayout) where
type RouteT m (QueryFlag sym :> sublayout) a = Bool -> RouteT m sublayout a
route _ a f m = RQueryFlag
(Proxy :: Proxy sym)
(\x -> route (Proxy :: Proxy sublayout) a (f x) m)
instance (HasRouter m sublayout, KnownSymbol path)
=> HasRouter m (path :> sublayout) where
type RouteT m (path :> sublayout) a = RouteT m sublayout a
route _ a page m = RPath
(Proxy :: Proxy path)
(route (Proxy :: Proxy sublayout) a page m)
instance HasRouter m (View a) where
type RouteT m (View a) x = m -> x
route _ _ a m = RPage (a m)
runRouteLoc :: forall m layout a. HasRouter m layout
=> Location -> Proxy layout -> RouteT m layout a -> m -> Either RoutingError a
runRouteLoc loc layout page m =
let routing = route layout (Proxy :: Proxy a) page m
in routeLoc loc routing m
runRoute
:: (HasURI m, HasRouter m layout)
=> Proxy layout
-> RouteT m layout a
-> m
-> Either RoutingError a
runRoute layout page m = runRouteLoc (uriToLocation (getURI m)) layout page m
routeLoc :: Location -> Router a -> m -> Either RoutingError a
routeLoc loc r m = case r of
RChoice a b -> do
case routeLoc loc a m of
Left Fail -> routeLoc loc b m
Right x -> Right x
RCapture f -> case locPath loc of
[] -> Left Fail
capture:paths ->
case parseUrlPieceMaybe capture of
Nothing -> Left Fail
Just x -> routeLoc loc { locPath = paths } (f x) m
RQueryParam sym f -> case lookup (BS.pack $ symbolVal sym) (locQuery loc) of
Nothing -> routeLoc loc (f Nothing) m
Just Nothing -> Left Fail
Just (Just text) -> case parseQueryParamMaybe (decodeUtf8 text) of
Nothing -> Left Fail
Just x -> routeLoc loc (f (Just x)) m
RQueryParams sym f -> maybe (Left Fail) (\x -> routeLoc loc (f x) m) $ do
ps <- sequence $ snd <$> Prelude.filter
(\(k, _) -> k == BS.pack (symbolVal sym)) (locQuery loc)
sequence $ (parseQueryParamMaybe . decodeUtf8) <$> ps
RQueryFlag sym f -> case lookup (BS.pack $ symbolVal sym) (locQuery loc) of
Nothing -> routeLoc loc (f False) m
Just Nothing -> routeLoc loc (f True) m
Just (Just _) -> Left Fail
RPath sym a -> case locPath loc of
[] -> Left Fail
p:paths -> if p == T.pack (symbolVal sym)
then routeLoc (loc { locPath = paths }) a m
else Left Fail
RPage a ->
case locPath loc of
[] -> Right a
_ -> Left Fail
uriToLocation :: URI -> Location
uriToLocation uri = Location
{ locPath = decodePathSegments $ BS.pack (uriPath uri)
, locQuery = parseQuery $ BS.pack (uriQuery uri)
}
class HasURI m where lensURI :: Lens' m URI
getURI :: HasURI m => m -> URI
getURI = get lensURI
setURI :: HasURI m => URI -> m -> m
setURI m u = set lensURI m u