1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! type extractor for request uri path

use core::ops::Deref;

use crate::{body::BodyStream, context::WebContext, error::Error, handler::FromRequest};

#[derive(Debug)]
pub struct PathRef<'a>(pub &'a str);

impl Deref for PathRef<'_> {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.0
    }
}

impl<'a, 'r, C, B> FromRequest<'a, WebContext<'r, C, B>> for PathRef<'a>
where
    B: BodyStream,
{
    type Type<'b> = PathRef<'b>;
    type Error = Error<C>;

    #[inline]
    async fn from_request(ctx: &'a WebContext<'r, C, B>) -> Result<Self, Self::Error> {
        Ok(PathRef(ctx.req().uri().path()))
    }
}

#[derive(Debug)]
pub struct PathOwn(pub String);

impl Deref for PathOwn {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.0.as_str()
    }
}

impl<'a, 'r, C, B> FromRequest<'a, WebContext<'r, C, B>> for PathOwn
where
    B: BodyStream,
{
    type Type<'b> = PathOwn;
    type Error = Error<C>;

    #[inline]
    async fn from_request(ctx: &'a WebContext<'r, C, B>) -> Result<Self, Self::Error> {
        Ok(PathOwn(ctx.req().uri().path().to_string()))
    }
}