Skip to main content

Router

Struct Router 

Source
#[non_exhaustive]
pub struct Router { pub routers: Vec<Router>, pub filters: Vec<Box<dyn Filter>>, pub hoops: Vec<Arc<dyn Handler>>, pub goal: Option<Arc<dyn Handler>>, /* private fields */ }
Expand description

Route request to different handlers.

View module level documentation for more details.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§routers: Vec<Router>

The children of current router.

§filters: Vec<Box<dyn Filter>>

The filters of current router.

§hoops: Vec<Arc<dyn Handler>>

The middlewares of current router.

§goal: Option<Arc<dyn Handler>>

The final handler to handle request of current router.

Implementations§

Source§

impl Router

Source

pub fn new() -> Router

Create a new Router.

Source

pub fn routers(&self) -> &Vec<Router>

Get current router’s children reference.

Source

pub fn routers_mut(&mut self) -> &mut Vec<Router>

Get current router’s children mutable reference.

Source

pub fn hoops(&self) -> &Vec<Arc<dyn Handler>>

Get current router’s middlewares reference.

Source

pub fn hoops_mut(&mut self) -> &mut Vec<Arc<dyn Handler>>

Get current router’s middlewares mutable reference.

Source

pub fn filters(&self) -> &Vec<Box<dyn Filter>>

Get current router’s filters reference.

Source

pub fn filters_mut(&mut self) -> &mut Vec<Box<dyn Filter>>

Get current router’s filters mutable reference.

Source

pub async fn detect( &self, req: &mut Request, path_state: &mut PathState<'_>, ) -> Option<DetectMatched>

Detect current router is matched for current request.

Source

pub fn unshift(self, router: Router) -> Router

Insert a router at the beginning of current router, shifting all routers after it to the right.

Source

pub fn insert(self, index: usize, router: Router) -> Router

Insert a router at position index within current router, shifting all routers after it to the right.

Source

pub fn push(self, router: Router) -> Router

Push a router as child of current router.

Source

pub fn append(self, others: &mut Vec<Router>) -> Router

Append all routers in a Vec as children of current router.

Source

pub fn with_hoop<H>(hoop: H) -> Router
where H: Handler,

Add a handler as middleware. It runs the handler in the current router or its descendants when handling the request.

Source

pub fn with_hoop_when<H, F>(hoop: H, filter: F) -> Router
where H: Handler, F: Fn(&Request, &Depot) -> bool + Send + Sync + 'static,

Add a handler as middleware. It runs the handler in the current router or its descendants when handling the request, but only when the filter returns true.

Source

pub fn hoop<H>(self, hoop: H) -> Router
where H: Handler,

Add a handler as middleware. It runs the handler in the current router or its descendants when handling the request.

Source

pub fn hoop_when<H, F>(self, hoop: H, filter: F) -> Router
where H: Handler, F: Fn(&Request, &Depot) -> bool + Send + Sync + 'static,

Add a handler as middleware. It runs the handler in the current router or its descendants when handling the request, but only when the filter returns true.

Source

pub fn with_path(path: impl Into<String>) -> Router

Create a new router and set path filter.

Invalid path patterns are logged and converted into a filter that never matches. Use Router::try_with_path to handle malformed patterns explicitly.

Source

pub fn try_with_path(path: impl Into<String>) -> Result<Router, String>

Try creating a new router and set path filter.

§Errors

Returns an error when the path pattern is malformed.

Source

pub fn path(self, path: impl Into<String>) -> Router

Create a new path filter for current router.

Invalid path patterns are logged and converted into a filter that never matches. Use Router::try_path to handle malformed patterns explicitly.

Source

pub fn try_path(self, path: impl Into<String>) -> Result<Router, String>

Try creating a new path filter for current router.

§Errors

Returns an error when the path pattern is malformed.

Source

pub fn with_filter(filter: impl Filter) -> Router

Create a new router and set filter.

Source

pub fn filter(self, filter: impl Filter) -> Router

Add a filter for current router.

Source

pub fn with_filter_fn<T>(func: T) -> Router
where T: for<'a> Fn(&mut Request, &mut PathState<'a>) -> bool + Send + Sync + 'static,

