Skip to main content

Router

Struct Router 

Source
pub struct Router { /* private fields */ }
Expand description

The route table under declaration.

Implementations§

Source§

impl Router

Source

pub fn resource(&mut self, name: &str, controller: Arc<dyn ResourceController>)

Register all seven RESTful routes for name.

Source

pub fn api_resource( &mut self, name: &str, controller: Arc<dyn ResourceController>, )

Register the five API routes for name — everything except the create and edit form pages.

Source

pub fn resource_actions( &mut self, name: &str, controller: Arc<dyn ResourceController>, actions: &[ResourceAction], )

Register exactly the listed actions.

They are always registered in ResourceAction::ALL order regardless of the order given, because /posts/create must be declared before /posts/{post} or the parameter swallows it.

Source§

impl Router

Source

pub fn new() -> Router

An empty router.

Source

pub fn add<H, Args>( &mut self, methods: Vec<Method>, uri: impl Into<String>, handler: H, ) -> &mut Route
where H: IntoRouteHandler<Args>,

Register a route for methods, returning it for further configuration.

Source

pub fn add_erased( &mut self, methods: Vec<Method>, uri: impl Into<String>, handler: Arc<dyn RouteHandler>, ) -> &mut Route

Register a route from an already-erased handler.

The path resource routes and other generated routes take, where the handler was built dynamically rather than inferred from a function signature.

Source

pub fn get<H, Args>(&mut self, uri: impl Into<String>, handler: H) -> &mut Route
where H: IntoRouteHandler<Args>,

GET (and HEAD).

Source

pub fn post<H, Args>( &mut self, uri: impl Into<String>, handler: H, ) -> &mut Route
where H: IntoRouteHandler<Args>,

POST.

Source

pub fn put<H, Args>(&mut self, uri: impl Into<String>, handler: H) -> &mut Route
where H: IntoRouteHandler<Args>,

PUT.

Source

pub fn patch<H, Args>( &mut self, uri: impl Into<String>, handler: H, ) -> &mut Route
where H: IntoRouteHandler<Args>,

PATCH.

Source

pub fn delete<H, Args>( &mut self, uri: impl Into<String>, handler: H, ) -> &mut Route
where H: IntoRouteHandler<Args>,

DELETE.

Source

pub fn options<H, Args>( &mut self, uri: impl Into<String>, handler: H, ) -> &mut Route
where H: IntoRouteHandler<Args>,

OPTIONS.

Source

pub fn any<H, Args>(&mut self, uri: impl Into<String>, handler: H) -> &mut Route
where H: IntoRouteHandler<Args>,

Every common method.

Source

pub fn redirect( &mut self, from: impl Into<String>, to: impl Into<String>, ) -> &mut Route

A permanent redirect from from to to.

Source

pub fn fallback<H, Args>(&mut self, handler: H)
where H: IntoRouteHandler<Args>,

The handler for requests that match nothing.

Without one, an unmatched request is a plain 404.

Source

pub fn group( &mut self, attributes: GroupAttributes, declare: impl FnOnce(&mut Router), )

Declare routes that share a prefix, a name prefix, middleware, or parameter constraints.

Groups nest: the attributes are applied to every route the closure added, after any inner group has applied its own — so the outer prefix ends up outermost and the outer middleware runs first.

Source

pub fn merge(&mut self, other: Router)

Merge another router’s routes into this one, keeping their order.

Source

pub fn routes(&self) -> &[Route]

Every declared route, in declaration order.

Source

pub fn routes_mut(&mut self) -> &mut [Route]

Mutable access to the declared routes.

Source

pub fn len(&self) -> usize

How many routes are declared.

Source

pub fn is_empty(&self) -> bool

Whether no routes are declared.

Source

pub fn uri_for(&self, name: &str) -> Option<&str>

The URI pattern of a named route.

Source

pub fn named_routes(&self) -> Vec<(&str, &str)>

Every named route, as (name, uri).

Source

pub fn describe(&self) -> Vec<RouteSummary>

The route table, without compiling anything.

CompiledRouter::describe needs a container, because compiling builds every middleware stage — and a deferred stage that resolves a service will fail if that service is not bound. That is the right behaviour before serving traffic and the wrong behaviour for a question as harmless as “what routes are there?”.

So route:list, a documentation generator and a test asserting on the table can all ask here. The middleware column is the declared labels, which is what those callers wanted to read anyway.

Source

pub fn compile(self, container: &Container) -> Result<CompiledRouter, Error>

Build every route’s pipeline.

Takes no registry: a route holds its middleware, so there is nothing to look up. It takes the container, because this is where every deferred stage is built — the point in the lifecycle where the container is populated and no request has arrived yet.

Trait Implementations§

Source§

impl Default for Router

Source§

fn default() -> Router

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

Auto Trait Implementations§

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> Event for T
where T: Send + Sync + 'static,

Source§

fn event_name() -> &'static str
where Self: Sized,

A human-readable name, used by wildcard listeners and log output. Always the Rust type path; see the trait docs.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more