ntex_router/
lib.rs

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