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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::fmt;

use http::{Method, StatusCode};
use http_error::HttpError;
use serde::Deserialize;

use crate::body::Body;
use crate::IntoResponse;

pub type Request = http::Request<Body>;

pub trait RequestExt: Sized {
    fn route(&self) -> Route<'_>;
    fn query<'de, T: Deserialize<'de>>(&'de self) -> Result<T, DecodeQueryError>;
}

pub struct Route<'a> {
    method: &'a Method,
    segments: Vec<&'a str>,
}

#[derive(Debug, thiserror::Error)]
#[error("invalid query string")]
pub struct DecodeQueryError(#[from] serde_urlencoded::de::Error);

impl<'a> Route<'a> {
    pub fn to_tuple(&'a self) -> (Method, &'a [&'a str]) {
        (self.method.clone(), self.segments.as_slice())
    }
}

impl RequestExt for Request {
    fn route(&self) -> Route<'_> {
        let path = cleanup_path(self.uri().path());

        Route {
            method: self.method(),
            segments: if path.is_empty() {
                Vec::new()
            } else {
                path.split('/').collect()
            },
        }
    }

    fn query<'de, T: Deserialize<'de>>(&'de self) -> Result<T, DecodeQueryError> {
        let uri = self.uri();
        let query = uri.query();
        serde_urlencoded::from_str(query.unwrap_or("")).map_err(DecodeQueryError)
    }
}

fn cleanup_path(segment: &str) -> &str {
    // remove leading `/`
    let segment = segment.strip_prefix('/').unwrap_or(segment);
    // remove trailing `/`
    let segment = segment.strip_suffix('/').unwrap_or(segment);

    segment
}

impl IntoResponse for DecodeQueryError {
    fn into_response(self) -> crate::Response {
        StatusCode::BAD_REQUEST.into_response()
    }
}

impl HttpError for DecodeQueryError {
    fn status_code(&self) -> StatusCode {
        StatusCode::BAD_REQUEST
    }

    fn reason(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("invalid query string")
    }
}

#[cfg(test)]
mod tests {
    use http::Method;
    use hyper::body::Incoming;

    use super::RequestExt;
    use crate::body::Body;

    #[test]
    fn test_root_path() {
        let req = http::Request::builder()
            .uri("https://example.org/")
            .body(Body::<Incoming, hyper::Error>::empty())
            .unwrap();
        assert_eq!(req.route().to_tuple(), (Method::GET, &[] as &[&str]));
    }
}