Skip to main content

Router

Struct Router 

Source
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>

Source

pub fn new() -> Self

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn layout(self, layout: Layout<S>) -> Self

Sets the layout wrapping all pages of this router.

§Panics

When a layout was already set.

Source

pub fn error<V, U>(self, view: V) -> Self
where 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).

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.

Source

pub fn hook<H, T, E>(self, hook: H) -> Self
where H: Handler<T, S, Output = Result<(), E>>, E: Into<Rejection>,

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.

Source

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.

Source

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.

Source

pub fn with_state_and_history( self, state: S, history: impl History + 'static, ) -> RouterApp<S>

Builds the app with a custom History.

§Panics

When routes conflict or refer to unknown matchers or router names.

Trait Implementations§

Source§

impl<S: Clone + 'static> Default for Router<S>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Unpin for Router<S>
where Vec<(String, Page<S>)>: Unpin, Vec<(String, Router<S>)>: Unpin, Vec<Rc<dyn Fn(&Request, &S) -> Pin<Box<dyn Future<Output = Result<Box<dyn Any>, Rejection>>>>>>: Unpin, Option<Layout<S>>: Unpin, Option<Rc<dyn Fn(&mut Ui, &ViewContext<'_, S>)>>: Unpin,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromRef<S> for S

Source§

fn from_ref(state: &S) -> &S

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.