Create a new router and set filter_fn.

Source

pub fn filter_fn<T>(self, func: T) -> Router
where T: for<'a> Fn(&mut Request, &mut PathState<'a>) -> bool + Send + Sync + 'static,

Create a new FnFilter from Fn.

Source

pub fn goal<H>(self, goal: H) -> Router
where H: Handler,

Sets current router’s handler.

Calling goal on a router that already has one replaces the previous handler (a warning is logged, since this usually indicates route-building code overwriting itself by accident). Note that the method helpers (get, post, …) do not set this router’s goal — each pushes a filtered child router with its own goal.

Source

pub fn then<F>(self, func: F) -> Router
where F: FnOnce(Router) -> Router,

Runs a closure with this router and returns the closure result. Useful for composing router chains conditionally.

Source

pub fn scheme(self, scheme: Scheme) -> Router

Add a SchemeFilter to current router.

Source

pub fn host(self, host: impl Into<String>) -> Router

Add a HostFilter to current router.

Source

pub fn with_host(host: impl Into<String>) -> Router

Create a new HostFilter and set host filter.

Source

pub fn port(self, port: u16) -> Router

Add a PortFilter to current router.

Source

pub fn with_port(port: u16) -> Router

Create a new PortFilter and set port filter.

Source

pub fn get<H>(self, goal: H) -> Router
where H: Handler,

Creates a new child router with MethodFilter to filter GET method and set this child router’s handler.

Each method helper (get, post, …) pushes a filtered child router; it does not set the goal of self. Two consequences worth knowing:

  • Router::with_path("x").get(a).post(b) builds one path router with two method children — the usual pattern.
  • .get(a).get(b) registers two sibling GET routes rather than replacing a; the first match wins, so b is unreachable.
Source

pub fn post<H>(self, goal: H) -> Router
where H: Handler,

Create a new child router with MethodFilter to filter post method and set this child router’s handler.

See get for how method helpers compose as child routers.

Source

pub fn put<H>(self, goal: H) -> Router
where H: Handler,

Create a new child router with MethodFilter to filter put method and set this child router’s handler.

Source

pub fn query<H>(self, goal: H) -> Router
where H: Handler,

Create a new child router with MethodFilter to filter QUERY method and set this child router’s handler.

QUERY is a safe, idempotent method that can carry request content, making it suitable for complex queries that do not fit well in a URI query string.

Source

pub fn delete<H>(self, goal: H) -> Router
where H: Handler,

Create a new child router with MethodFilter to filter delete method and set this child router’s handler.

Source

pub fn patch<H>(self, goal: H) -> Router
where H: Handler,

Create a new child router with MethodFilter to filter patch method and set this child router’s handler.

Source

pub fn head<H>(self, goal: H) -> Router
where H: Handler,

Create a new child router with MethodFilter to filter head method and set this child router’s handler.

Source

pub fn options<H>(self, goal: H) -> Router
where H: Handler,

Create a new child router with MethodFilter to filter options method and set this child router’s handler.

Trait Implementations§

Source§

impl Debug for Router

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Router

Source§

fn default() -> Router

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

impl RouterExt for Router

Source§

fn oapi_security(self, security: SecurityRequirement) -> Router

Add security requirement to the router. Read more
Source§

fn oapi_securities<I>(self, iter: I) -> Router

Add security requirements to the router. Read more
Source§

fn oapi_tag(self, tag: impl Into<String>) -> Router

Add tag to the router. Read more
Source§

fn oapi_tags<I, V>(self, iter: I) -> Router
where I: IntoIterator<Item = V>, V: Into<String>,

Add tags to the router. Read more
Source§

impl SendTarget for Router

Source§

async fn call(self, req: Request) -> Response

Send request to target, such as Router, Service, Handler.

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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