Fix IdxSegment item for paths with no root

This commit is contained in:
Nikolay Kim 2020-04-06 12:51:50 +06:00
parent a7b6fa83a5
commit 740cb0fd33
4 changed files with 51 additions and 5 deletions

View file

@ -1,5 +1,9 @@
# Changes
## [0.3.2] - 2020-04-06
* Fix IdxSegment item for paths with no root
## [0.3.1] - 2020-04-06
* Fix url quoter

View file

@ -1,6 +1,6 @@
[package]
name = "ntex-router"
version = "0.3.1"
version = "0.3.2"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Path router"
keywords = ["ntex"]

View file

@ -890,9 +890,49 @@ mod tests {
assert_eq!(tree.find(&mut Path::new("/user/profile")), Some(1));
assert_eq!(tree.find(&mut Path::new("/user/profile/profile")), None);
let tree = Tree::new(&ResourceDef::new("{id}"), 1);
assert_eq!(tree.find(&mut Path::new("profile")), Some(1));
assert_eq!(tree.find(&mut Path::new("2345")), Some(1));
assert_eq!(tree.find(&mut Path::new("/2345/")), None);
assert_eq!(tree.find(&mut Path::new("/2345/sdg")), None);
let tree = Tree::new(&ResourceDef::new("{user}/profile/{no}"), 1);
assert_eq!(tree.find(&mut Path::new("user/profile/123")), Some(1));
assert_eq!(tree.find(&mut Path::new("/user/profile/123")), Some(1));
assert_eq!(tree.find(&mut Path::new("/user/profile/p/test/")), None);
let tree = Tree::new(&ResourceDef::new("v{version}/resource/{id}/test"), 1);
assert_eq!(
tree.find(&mut Path::new("v1/resource/320120/test")),
Some(1)
);
assert_eq!(tree.find(&mut Path::new("v/resource/1/test")), None);
let mut resource = Path::new("v151/resource/adahg32/test");
assert_eq!(tree.find(&mut resource), Some(1));
assert_eq!(resource.get("version").unwrap(), "151");
assert_eq!(resource.get("id").unwrap(), "adahg32");
let re = ResourceDef::new("v/{id:[[:digit:]]{6}}");
let tree = Tree::new(&re, 1);
assert_eq!(tree.find(&mut Path::new("v/012345")), Some(1));
assert_eq!(tree.find(&mut Path::new("v/012345/")), None);
assert_eq!(tree.find(&mut Path::new("v/012345/index")), None);
assert_eq!(tree.find(&mut Path::new("v/012")), None);
assert_eq!(tree.find(&mut Path::new("v/01234567")), None);
assert_eq!(tree.find(&mut Path::new("v/XXXXXX")), None);
let mut resource = Path::new("v/012345");
assert_eq!(tree.find(&mut resource), Some(1));
assert_eq!(resource.get("id").unwrap(), "012345");
let re = ResourceDef::new("u/test/v{version}-no-{minor}xx/resource/{id}/{name}");
let tree = Tree::new(&re, 1);
let mut resource = Path::new("u/test/v1-no-3xx/resource/320120/name");
assert_eq!(tree.find(&mut resource), Some(1));
assert_eq!(resource.get("version").unwrap(), "1");
assert_eq!(resource.get("minor").unwrap(), "3");
assert_eq!(resource.get("id").unwrap(), "320120");
assert_eq!(resource.get("name").unwrap(), "name");
}
}

View file

@ -200,7 +200,7 @@ impl Tree {
F: Fn(usize, &R) -> bool,
{
let path = resource.resource_path();
let base_skip = path.skip;
let mut base_skip = path.skip as isize;
let mut segments = mem::take(&mut path.segments);
let path = resource.path();
@ -230,6 +230,7 @@ impl Tree {
let path = if path.starts_with('/') {
&path[1..]
} else {
base_skip -= 1;
path
};
@ -267,6 +268,7 @@ impl Tree {
let path = if path.starts_with('/') {
&path[1..]
} else {
base_skip -= 1;
path
};
@ -296,7 +298,7 @@ impl Tree {
mut skip: usize,
segments: &mut Vec<(&'static str, PathItem)>,
insensitive: bool,
base_skip: u16,
base_skip: isize,
) -> Option<(usize, usize)>
where
T: ResourcePath,
@ -344,8 +346,8 @@ impl Tree {
PathItem::Segment(m.as_str().to_string())
} else {
PathItem::IdxSegment(
base_skip + (skip + m.start()) as u16,
base_skip + (skip + m.end()) as u16,
(base_skip + (skip + m.start()) as isize) as u16,
(base_skip + (skip + m.end()) as isize) as u16,
)
};
segments.push((name, item));