little_hyper/path/
mod.rs

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
pub mod path_regex;
use crate::query::{QueryData, QUERY_START_WITH};

/// Path
///
///
/// ```fn my_path(Path(path): Path<MyStruct>) { }```
// #[derive(Debug)]
// pub struct Path<T>(T);

// impl<T> Extractor<T> for Path<T> {
//     fn inner(&self) -> &T {
//         &self.0
//     }
// }

#[derive(Debug)]
pub struct PathData {
    pub path: String,
    pub pathname: String,
    pub querystring: String,
    pub query: QueryData,
}

impl PathData {
    pub fn new(path: &str) -> Self {
        let path = sanitize_path(path);

        let (pathname, querystring) = path.split_once(QUERY_START_WITH).unwrap_or((&path, &""));

        Self {
            path: path.to_string(),
            pathname: pathname.to_string(),
            querystring: querystring.to_string(),
            query: QueryData::from(querystring),
        }
    }
}

// remove noise of path
pub fn sanitize_path(path: &str) -> String {
    let path = path.replace('\\', "/");
    let path = path.replace('/', "/");
    let mut path = path.replace("/?", "?");

    if path.len() > 1 && path.ends_with('/') {
        path.pop();
    }

    path
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sanitize_path() {
        assert_eq!(sanitize_path("/abc/xyz/"), "/abc/xyz");
        assert_eq!(sanitize_path("/abc/1/"), "/abc/1");
        assert_eq!(sanitize_path("/abc/?a=1"), "/abc?a=1");
    }

    #[test]
    fn test_path_data() {
        let path_one = PathData::new("/users/?userId=10");

        assert!(path_one.path == "/users?userId=10");
        assert!(path_one.pathname == "/users");
        assert!(path_one.querystring == "userId=10");
        assert!(path_one.query.len() == 1);
    }
}