Router

Struct Router 

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

A router that can be used to route requests to their respective views.

This struct is used to route requests to their respective views. It can be created directly by calling the Router::with_urls method, and that’s what is typically done in cot::App::router implementations.

§Examples

use cot::request::Request;
use cot::response::Response;
use cot::router::{Route, Router};

async fn home(request: Request) -> cot::Result<Response> {
    unimplemented!()
}

let router = Router::with_urls([Route::with_handler_and_name("/", home, "home")]);

Implementations§

Source§

impl Router

Source

pub fn empty() -> Self

Create an empty router.

This router will not route any requests.

§Examples
use cot::router::Router;

let router = Router::empty();
Source

pub fn with_urls<T: Into<Vec<Route>>>(urls: T) -> Self

Create a router with the given routes.

§Examples
use cot::request::Request;
use cot::response::Response;
use cot::router::{Route, Router};

async fn home(request: Request) -> cot::Result<Response> {
    unimplemented!()
}

let router = Router::with_urls([Route::with_handler_and_name("/", home, "home")]);
Source

pub async fn handle(&self, request: Request) -> Result<Response>

Handle a request.

This method is called by the CotApp to handle a request.

§Errors

This method re-throws any errors that occur in the request handler.

Source

pub fn reverse( &self, app_name: Option<&str>, name: &str, params: &ReverseParamMap, ) -> Result<String>

Get a URL for a view by name.

Instead of using this method directly, consider using the reverse! macro which provides much more ergonomic way to call this.

app_name is the name of the app that the view should be found in. If app_name is None, the view will be searched for in any app.

§Errors

This method returns an error if the view name is not found.

This method returns an error if the URL cannot be generated because of missing parameters.

Source

pub fn reverse_option( &self, app_name: Option<&str>, name: &str, params: &ReverseParamMap, ) -> Result<Option<String>>

Get a URL for a view by name.

app_name is the name of the app that the view should be found in. If app_name is None, the view will be searched for in any app.

Returns None if the view name is not found.

§Errors

This method returns an error if the URL cannot be generated because of missing parameters.

Source

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

Get the routes in this router.

§Examples
use cot::request::Request;
use cot::response::Response;
use cot::router::{Route, Router};

async fn home(request: Request) -> cot::Result<Response> {
    unimplemented!()
}

let router = Router::with_urls([Route::with_handler_and_name("/", home, "home")]);
assert_eq!(router.routes().len(), 1);
Source

pub fn is_empty(&self) -> bool

Check if this router is empty.

§Examples
use cot::request::Request;
use cot::response::Response;
use cot::router::{Route, Router};

async fn home(request: Request) -> cot::Result<Response> {
    unimplemented!()
}

let router = Router::empty();
assert!(router.is_empty());

let router = Router::with_urls([Route::with_handler_and_name("/", home, "home")]);
assert!(!router.is_empty());
Source

pub fn as_api(&self) -> OpenApi

Available on crate feature openapi only.

Returns the OpenAPI paths for the router.

This might be useful if you want to manually serve the generated OpenAPI specs.

§Panics

Panics if invalid schemas are generated. This should not happen in normal operation, but if it does, it indicates a bug in the schemars library or in the way the OpenAPI specs are generated.

Trait Implementations§

Source§

impl Clone for Router

Source§

fn clone(&self) -> Router

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Router

Source§

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

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

impl Default for Router

Source§

fn default() -> Self

Returns the “default value” for a type. 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<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> Chain<T> for T

Source§

fn len(&self) -> usize

The number of items that this chain link consists of.
Source§

fn append_to(self, v: &mut Vec<T>)

Append the elements in this link to the chain.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Container<T> for T
where T: Clone,

Source§

type Iter = Once<T>

An iterator over the items within this container, by value.
Source§

fn get_iter(&self) -> <T as Container<T>>::Iter

Iterate over the elements of the container (using internal iteration because GATs are unstable).
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> Fake for T

Source§

fn fake<U>(&self) -> U
where Self: FakeBase<U>,

Source§

fn fake_with_rng<U, R>(&self, rng: &mut R) -> U
where R: Rng + ?Sized, Self: FakeBase<U>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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> IntoApi for T

Source§

fn into_api<A>(self) -> UseApi<T, A>

into UseApi
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> IntoField<Auto<T>> for T

Source§

fn into_field(self) -> Auto<T>

Available on crate feature db only.
Converts the type to the field type.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> OrderedContainer<T> for T
where T: Clone,