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
95
pub mod route;

pub use route::{Parser, Route};

#[derive(Debug, Clone, PartialEq)]
pub struct Routes {
    routes: Vec<Route>,
}

impl Routes {
    pub fn new() -> Routes {
        Routes { routes: Vec::new() }
    }

    pub fn add(&mut self, route: Route) {
        self.routes.push(route);
    }

    pub fn find(&self, path: &str) -> Option<&Route> {
        self.routes.iter().find(|&route| route.check(path))
    }
}

impl Default for Routes {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use crate::route::ParamType;

    use super::*;

    #[test]
    fn test_routes() {
        let mut routes: Routes = Routes::new();
        let parser = Parser::default();

        let user_route = parser
            .route("user-by-id", "/user/<id:int>/")
            .expect("route should parse");
        let club_route = parser
            .route("club-by-id", "/club/<id:uuid>/")
            .expect("route should parse");
        let game_route = parser
            .route("game", "/game/<slug>/")
            .expect("route should parse");

        routes.add(user_route.clone());
        routes.add(club_route.clone());
        routes.add(game_route.clone());

        assert_eq!(None, routes.find("/user/abc/"));
        assert_eq!(Some(&user_route), routes.find("/user/123/"));
        assert_eq!(None, routes.find("/club//"));
        assert_eq!(None, routes.find("/club/abc/"));
        assert_eq!(None, routes.find("/club/123/"));
        assert_eq!(
            Some(&club_route),
            routes.find("/club/36be8705-6c31-45d7-9321-d56cc07b50d9/")
        );
        assert_eq!(Some(&game_route), routes.find("/game/123/"));
        assert_eq!(Some(&game_route), routes.find("/game//"));
        assert_eq!(Some(&game_route), routes.find("/game/abc/"));
        assert_eq!(None, routes.find("/game/123"));
    }

    #[test]
    fn test_parses_custom_types() {
        fn is_palindrome(ident: &str) -> bool {
            ident.chars().rev().collect::<String>() == ident
        }

        let palindromic_type = ParamType::new("palindrome", is_palindrome);

        let mut routes: Routes = Routes::new();
        let mut parser = Parser::default();
        parser.add_param_type(palindromic_type);

        let user_route = parser
            .route("user-by-id", "/user/<id:int>/")
            .expect("route should parse");
        let club_route = parser
            .route("club-by-id", "/club/<id:palindrome>/")
            .expect("route should parse");

        routes.add(user_route.clone());
        routes.add(club_route.clone());

        assert_eq!(None, routes.find("/club/myclub/"));
        assert_eq!(Some(&club_route), routes.find("/club/radar/"));
    }
}