pub struct Route { /* private fields */ }Expand description
A route that can be used to route requests to their respective views.
Non-empty route paths may omit the leading slash. Cot normalizes them by
prepending /, so "home" and "/home" define the same route.
§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 Route
impl Route
Sourcepub fn with_handler<HandlerParams, H>(url: &str, handler: H) -> Self
pub fn with_handler<HandlerParams, H>(url: &str, handler: H) -> Self
Create a new route with the given handler.
§Examples
use cot::request::Request;
use cot::response::Response;
use cot::router::{Route, Router};
async fn home(request: Request) -> cot::Result<Response> {
// ...
}
let route = Route::with_handler("home", home);
assert_eq!(route.url(), "/home");Sourcepub fn with_api_handler<HandlerParams, H>(url: &str, handler: H) -> Selfwhere
HandlerParams: 'static,
H: RequestHandler<HandlerParams> + AsApiRoute + Send + Sync + 'static,
Available on crate feature openapi only.
pub fn with_api_handler<HandlerParams, H>(url: &str, handler: H) -> Selfwhere
HandlerParams: 'static,
H: RequestHandler<HandlerParams> + AsApiRoute + Send + Sync + 'static,
openapi only.Create a new route with the given handler for inclusion in the OpenAPI specifications.
See crate::openapi module documentation for more details on how to
generate OpenAPI specifications automatically.
§Examples
use cot::request::Request;
use cot::response::Response;
use cot::router::method::openapi::api_get;
use cot::router::{Route, Router};
async fn home(request: Request) -> cot::Result<Response> {
// ...
}
let route = Route::with_api_handler("/", api_get(home));Sourcepub fn with_handler_and_name<N, HandlerParams, H>(
url: &str,
handler: H,
name: N,
) -> Selfwhere
N: Into<String>,
HandlerParams: 'static,
H: RequestHandler<HandlerParams> + Send + Sync + 'static,
pub fn with_handler_and_name<N, HandlerParams, H>(
url: &str,
handler: H,
name: N,
) -> Selfwhere
N: Into<String>,
HandlerParams: 'static,
H: RequestHandler<HandlerParams> + Send + Sync + 'static,
Create a new route with the given handler and name.
§Examples
use cot::request::Request;
use cot::response::Response;
use cot::router::method::openapi::api_get;
use cot::router::{Route, Router};
async fn home(request: Request) -> cot::Result<Response> {
// ...
}
let route = Route::with_handler_and_name("/", api_get(home), "home");Sourcepub fn with_api_handler_and_name<N, HandlerParams, H>(
url: &str,
handler: H,
name: N,
) -> Selfwhere
N: Into<String>,
HandlerParams: 'static,
H: RequestHandler<HandlerParams> + AsApiRoute + Send + Sync + 'static,
Available on crate feature openapi only.
pub fn with_api_handler_and_name<N, HandlerParams, H>(
url: &str,
handler: H,
name: N,
) -> Selfwhere
N: Into<String>,
HandlerParams: 'static,
H: RequestHandler<HandlerParams> + AsApiRoute + Send + Sync + 'static,
openapi only.Create a new route with the given handler and name for inclusion in the OpenAPI specs.
See crate::openapi module documentation for more details on how to
generate OpenAPI specs automatically.
§Examples
use cot::request::Request;
use cot::response::Response;
use cot::router::method::openapi::api_post;
use cot::router::{Route, Router};
async fn home(request: Request) -> cot::Result<Response> {
// ...
}
let route = Route::with_api_handler_and_name("/", api_post(home), "home");Sourcepub fn with_router(url: &str, router: Router) -> Self
pub fn with_router(url: &str, router: Router) -> Self
Create a new route with the given 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")]);
let route = Route::with_router("/", router);Sourcepub fn url(&self) -> String
pub fn url(&self) -> String
Get the URL for this route.
§Examples
use cot::request::Request;
use cot::response::Response;
use cot::router::{Route, Router};
async fn home(request: Request) -> cot::Result<Response> {
unimplemented!()
}
let route = Route::with_handler("/test", home);
assert_eq!(route.url(), "/test");Sourcepub fn name(&self) -> Option<&str>
pub fn name(&self) -> Option<&str>
Get the name of this route, if it was created with the
Self::with_handler_and_name function.
§Examples
use cot::request::Request;
use cot::response::Response;
use cot::router::{Route, Router};
async fn home(request: Request) -> cot::Result<Response> {
unimplemented!()
}
let route = Route::with_handler_and_name("/", home, "home");
assert_eq!(route.name(), Some("home"));Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Route
impl !UnwindSafe for Route
impl Forget for Route
impl Freeze for Route
impl Send for Route
impl Sync for Route
impl Unpin for Route
impl UnsafeUnpin for Route
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
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.