Skip to main content

eternal/router/
mod.rs

1//! Resource path matching library.
2mod de;
3mod path;
4mod resource;
5mod router;
6
7pub use self::de::PathDeserializer;
8pub use self::path::Path;
9pub use self::resource::ResourceDef;
10pub use self::router::{ResourceInfo, Router, RouterBuilder};
11
12use crate::util::bytestring;
13
14pub trait Resource<T: ResourcePath> {
15    fn resource_path(&mut self) -> &mut Path<T>;
16}
17
18pub trait ResourcePath {
19    fn path(&self) -> &str;
20}
21
22impl ResourcePath for String {
23    fn path(&self) -> &str {
24        self.as_str()
25    }
26}
27
28impl<'a> ResourcePath for &'a str {
29    fn path(&self) -> &str {
30        self
31    }
32}
33
34impl ResourcePath for bytestring::ByteString {
35    fn path(&self) -> &str {
36        &*self
37    }
38}
39
40/// Helper trait for type that could be converted to path pattern
41pub trait IntoPattern {
42    /// Signle patter
43    fn is_single(&self) -> bool;
44
45    fn patterns(&self) -> Vec<String>;
46}
47
48impl IntoPattern for String {
49    fn is_single(&self) -> bool {
50        true
51    }
52
53    fn patterns(&self) -> Vec<String> {
54        vec![self.clone()]
55    }
56}
57
58impl<'a> IntoPattern for &'a String {
59    fn is_single(&self) -> bool {
60        true
61    }
62
63    fn patterns(&self) -> Vec<String> {
64        vec![self.as_str().to_string()]
65    }
66}
67
68impl<'a> IntoPattern for &'a str {
69    fn is_single(&self) -> bool {
70        true
71    }
72
73    fn patterns(&self) -> Vec<String> {
74        vec![self.to_string()]
75    }
76}
77
78impl<T: AsRef<str>> IntoPattern for Vec<T> {
79    fn is_single(&self) -> bool {
80        self.len() == 1
81    }
82
83    fn patterns(&self) -> Vec<String> {
84        self.into_iter().map(|v| v.as_ref().to_string()).collect()
85    }
86}
87
88macro_rules! array_patterns (($tp:ty, $num:tt) => {
89    impl IntoPattern for [$tp; $num] {
90        fn is_single(&self) -> bool {
91            $num == 1
92        }
93
94        fn patterns(&self) -> Vec<String> {
95            self.iter().map(|v| v.to_string()).collect()
96        }
97    }
98});
99
100array_patterns!(&str, 1);
101array_patterns!(&str, 2);
102array_patterns!(&str, 3);
103array_patterns!(&str, 4);
104array_patterns!(&str, 5);
105array_patterns!(&str, 6);
106array_patterns!(&str, 7);
107array_patterns!(&str, 8);
108array_patterns!(&str, 9);
109array_patterns!(&str, 10);
110array_patterns!(&str, 11);
111array_patterns!(&str, 12);
112array_patterns!(&str, 13);
113array_patterns!(&str, 14);
114array_patterns!(&str, 15);
115array_patterns!(&str, 16);
116
117array_patterns!(String, 1);
118array_patterns!(String, 2);
119array_patterns!(String, 3);
120array_patterns!(String, 4);
121array_patterns!(String, 5);
122array_patterns!(String, 6);
123array_patterns!(String, 7);
124array_patterns!(String, 8);
125array_patterns!(String, 9);
126array_patterns!(String, 10);
127array_patterns!(String, 11);
128array_patterns!(String, 12);
129array_patterns!(String, 13);
130array_patterns!(String, 14);
131array_patterns!(String, 15);
132array_patterns!(String, 16);
133
134mod url;
135
136pub use self::url::{Quoter, Url};
137
138mod http_support {
139    use super::ResourcePath;
140    use http::Uri;
141
142    impl ResourcePath for Uri {
143        fn path(&self) -> &str {
144            self.path()
145        }
146    }
147}