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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! A fork of [matchit](https://github.com/ibraheemdev/matchit) using small string type for params lifetime elision.
//!
//!```rust
//!use xitca_router::Router;
//!
//!fn main() -> Result<(), Box<dyn core::error::Error>> {
//! let mut router = Router::new();
//! router.insert("/home", "Welcome!")?;
//! router.insert("/users/{id}", "A User")?;
//!
//! let matched = router.at("/users/978")?;
//! assert_eq!(matched.params.get("id"), Some("978"));
//! assert_eq!(*matched.value, "A User");
//!
//! Ok(())
//!}
//!```
//!
//!# Parameters
//!
//!The router supports dynamic route segments. These can either be named or catch-all parameters.
//!
//!Named parameters like `/{id}` match anything until the next static segment or the end of the path.
//!
//!```rust
//!# use xitca_router::Router;
//!# fn main() -> Result<(), Box<dyn core::error::Error>> {
//!let mut router = Router::new();
//!router.insert("/users/{id}", 42)?;
//!
//!let matched = router.at("/users/1")?;
//!assert_eq!(matched.params.get("id"), Some("1"));
//!
//!let matched = router.at("/users/23")?;
//!assert_eq!(matched.params.get("id"), Some("23"));
//!
//!assert!(router.at("/users").is_err());
//!# Ok(())
//!# }
//!```
//!
//!Prefixes and suffixes within a segment are also supported. However, there may only be a single named parameter per route segment.
//!```rust
//!# use xitca_router::Router;
//!# fn main() -> Result<(), Box<dyn core::error::Error>> {
//!let mut router = Router::new();
//!router.insert("/images/img-{id}.png", true)?;
//!
//!let matched = router.at("/images/img-1.png")?;
//!assert_eq!(matched.params.get("id"), Some("1"));
//!
//!assert!(router.at("/images/img-1.jpg").is_err());
//!# Ok(())
//!# }
//!```
//!
//!Catch-all parameters start with a `*` and match anything until the end of the path. They must always be at the *end* of the route.
//!
//!```rust
//!# use xitca_router::Router;
//!# fn main() -> Result<(), Box<dyn core::error::Error>> {
//!let mut router = Router::new();
//!router.insert("/{*rest}", true)?;
//!
//!let matched = router.at("/foo.html")?;
//!assert_eq!(matched.params.get("rest"), Some("foo.html"));
//!
//!let matched = router.at("/static/bar.css")?;
//!assert_eq!(matched.params.get("rest"), Some("static/bar.css"));
//!
//!// Note that this would lead to an empty parameter value.
//!assert!(router.at("/").is_err());
//!# Ok(())
//!# }
//!```
//!
//!Relaxed Catch-all Parameters
//!Relaxed Catch-all parameters with a single `*` and match everything after the `/`(Including `/` itself). They must always be at the **end** of the route:
//!
//! ```rust
//!# use xitca_router::Router;
//!# fn main() -> Result<(), Box<dyn core::error::Error>> {
//! let mut m = Router::new();
//! m.insert("/{*}", true)?;
//!
//! assert!(m.at("/")?.value);
//! assert!(m.at("/foo")?.value);
//!
//! # Ok(())
//! # }
//! ```
//!
//!The literal characters `{` and `}` may be included in a static route by escaping them with the same character. For example, the `{` character is escaped with `{{`, and the `}` character is escaped with `}}`.
//!
//!```rust
//!# use xitca_router::Router;
//!# fn main() -> Result<(), Box<dyn core::error::Error>> {
//!let mut router = Router::new();
//!router.insert("/{{hello}}", true)?;
//!router.insert("/{hello}", true)?;
//!
//!// Match the static route.
//!let matched = router.at("/{hello}")?;
//!assert!(matched.params.is_empty());
//!
//!// Match the dynamic route.
//!let matched = router.at("/hello")?;
//!assert_eq!(matched.params.get("hello"), Some("hello"));
//!# Ok(())
//!# }
//!```
//!
//!# Conflict Rules
//!
//!Static and dynamic route segments are allowed to overlap. If they do, static segments will be given higher priority:
//!
//!```rust
//!# use xitca_router::Router;
//!# fn main() -> Result<(), Box<dyn core::error::Error>> {
//!let mut router = Router::new();
//!router.insert("/", "Welcome!").unwrap(); // Priority: 1
//!router.insert("/about", "About Me").unwrap(); // Priority: 1
//!router.insert("/{*filepath}", "...").unwrap(); // Priority: 2
//!# Ok(())
//!# }
//!```
//!
//!Formally, a route consists of a list of segments separated by `/`, with an optional leading and trailing slash: `(/)<segment_1>/.../<segment_n>(/)`.
//!
//!Given set of routes, their overlapping segments may include, in order of priority:
//!
//!- Any number of static segments (`/a`, `/b`, ...).
//!- *One* of the following:
//! - Any number of route parameters with a suffix (`/{x}a`, `/{x}b`, ...), prioritizing the longest suffix.
//! - Any number of route parameters with a prefix (`/a{x}`, `/b{x}`, ...), prioritizing the longest prefix.
//! - A single route parameter with both a prefix and a suffix (`/a{x}b`).
//!- *One* of the following;
//! - A single standalone parameter (`/{x}`).
//! - A single standalone catch-all parameter (`/{*rest}`). Note this only applies to the final route segment.
//!
//!Any other combination of route segments is considered ambiguous, and attempting to insert such a route will result in an error.
//!
//!The one exception to the above set of rules is that catch-all parameters are always considered to conflict with suffixed route parameters, i.e. that `/{*rest}`
//!and `/{x}suffix` are overlapping. This is due to an implementation detail of the routing tree that may be relaxed in the future.
pub use ;
pub use ;
extern crate alloc;
// TODO: consider no alloc alternative of these types so alloc can become an optional feature
use ;
use SmallBoxedStr as SmallStr;