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
impl Router
Sourcepub fn empty() -> Self
pub fn empty() -> Self
Create an empty router.
This router will not route any requests.
§Examples
use cot::router::Router;
let router = Router::empty();Sourcepub fn with_urls<T: Into<Vec<Route>>>(urls: T) -> Self
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")]);Sourcepub fn reverse(
&self,
app_name: Option<&str>,
name: &str,
params: &ReverseParamMap,
) -> Result<String>
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.
Sourcepub fn reverse_option(
&self,
app_name: Option<&str>,
name: &str,
params: &ReverseParamMap,
) -> Result<Option<String>>
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.
Sourcepub fn routes(&self) -> &[Route]
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);Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn as_api(&self) -> OpenApi
Available on crate feature openapi only.
pub fn as_api(&self) -> OpenApi
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> IntoField<Auto<T>> for T
impl<T> IntoField<Auto<T>> for T
Source§fn into_field(self) -> Auto<T>
fn into_field(self) -> Auto<T>
db only.