pub struct Router<S> { /* private fields */ }Expand description
The route builder.
A router’s layout, error page and hooks apply to all of its pages and to all routers nested inside it. The order of the builder calls does not matter.
Paths are made of static segments and parameters: {id}, {id=matcher}, optional
{?lang} / {?lang=matcher} and a trailing {*rest}.
Implementations§
Source§impl<S: Clone + 'static> Router<S>
impl<S: Clone + 'static> Router<S>
pub fn new() -> Self
Sourcepub fn page(self, path: &str, page: Page<S>) -> Self
pub fn page(self, path: &str, page: Page<S>) -> Self
Adds a page at path, e.g. /, /about, /blog/{slug} or /files/{*path}.
§Panics
When the pattern is invalid. Conflicting routes panic when the app is built.
Sourcepub fn nest(self, prefix: &str, router: Self) -> Self
pub fn nest(self, prefix: &str, router: Self) -> Self
Mounts router below prefix. The prefix may contain parameters, e.g. /users/{id},
which are then available to the nested hooks, layouts and pages.
Sourcepub fn merge(self, router: Self) -> Self
pub fn merge(self, router: Self) -> Self
Adds the routes of router at the same level, keeping its layout, hooks and error page
scoped to its own routes.
Paths that match no route are a 404 of this router, not of router: its error page only
shows the errors of its own routes.
Sourcepub fn named(self, name: impl Into<String>) -> Self
pub fn named(self, name: impl Into<String>) -> Self
Names this router, so pages and layouts below it can reset their layouts up to here.
Sourcepub fn error<V, U>(self, view: V) -> Selfwhere
V: View<U, S, RouteError>,
pub fn error<V, U>(self, view: V) -> Selfwhere
V: View<U, S, RouteError>,
Sets the error page for errors of this router’s routes, including paths below its prefix that match no route (404). For a router merged into another, those 404s go to the other router’s error page.
The view extracts the error as &RouteError, like a page extracts its data.
It is rendered inside this router’s layout. Errors raised by the layout’s own load
(or this router’s hooks) are handled by the error page of a parent router.
§Panics
When an error page was already set.
Sourcepub fn hook<H, T, E>(self, hook: H) -> Self
pub fn hook<H, T, E>(self, hook: H) -> Self
Adds a hook that runs before every navigation to a route of this router (and its nested
routers), before any load. Hooks of outer routers run first. Hooks also run when a
route is preloaded, so keep them free of side effects.
A hook is an async handler with extractor arguments, returning Result<(), E> where
E: Into<Rejection>: return an error to show the error page, or a redirect.
Sourcepub fn matcher(
self,
name: impl Into<String>,
matcher: impl Fn(&str) -> bool + 'static,
) -> Self
pub fn matcher( self, name: impl Into<String>, matcher: impl Fn(&str) -> bool + 'static, ) -> Self
Registers a parameter matcher, usable as {param=name} in any route of the app.
A route only matches if the matcher accepts the
segment; otherwise other routes are tried.
let router = Router::<()>::new().matcher("int", |s| s.parse::<u64>().is_ok());§Panics
When building an app with two matchers of the same name, or a route using an unknown one.
Sourcepub fn with_state(self, state: S) -> RouterApp<S>
pub fn with_state(self, state: S) -> RouterApp<S>
Builds the app, using the browser’s history on wasm and an in-memory history elsewhere.
Trait Implementations§
Auto Trait Implementations§
impl<S> !Freeze for Router<S>
impl<S> !RefUnwindSafe for Router<S>
impl<S> !Send for Router<S>
impl<S> !Sync for Router<S>
impl<S> !UnwindSafe for Router<S>
impl<S> Forget for Router<S>
impl<S> Move for Router<S>
impl<S> Unpin for Router<S>
impl<S> UnsafeUnpin for Router<S>where
Vec<(String, Page<S>)>: UnsafeUnpin,
Vec<(String, Router<S>)>: UnsafeUnpin,
Vec<Rc<dyn Fn(&Request, &S) -> Pin<Box<dyn Future<Output = Result<Box<dyn Any>, Rejection>>>>>>: UnsafeUnpin,
Option<Layout<S>>: UnsafeUnpin,
Option<Rc<dyn Fn(&mut Ui, &ViewContext<'_, S>)>>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more