xitca_web/handler/types/
uri.rs

1use core::ops::Deref;
2
3use crate::{context::WebContext, error::Error, handler::FromRequest, http::Uri};
4
5#[derive(Debug)]
6pub struct UriRef<'a>(pub &'a Uri);
7
8impl Deref for UriRef<'_> {
9    type Target = Uri;
10
11    fn deref(&self) -> &Self::Target {
12        self.0
13    }
14}
15
16impl<'a, 'r, C, B> FromRequest<'a, WebContext<'r, C, B>> for UriRef<'a> {
17    type Type<'b> = UriRef<'b>;
18    type Error = Error;
19
20    #[inline]
21    async fn from_request(ctx: &'a WebContext<'r, C, B>) -> Result<Self, Self::Error> {
22        Ok(UriRef(ctx.req().uri()))
23    }
24}
25
26impl<'a, 'r, C, B> FromRequest<'a, WebContext<'r, C, B>> for &'a Uri {
27    type Type<'b> = &'b Uri;
28    type Error = Error;
29
30    #[inline]
31    async fn from_request(ctx: &'a WebContext<'r, C, B>) -> Result<Self, Self::Error> {
32        Ok(ctx.req().uri())
33    }
34}
35
36impl<'a, 'r, C, B> FromRequest<'a, WebContext<'r, C, B>> for Uri {
37    type Type<'b> = Uri;
38    type Error = Error;
39
40    #[inline]
41    async fn from_request(ctx: &'a WebContext<'r, C, B>) -> Result<Self, Self::Error> {
42        Ok(ctx.req().uri().clone())
43    }
44}
45
46#[derive(Debug)]
47pub struct UriOwn(pub Uri);
48
49impl Deref for UriOwn {
50    type Target = Uri;
51
52    fn deref(&self) -> &Self::Target {
53        &self.0
54    }
55}
56
57impl<'a, 'r, C, B> FromRequest<'a, WebContext<'r, C, B>> for UriOwn {
58    type Type<'b> = UriOwn;
59    type Error = Error;
60
61    #[inline]
62    async fn from_request(ctx: &'a WebContext<'r, C, B>) -> Result<Self, Self::Error> {
63        Ok(UriOwn(ctx.req().uri().clone()))
64    }
65}