Skip to content

Commit

Permalink
Use IntoPattern for prefix resources
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Mar 16, 2021
1 parent 5eaaca9 commit c448c0b
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 17 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.4.2] - 2021-03-16

* Use `IntoPattern` for prefix resources

## [0.4.1] - 2021-02-26

* Fix: partial matches keep garbage in segments
Expand Down
12 changes: 6 additions & 6 deletions ntex-router/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-router"
version = "0.4.1"
version = "0.4.2"
authors = ["ntex contributors <[email protected]>"]
description = "Path router"
keywords = ["ntex"]
Expand All @@ -17,12 +17,12 @@ path = "src/lib.rs"
default = ["http"]

[dependencies]
regex = "1.4.1"
serde = "1.0.116"
regex = "1.4"
serde = "1.0"
bytestring = "1.0"
log = "0.4.8"
http = { version = "0.2.1", optional = true }
log = "0.4"
http = { version = "0.2", optional = true }

[dev-dependencies]
http = "0.2.1"
http = "0.2"
serde_derive = "1.0"
103 changes: 92 additions & 11 deletions ntex-router/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,22 @@ impl ResourceDef {
/// Use `prefix` type instead of `static`.
///
/// Panics if path regex pattern is malformed.
pub fn prefix(path: &str) -> Self {
ResourceDef::with_prefix(path, true)
pub fn prefix<T: IntoPattern>(path: T) -> Self {
ResourceDef::with_prefix(path)
}

/// Parse path pattern and create new `ResourceDef` instance.
/// Inserts `/` to the start of the pattern.
///
/// Panics if path regex pattern is malformed.
pub fn root_prefix(path: &str) -> Self {
ResourceDef::with_prefix(&insert_slash(path), true)
pub fn root_prefix<T: IntoPattern>(path: T) -> Self {
let mut patterns = path.patterns();
for path in &mut patterns {
let p = insert_slash(path.as_str());
*path = p
}

ResourceDef::with_prefix(patterns)
}

/// Resource id
Expand All @@ -145,17 +151,27 @@ impl ResourceDef {
}

/// Parse path pattern and create new `Pattern` instance with custom prefix
fn with_prefix(path: &str, for_prefix: bool) -> Self {
let path = path.to_owned();
let (tp, elements) = ResourceDef::parse(&path);
fn with_prefix<T: IntoPattern>(path: T) -> Self {
let patterns = path.patterns();

let mut p = String::new();
let mut tp = Vec::new();
let mut elements = Vec::new();

for path in patterns {
p = path.clone();
let (pelems, elems) = ResourceDef::parse(&path);
tp.push(pelems);
elements = elems;
}

ResourceDef {
tp,
elements,
id: 0,
tp: vec![tp],
name: String::new(),
pattern: path,
prefix: for_prefix,
pattern: p,
prefix: true,
}
}

Expand Down Expand Up @@ -806,12 +822,28 @@ mod tests {
assert_eq!(tree.find(&mut Path::new("/name")), None);
assert_eq!(tree.find(&mut Path::new("/name1")), None);

let tree = Tree::new(&ResourceDef::prefix(vec!["/name/", "/name2/"]), 1);
assert_eq!(tree.find(&mut Path::new("/name/")), Some(1));
assert_eq!(tree.find(&mut Path::new("/name/test/test")), Some(1));
assert_eq!(tree.find(&mut Path::new("/name2/")), Some(1));
assert_eq!(tree.find(&mut Path::new("/name2/test/test")), Some(1));
assert_eq!(tree.find(&mut Path::new("/name")), None);
assert_eq!(tree.find(&mut Path::new("/name1")), None);

let tree = Tree::new(&ResourceDef::root_prefix("name/"), 1);
assert_eq!(tree.find(&mut Path::new("/name/")), Some(1));
assert_eq!(tree.find(&mut Path::new("/name/test/test")), Some(1));
assert_eq!(tree.find(&mut Path::new("/name")), None);
assert_eq!(tree.find(&mut Path::new("/name1")), None);

let tree = Tree::new(&ResourceDef::root_prefix(vec!["name/", "name2/"]), 1);
assert_eq!(tree.find(&mut Path::new("/name/")), Some(1));
assert_eq!(tree.find(&mut Path::new("/name/test/test")), Some(1));
assert_eq!(tree.find(&mut Path::new("/name2/")), Some(1));
assert_eq!(tree.find(&mut Path::new("/name2/test/test")), Some(1));
assert_eq!(tree.find(&mut Path::new("/name")), None);
assert_eq!(tree.find(&mut Path::new("/name1")), None);

let mut resource = Path::new("/name/subpath1/subpath2/index.html");
assert_eq!(tree.find(&mut resource), Some(1));
assert_eq!(resource.path(), "/subpath1/subpath2/index.html");
Expand Down Expand Up @@ -844,12 +876,30 @@ mod tests {
assert_eq!(tree.find(&mut Path::new("/name1")), Some(1));
assert_eq!(tree.find(&mut Path::new("/name~")), Some(1));

let mut resource = Path::new("/test2/subpath1/subpath2/index.html");
let tree = Tree::new(&ResourceDef::prefix(vec!["/1/{name}/", "/2/{name}/"]), 1);
assert_eq!(tree.find(&mut Path::new("/1/name/")), Some(1));
assert_eq!(tree.find(&mut Path::new("/1/name/test/test")), Some(1));
assert_eq!(tree.find(&mut Path::new("/2/name/")), Some(1));
assert_eq!(tree.find(&mut Path::new("/2/name/test/test")), Some(1));
assert_eq!(tree.find(&mut Path::new("/1/name")), None);
assert_eq!(tree.find(&mut Path::new("/1/name1")), None);
assert_eq!(tree.find(&mut Path::new("/1/name~")), None);
assert_eq!(tree.find(&mut Path::new("/2/name")), None);
assert_eq!(tree.find(&mut Path::new("/2/name1")), None);
assert_eq!(tree.find(&mut Path::new("/2/name~")), None);

let mut resource = Path::new("/1/test2/subpath1/subpath2/index.html");
assert_eq!(tree.find(&mut resource), Some(1));
assert_eq!(&resource["name"], "test2");
assert_eq!(&resource[0], "test2");
assert_eq!(resource.path(), "/subpath1/subpath2/index.html");

let mut resource = Path::new("/2/test3/subpath1/subpath2/index.html");
assert_eq!(tree.find(&mut resource), Some(1));
assert_eq!(&resource["name"], "test3");
assert_eq!(&resource[0], "test3");
assert_eq!(resource.path(), "/subpath1/subpath2/index.html");

// nested
let mut tree = Tree::new(&ResourceDef::prefix("/prefix/{v1}/second/{v2}"), 1);
tree.insert(&ResourceDef::prefix("/prefix/{v1}"), 2);
Expand All @@ -863,6 +913,37 @@ mod tests {
assert_eq!(tree.find(&mut resource), Some(2));
assert_eq!(&resource["v1"], "1");
assert_eq!(tree.find(&mut Path::new("/prefix/1")), Some(2));

// nested
let mut tree = Tree::new(
&ResourceDef::prefix(vec![
"/prefix/{v1}/second/{v2}",
"/prefix2/{v1}/second/{v2}",
]),
1,
);
tree.insert(&ResourceDef::prefix("/prefix/{v1}"), 2);
tree.insert(&ResourceDef::prefix("/prefix2/{v1}"), 3);

let mut resource = Path::new("/prefix/1/second/2");
assert_eq!(tree.find(&mut resource), Some(1));
assert_eq!(&resource["v1"], "1");
assert_eq!(&resource["v2"], "2");

let mut resource = Path::new("/prefix2/1/second/2");
assert_eq!(tree.find(&mut resource), Some(1));
assert_eq!(&resource["v1"], "1");
assert_eq!(&resource["v2"], "2");

let mut resource = Path::new("/prefix/1/second");
assert_eq!(tree.find(&mut resource), Some(2));
assert_eq!(&resource["v1"], "1");
assert_eq!(tree.find(&mut Path::new("/prefix/1")), Some(2));

let mut resource = Path::new("/prefix2/1/second");
assert_eq!(tree.find(&mut resource), Some(3));
assert_eq!(&resource["v1"], "1");
assert_eq!(tree.find(&mut Path::new("/prefix2/1")), Some(3));
}

#[test]
Expand Down

0 comments on commit c448c0b

Please sign in to comment.