Skip to content

Commit

Permalink
normalize route parameters (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheemdev authored Sep 22, 2023
1 parent 007c71b commit 05a8c30
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 48 deletions.
9 changes: 8 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::tree::Node;
use crate::tree::{denormalize_params, Node};

use std::fmt;

Expand Down Expand Up @@ -49,12 +49,19 @@ impl InsertError {
route.extend_from_slice(&current.prefix);
}

let mut last = current;
while let Some(node) = last.children.first() {
last = node;
}

let mut current = current.children.first();
while let Some(node) = current {
route.extend_from_slice(&node.prefix);
current = node.children.first();
}

denormalize_params(&mut route, &last.param_remapping);

InsertError::Conflict {

Check warning on line 65 in src/error.rs

View workflow job for this annotation

GitHub Actions / Clippy Lints

unnecessary structure name repetition
with: String::from_utf8(route).unwrap(),
}
Expand Down
18 changes: 18 additions & 0 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@ impl<'k, 'v> Params<'k, 'v> {
ParamsKind::Large(vec) => vec.push(param),
}
}

// Transform each key.
pub(crate) fn for_each_key_mut(&mut self, f: impl Fn((usize, &mut &'k [u8]))) {
match &mut self.kind {
ParamsKind::None => {}
ParamsKind::Small(arr, len) => arr
.iter_mut()
.take(*len)
.map(|param| &mut param.key)
.enumerate()
.for_each(f),
ParamsKind::Large(vec) => vec
.iter_mut()
.map(|param| &mut param.key)
.enumerate()
.for_each(f),
}
}
}

/// An iterator over the keys and values of a route's [parameters](crate::Params).
Expand Down
Loading

0 comments on commit 05a8c30

Please sign in to comment.