Skip to content

Commit 79b0067

Browse files
committed
add support for overlapping parameter suffixes
1 parent efb488e commit 79b0067

File tree

7 files changed

+280
-240
lines changed

7 files changed

+280
-240
lines changed

src/error.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ pub enum InsertError {
1212
/// The existing route that the insertion is conflicting with.
1313
with: String,
1414
},
15+
1516
/// Only one parameter per route segment is allowed.
1617
///
1718
/// For example, `/foo-{bar}` and `/{bar}-foo` are valid routes, but `/{foo}-{bar}`
1819
/// is not.
1920
InvalidParamSegment,
21+
2022
/// Parameters must be registered with a valid name and matching braces.
2123
///
2224
/// Note you can use `{{` or `}}` to escape literal brackets.
2325
InvalidParam,
26+
2427
/// Catch-all parameters are only allowed at the end of a path.
2528
InvalidCatchAll,
2629
}
@@ -31,8 +34,7 @@ impl fmt::Display for InsertError {
3134
Self::Conflict { with } => {
3235
write!(
3336
f,
34-
"Insertion failed due to conflict with previously registered route: {}",
35-
with
37+
"Insertion failed due to conflict with previously registered route: {with}"
3638
)
3739
}
3840
Self::InvalidParamSegment => {

src/escape.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl<'a> std::ops::Deref for UnescapedRef<'a> {
173173
}
174174
}
175175

176-
impl<'a> fmt::Debug for UnescapedRef<'a> {
176+
impl fmt::Debug for UnescapedRef<'_> {
177177
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
178178
f.debug_struct("UnescapedRef")
179179
.field("inner", &std::str::from_utf8(self.inner))

src/params.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::{fmt, iter, mem, slice};
44
#[derive(PartialEq, Eq, Ord, PartialOrd, Default, Copy, Clone)]
55
struct Param<'k, 'v> {
66
// Keys and values are stored as byte slices internally by the router
7-
// to avoid UTF8 checks when slicing, but UTF8 is still respected,
8-
// so these slices are valid strings.
7+
// to avoid utf8 checks when slicing. This allows us to perform utf8
8+
// validation lazily without resorting to unsafe code.
99
key: &'k [u8],
1010
value: &'v [u8],
1111
}
@@ -118,6 +118,7 @@ impl<'k, 'v> Params<'k, 'v> {
118118
/// Inserts a key value parameter pair into the list.
119119
pub(crate) fn push(&mut self, key: &'k [u8], value: &'v [u8]) {
120120
#[cold]
121+
#[inline(never)]
121122
fn drain_to_vec<T: Default>(len: usize, elem: T, arr: &mut [T; SMALL]) -> Vec<T> {
122123
let mut vec = Vec::with_capacity(len + 1);
123124
vec.extend(arr.iter_mut().map(mem::take));
@@ -136,6 +137,7 @@ impl<'k, 'v> Params<'k, 'v> {
136137
arr[*len] = param;
137138
*len += 1;
138139
}
140+
139141
ParamsKind::Large(vec) => vec.push(param),
140142
}
141143
}
@@ -149,6 +151,7 @@ impl<'k, 'v> Params<'k, 'v> {
149151
.map(|param| &mut param.key)
150152
.enumerate()
151153
.for_each(f),
154+
152155
ParamsKind::Large(vec) => vec
153156
.iter_mut()
154157
.map(|param| &mut param.key)
@@ -184,7 +187,7 @@ enum ParamsIterKind<'ps, 'k, 'v> {
184187
Large(slice::Iter<'ps, Param<'k, 'v>>),
185188
}
186189

187-
impl<'ps, 'k, 'v> Iterator for ParamsIter<'ps, 'k, 'v> {
190+
impl<'k, 'v> Iterator for ParamsIter<'_, 'k, 'v> {
188191
type Item = (&'k str, &'v str);
189192

190193
fn next(&mut self) -> Option<Self::Item> {

src/router.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ impl<T> Router<T> {
116116
/// assert_eq!(router.remove("/home/{id}/"), None);
117117
///
118118
/// router.insert("/home/{id}/", "Hello!");
119-
/// // the route does not match
119+
/// // The route does not match.
120120
/// assert_eq!(router.remove("/home/{user}"), None);
121121
/// assert_eq!(router.remove("/home/{id}/"), Some("Hello!"));
122122
///
123123
/// router.insert("/home/{id}/", "Hello!");
124-
/// // invalid route
124+
/// // Invalid route.
125125
/// assert_eq!(router.remove("/home/{id"), None);
126126
/// assert_eq!(router.remove("/home/{id}/"), Some("Hello!"));
127127
/// ```

0 commit comments

Comments
 (0)