pub struct Urls { /* private fields */ }Expand description
A helper structure to allow reversing URLs from a request handler.
This is mainly useful as an extractor to allow reversing URLs without
access to a full Request object.
§Examples
use cot::request::Request;
use cot::response::{Response, ResponseExt};
use cot::router::{Route, Router, Urls};
use cot::test::TestRequestBuilder;
use cot::{Body, RequestHandler, StatusCode, reverse};
async fn my_handler(urls: Urls) -> cot::Result<Response> {
let url = reverse!(urls, "home")?;
Ok(Response::new_html(
StatusCode::OK,
Body::fixed(format!("{url}")),
))
}
let router = Router::with_urls([Route::with_handler_and_name("/", my_handler, "home")]);
let request = TestRequestBuilder::get("/").router(router).build();
assert_eq!(
my_handler
.handle(request)
.await?
.into_body()
.into_bytes()
.await?,
"/"
);Implementations§
Source§impl Urls
impl Urls
Sourcepub fn from_request(request: &Request) -> Self
pub fn from_request(request: &Request) -> Self
Create a new Urls object from a Request object.
§Examples
use cot::request::Request;
use cot::response::{Response, ResponseExt};
use cot::router::Urls;
use cot::{Body, StatusCode, reverse};
async fn my_handler(request: Request) -> cot::Result<Response> {
let urls = Urls::from_request(&request);
let url = reverse!(urls, "home")?;
Ok(Response::new_html(
StatusCode::OK,
Body::fixed(format!("Hello! The URL for this view is: {}", url)),
))
}Sourcepub fn app_name(&self) -> Option<&str>
pub fn app_name(&self) -> Option<&str>
Get the app name the current route belongs to, or None if the
request is not routed.
This is mainly useful for providing context to reverse redirects, where you want to redirect to a route in the same app.
§Examples
use cot::request::{Request, RequestExt};
use cot::response::Response;
use cot::router::Urls;
async fn my_handler(urls: Urls) -> cot::Result<Response> {
let app_name = urls.app_name();
// ... do something with the app name
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Urls
impl !RefUnwindSafe for Urls
impl Send for Urls
impl Sync for Urls
impl Unpin for Urls
impl !UnwindSafe for Urls
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
Mutably borrows from an owned value. Read more
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>
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 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>
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 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>
Available on crate feature
db only.Converts the type to the field type.