2016-03-21 04:27:17 +00:00
|
|
|
use std::path::Component;
|
2016-04-01 23:54:53 +00:00
|
|
|
use std::path::Path;
|
2016-03-18 02:43:58 +00:00
|
|
|
|
2016-03-21 04:27:17 +00:00
|
|
|
pub trait Collider<T: ?Sized = Self> {
|
2016-03-19 02:05:29 +00:00
|
|
|
fn collides_with(&self, other: &T) -> bool;
|
2016-03-18 02:43:58 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 05:04:39 +00:00
|
|
|
pub fn index_match_until(break_c: char, a: &str, b: &str, dir: bool)
|
|
|
|
-> Option<(isize, isize)> {
|
2016-03-19 02:05:29 +00:00
|
|
|
let (a_len, b_len) = (a.len() as isize, b.len() as isize);
|
|
|
|
let (mut i, mut j, delta) = if dir {
|
|
|
|
(0, 0, 1)
|
|
|
|
} else {
|
|
|
|
(a_len - 1, b_len - 1, -1)
|
|
|
|
};
|
|
|
|
|
|
|
|
while i >= 0 && j >= 0 && i < a_len && j < b_len {
|
|
|
|
let (c1, c2) = (a.char_at(i as usize), b.char_at(j as usize));
|
2016-03-21 04:27:17 +00:00
|
|
|
if c1 == break_c || c2 == break_c {
|
2016-03-19 02:05:29 +00:00
|
|
|
break;
|
|
|
|
} else if c1 != c2 {
|
2016-03-22 05:04:39 +00:00
|
|
|
return None;
|
2016-03-19 02:05:29 +00:00
|
|
|
} else {
|
|
|
|
i += delta;
|
|
|
|
j += delta;
|
|
|
|
}
|
|
|
|
}
|
2016-03-18 02:43:58 +00:00
|
|
|
|
2016-03-22 05:04:39 +00:00
|
|
|
return Some((i, j));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_match_until(break_c: char, a: &str, b: &str, dir: bool) -> bool {
|
|
|
|
index_match_until(break_c, a, b, dir).is_some()
|
2016-03-19 02:05:29 +00:00
|
|
|
}
|
2016-03-18 02:43:58 +00:00
|
|
|
|
2016-03-21 04:27:17 +00:00
|
|
|
macro_rules! comp_to_str {
|
|
|
|
($component:expr) => (
|
|
|
|
match $component {
|
|
|
|
&Component::Normal(ref comp) => {
|
|
|
|
if let Some(string) = comp.to_str() { string }
|
|
|
|
else { return true }
|
|
|
|
},
|
|
|
|
_ => return true
|
|
|
|
};
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-04-01 23:54:53 +00:00
|
|
|
impl Collider for Path {
|
|
|
|
// TODO: It's expensive to compute the number of components: O(n) per path
|
|
|
|
// where n == number of chars.
|
|
|
|
//
|
|
|
|
// Idea: Create a `CachedPath` type that caches the number of components
|
|
|
|
// similar to the way `Route` does it.
|
|
|
|
fn collides_with(&self, b: &Path) -> bool {
|
|
|
|
if self.components().count() != b.components().count() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut a_components = self.components();
|
|
|
|
let mut b_components = b.components();
|
|
|
|
while let Some(ref c1) = a_components.next() {
|
|
|
|
if let Some(ref c2) = b_components.next() {
|
|
|
|
if !c1.collides_with(c2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-19 02:05:29 +00:00
|
|
|
impl<'a> Collider for Component<'a> {
|
|
|
|
fn collides_with(&self, other: &Component<'a>) -> bool {
|
|
|
|
let (a, b) = (comp_to_str!(self), comp_to_str!(other));
|
2016-03-22 05:04:39 +00:00
|
|
|
do_match_until('<', a, b, true) && do_match_until('>', a, b, false)
|
2016-03-18 02:43:58 +00:00
|
|
|
}
|
2016-03-19 02:05:29 +00:00
|
|
|
}
|
2016-03-18 02:43:58 +00:00
|
|
|
|
2016-04-01 23:54:53 +00:00
|
|
|
impl<'a> Collider<str> for &'a str {
|
|
|
|
fn collides_with(&self, other: &str) -> bool {
|
|
|
|
Path::new(self).collides_with(Path::new(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-19 02:05:29 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-03-21 04:27:17 +00:00
|
|
|
use router::Collider;
|
|
|
|
use router::route::Route;
|
2016-03-19 02:05:29 +00:00
|
|
|
use Method;
|
|
|
|
use Method::*;
|
2016-03-22 05:04:39 +00:00
|
|
|
use {Request, Response};
|
2016-03-19 02:05:29 +00:00
|
|
|
|
2016-03-21 04:27:17 +00:00
|
|
|
type SimpleRoute = (Method, &'static str);
|
|
|
|
|
2016-03-22 05:04:39 +00:00
|
|
|
fn dummy_handler(_req: Request) -> Response<'static> {
|
|
|
|
Response::empty()
|
|
|
|
}
|
|
|
|
|
2016-03-21 04:27:17 +00:00
|
|
|
fn m_collide(a: SimpleRoute, b: SimpleRoute) -> bool {
|
2016-04-02 07:51:40 +00:00
|
|
|
let route_a = Route::new(a.0, a.1.to_string(), dummy_handler);
|
|
|
|
route_a.collides_with(&Route::new(b.0, b.1.to_string(), dummy_handler))
|
2016-03-19 02:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn collide(a: &'static str, b: &'static str) -> bool {
|
2016-04-02 07:51:40 +00:00
|
|
|
let route_a = Route::new(Get, a.to_string(), dummy_handler);
|
|
|
|
route_a.collides_with(&Route::new(Get, b.to_string(), dummy_handler))
|
2016-03-19 02:05:29 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 09:22:22 +00:00
|
|
|
fn s_r_collide(a: &'static str, b: &'static str) -> bool {
|
2016-04-02 07:51:40 +00:00
|
|
|
a.collides_with(&Route::new(Get, b.to_string(), dummy_handler))
|
2016-03-21 04:27:17 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 09:22:22 +00:00
|
|
|
fn r_s_collide(a: &'static str, b: &'static str) -> bool {
|
2016-04-02 07:51:40 +00:00
|
|
|
let route_a = Route::new(Get, a.to_string(), dummy_handler);
|
2016-03-21 09:22:22 +00:00
|
|
|
route_a.collides_with(b)
|
|
|
|
}
|
|
|
|
|
2016-04-01 23:54:53 +00:00
|
|
|
fn s_s_collide(a: &'static str, b: &'static str) -> bool {
|
|
|
|
a.collides_with(b)
|
|
|
|
}
|
|
|
|
|
2016-03-19 02:05:29 +00:00
|
|
|
#[test]
|
|
|
|
fn simple_collisions() {
|
|
|
|
assert!(collide("a", "a"));
|
|
|
|
assert!(collide("/a", "/a"));
|
|
|
|
assert!(collide("/hello", "/hello"));
|
|
|
|
assert!(collide("/hello", "/hello/"));
|
|
|
|
assert!(collide("/hello/there/how/ar", "/hello/there/how/ar"));
|
|
|
|
assert!(collide("/hello/there", "/hello/there/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_param_collisions() {
|
|
|
|
assert!(collide("/hello/<name>", "/hello/<person>"));
|
|
|
|
assert!(collide("/hello/<name>/hi", "/hello/<person>/hi"));
|
|
|
|
assert!(collide("/hello/<name>/hi/there", "/hello/<person>/hi/there"));
|
|
|
|
assert!(collide("/<name>/hi/there", "/<person>/hi/there"));
|
|
|
|
assert!(collide("/<name>/hi/there", "/dude/<name>/there"));
|
|
|
|
assert!(collide("/<name>/<a>/<b>", "/<a>/<b>/<c>"));
|
|
|
|
assert!(collide("/<name>/<a>/<b>/", "/<a>/<b>/<c>/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn medium_param_collisions() {
|
|
|
|
assert!(collide("/hello/<name>", "/hello/bob"));
|
|
|
|
assert!(collide("/<name>", "//bob"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hard_param_collisions() {
|
|
|
|
assert!(collide("/<name>bob", "/<name>b"));
|
|
|
|
assert!(collide("/a<b>c", "/abc"));
|
|
|
|
assert!(collide("/a<b>c", "/azooc"));
|
|
|
|
assert!(collide("/a<b>", "/a"));
|
|
|
|
assert!(collide("/<b>", "/a"));
|
|
|
|
assert!(collide("/<a>/<b>", "/a/b<c>"));
|
|
|
|
assert!(collide("/<a>/bc<b>", "/a/b<c>"));
|
|
|
|
assert!(collide("/<a>/bc<b>d", "/a/b<c>"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn non_collisions() {
|
|
|
|
assert!(!collide("/a", "/b"));
|
|
|
|
assert!(!collide("/a/b", "/a"));
|
|
|
|
assert!(!collide("/a/b", "/a/c"));
|
|
|
|
assert!(!collide("/a/hello", "/a/c"));
|
|
|
|
assert!(!collide("/hello", "/a/c"));
|
|
|
|
assert!(!collide("/hello/there", "/hello/there/guy"));
|
|
|
|
assert!(!collide("/b<a>/there", "/hi/there"));
|
|
|
|
assert!(!collide("/<a>/<b>c", "/hi/person"));
|
|
|
|
assert!(!collide("/<a>/<b>cd", "/hi/<a>e"));
|
|
|
|
assert!(!collide("/a<a>/<b>", "/b<b>/<a>"));
|
|
|
|
assert!(!collide("/a/<b>", "/b/<b>"));
|
|
|
|
assert!(!collide("/a<a>/<b>", "/b/<b>"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn method_dependent_non_collisions() {
|
|
|
|
assert!(!m_collide((Get, "/"), (Post, "/")));
|
|
|
|
assert!(!m_collide((Post, "/"), (Put, "/")));
|
|
|
|
assert!(!m_collide((Put, "/a"), (Put, "/")));
|
|
|
|
assert!(!m_collide((Post, "/a"), (Put, "/")));
|
|
|
|
assert!(!m_collide((Get, "/a"), (Put, "/")));
|
|
|
|
assert!(!m_collide((Get, "/hello"), (Put, "/hello")));
|
|
|
|
}
|
2016-03-21 04:27:17 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_str_non_collisions() {
|
2016-03-21 09:22:22 +00:00
|
|
|
assert!(!s_r_collide("/a", "/b"));
|
|
|
|
assert!(!s_r_collide("/a/b", "/a"));
|
|
|
|
assert!(!s_r_collide("/a/b", "/a/c"));
|
|
|
|
assert!(!s_r_collide("/a/hello", "/a/c"));
|
|
|
|
assert!(!s_r_collide("/hello", "/a/c"));
|
|
|
|
assert!(!s_r_collide("/hello/there", "/hello/there/guy"));
|
|
|
|
assert!(!s_r_collide("/b<a>/there", "/hi/there"));
|
|
|
|
assert!(!s_r_collide("/<a>/<b>c", "/hi/person"));
|
|
|
|
assert!(!s_r_collide("/<a>/<b>cd", "/hi/<a>e"));
|
|
|
|
assert!(!s_r_collide("/a<a>/<b>", "/b<b>/<a>"));
|
|
|
|
assert!(!s_r_collide("/a/<b>", "/b/<b>"));
|
|
|
|
assert!(!s_r_collide("/a<a>/<b>", "/b/<b>"));
|
|
|
|
assert!(!r_s_collide("/a", "/b"));
|
|
|
|
assert!(!r_s_collide("/a/b", "/a"));
|
|
|
|
assert!(!r_s_collide("/a/b", "/a/c"));
|
|
|
|
assert!(!r_s_collide("/a/hello", "/a/c"));
|
|
|
|
assert!(!r_s_collide("/hello", "/a/c"));
|
|
|
|
assert!(!r_s_collide("/hello/there", "/hello/there/guy"));
|
|
|
|
assert!(!r_s_collide("/b<a>/there", "/hi/there"));
|
|
|
|
assert!(!r_s_collide("/<a>/<b>c", "/hi/person"));
|
|
|
|
assert!(!r_s_collide("/<a>/<b>cd", "/hi/<a>e"));
|
|
|
|
assert!(!r_s_collide("/a<a>/<b>", "/b<b>/<a>"));
|
|
|
|
assert!(!r_s_collide("/a/<b>", "/b/<b>"));
|
|
|
|
assert!(!r_s_collide("/a<a>/<b>", "/b/<b>"));
|
2016-03-21 04:27:17 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 23:54:53 +00:00
|
|
|
#[test]
|
2016-03-21 04:27:17 +00:00
|
|
|
fn test_str_collisions() {
|
2016-04-01 23:54:53 +00:00
|
|
|
assert!(!s_s_collide("/a", "/b"));
|
|
|
|
assert!(!s_s_collide("/a/b", "/a"));
|
|
|
|
assert!(!s_s_collide("/a/b", "/a/c"));
|
|
|
|
assert!(!s_s_collide("/a/hello", "/a/c"));
|
|
|
|
assert!(!s_s_collide("/hello", "/a/c"));
|
|
|
|
assert!(!s_s_collide("/hello/there", "/hello/there/guy"));
|
|
|
|
assert!(!s_s_collide("/b<a>/there", "/hi/there"));
|
|
|
|
assert!(!s_s_collide("/<a>/<b>c", "/hi/person"));
|
|
|
|
assert!(!s_s_collide("/<a>/<b>cd", "/hi/<a>e"));
|
|
|
|
assert!(!s_s_collide("/a<a>/<b>", "/b<b>/<a>"));
|
|
|
|
assert!(!s_s_collide("/a/<b>", "/b/<b>"));
|
|
|
|
assert!(!s_s_collide("/a<a>/<b>", "/b/<b>"));
|
2016-03-21 04:27:17 +00:00
|
|
|
}
|
2016-03-19 02:05:29 +00:00
|
|
|
}
|