mirror of https://github.com/rwf2/Rocket.git
Change rank meaning: lower means higher priority.
This commit is contained in:
parent
a1ad05e879
commit
860b302793
|
@ -43,7 +43,7 @@ impl Router {
|
|||
for route in routes.iter().filter(|r| r.collides_with(req)) {
|
||||
info_!("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);
|
||||
}
|
||||
} else {
|
||||
|
@ -217,6 +217,8 @@ mod test {
|
|||
|
||||
#[test]
|
||||
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.
|
||||
}
|
||||
|
||||
|
|
|
@ -13,10 +13,16 @@ pub struct Route {
|
|||
pub method: Method,
|
||||
pub handler: Handler,
|
||||
pub path: URIBuf,
|
||||
pub rank: isize,
|
||||
pub rank: isize, // Lower ranks have higher priorities.
|
||||
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 {
|
||||
pub fn ranked<S>(rank: isize, m: Method, path: S, handler: Handler)
|
||||
-> Route where S: AsRef<str> {
|
||||
|
@ -33,7 +39,7 @@ impl Route {
|
|||
Route {
|
||||
method: m,
|
||||
handler: handler,
|
||||
rank: (!path.as_ref().contains('<') as isize),
|
||||
rank: default_rank(path.as_ref()),
|
||||
path: URIBuf::from(path.as_ref()),
|
||||
content_type: ContentType::any(),
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue