Change rank meaning: lower means higher priority.

This commit is contained in:
Sergio Benitez 2016-08-26 19:03:21 -07:00
parent a1ad05e879
commit 860b302793
2 changed files with 11 additions and 3 deletions

View File

@ -43,7 +43,7 @@ impl Router {
for route in routes.iter().filter(|r| r.collides_with(req)) { for route in routes.iter().filter(|r| r.collides_with(req)) {
info_!("Matched: {}", route); info_!("Matched: {}", route);
if let Some(existing_route) = matched_route { if let Some(existing_route) = matched_route {
if route.rank > existing_route.rank { if route.rank < existing_route.rank {
matched_route = Some(route); matched_route = Some(route);
} }
} else { } else {
@ -217,6 +217,8 @@ mod test {
#[test] #[test]
fn test_ranking() { fn test_ranking() {
let a = Route::ranked(1, Get, "a/<b>", dummy_handler);
let b = Route::ranked(2, Get, "a/<b>", dummy_handler);
// FIXME: Add tests for non-default ranks. // FIXME: Add tests for non-default ranks.
} }

View File

@ -13,10 +13,16 @@ pub struct Route {
pub method: Method, pub method: Method,
pub handler: Handler, pub handler: Handler,
pub path: URIBuf, pub path: URIBuf,
pub rank: isize, pub rank: isize, // Lower ranks have higher priorities.
pub content_type: ContentType, pub content_type: ContentType,
} }
fn default_rank(path: &str) -> isize {
// The rank for a given path is 0 if it is a static route (it doesn't
// contain any dynamic <segmants>) or 1 if it is dynamic.
path.contains('<') as isize
}
impl Route { impl Route {
pub fn ranked<S>(rank: isize, m: Method, path: S, handler: Handler) pub fn ranked<S>(rank: isize, m: Method, path: S, handler: Handler)
-> Route where S: AsRef<str> { -> Route where S: AsRef<str> {
@ -33,7 +39,7 @@ impl Route {
Route { Route {
method: m, method: m,
handler: handler, handler: handler,
rank: (!path.as_ref().contains('<') as isize), rank: default_rank(path.as_ref()),
path: URIBuf::from(path.as_ref()), path: URIBuf::from(path.as_ref()),
content_type: ContentType::any(), content_type: ContentType::any(),
} }