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 will run the handler in current router or it’s descendants handle 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 will run the handler in current router or it’s descendants handle the request. This middleware is only effective when the filter returns true..

Source

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

Add a handler as middleware, it will run the handler in current router or it’s descendants handle 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 will run the handler in current router or it’s descendants handle the request. This middleware is only effective when the filter returns true..

Source

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

Create a new router and set path filter.

§Panics

Panics if path value is not in correct format.

Source

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

Create a new path filter for current router.

§Panics

Panics if path value is not in correct format.

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: Fn(&mut Request, &mut PathState) -> bool + Send + Sync + 'static,

Create a new router and set filter_fn.

Source

pub fn filter_fn<T>(self, func: T) -> Router
where T: Fn(&mut Request, &mut PathState) -> 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.

Source

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

When you want write router chain, this function will be useful, You can write your custom logic in FnOnce.

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.

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.

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

Auto Trait Implementations§

§

impl Freeze for Router

§

impl !RefUnwindSafe for Router

§

impl Send for Router

§

impl Sync for Router

§

impl Unpin for Router

§

impl !UnwindSafe for Router

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

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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: 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> 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