Skip to content

Commit

Permalink
Handle non-prefix and prefix resources combinations
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed May 9, 2020
1 parent 4c053c1 commit a7187c4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ntex-router/CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [0.3.5] - 2020-05-09

* Handle non-prefix and prefix resources combinations

## [0.3.4] - 2020-04-17

* Add ResourcePath impl for &T where T: ResourcePath
Expand Down
2 changes: 1 addition & 1 deletion ntex-router/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-router"
version = "0.3.4"
version = "0.3.5"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Path router"
keywords = ["ntex"]
Expand Down
15 changes: 15 additions & 0 deletions ntex-router/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,4 +943,19 @@ mod tests {
assert_eq!(resource.get("id").unwrap(), "320120");
assert_eq!(resource.get("name").unwrap(), "name");
}

#[test]
fn test_recursive() {
let mut tree = Tree::new(&ResourceDef::new("/name"), 1);
tree.insert(&ResourceDef::new("/name/"), 2);
tree.insert(&ResourceDef::new("/name/index.html"), 3);
tree.insert(&ResourceDef::prefix("/"), 4);

assert_eq!(tree.find(&mut Path::new("/name")), Some(1));
assert_eq!(tree.find(&mut Path::new("/name/")), Some(2));
assert_eq!(tree.find(&mut Path::new("/name/index.html")), Some(3));
assert_eq!(tree.find(&mut Path::new("/")), Some(4));
assert_eq!(tree.find(&mut Path::new("/test")), Some(4));
assert_eq!(tree.find(&mut Path::new("/test/index.html")), Some(4));
}
}
33 changes: 32 additions & 1 deletion ntex-router/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,16 @@ impl Tree {
};
self.children.push(child);
}

// update value if key is the same
if p == key.len() {
self.value.push(value);
match value {
Value::PrefixSlesh(v) => {
self.children
.push(Tree::child(Vec::new(), Some(Value::Prefix(v))));
}
value => self.value.push(value),
}
} else {
// insert into sub tree
let mut child = self
Expand Down Expand Up @@ -316,6 +323,30 @@ impl Tree {
R: Resource<T>,
F: Fn(usize, &R) -> bool,
{
if self.key.is_empty() {
if path.is_empty() {
for val in &self.value {
let v = match val {
Value::Val(v) | Value::Prefix(v) => *v,
_ => continue,
};
if check(v, resource) {
return Some((v, skip));
}
}
} else {
for val in &self.value {
let v = match val {
Value::Prefix(v) => *v,
_ => continue,
};
if check(v, resource) {
return Some((v, skip));
}
}
}
return None;
}
let mut key: &[_] = &self.key;

loop {
Expand Down

0 comments on commit a7187c4

Please sign in to comment.