1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 07:53:00 +01:00

simplify has_prefixed_route()

This commit is contained in:
Nikolay Kim 2018-07-16 11:10:51 +06:00
parent bce05e4fcb
commit b7a3fce17b

View File

@ -1,7 +1,6 @@
use std::cmp::min; use std::cmp::min;
use std::collections::HashMap; use std::collections::HashMap;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::path::Path;
use std::rc::Rc; use std::rc::Rc;
use regex::{escape, Regex}; use regex::{escape, Regex};
@ -159,19 +158,11 @@ impl ResourceInfo {
/// It will not match against prefix in case it's not given. For example for `/name` /// It will not match against prefix in case it's not given. For example for `/name`
/// with a `/test` prefix would return `false` /// with a `/test` prefix would return `false`
pub fn has_prefixed_route(&self, path: &str) -> bool { pub fn has_prefixed_route(&self, path: &str) -> bool {
if self.prefix == 0 { let prefix = self.prefix as usize;
return self.has_route(path); if prefix >= path.len() {
return false;
} }
self.has_route(&path[prefix..])
let path_matcher = Path::new(if path.is_empty() { "/" } else { path });
let router_prefix = Path::new(&path[..(self.prefix as usize)]);
if let Ok(p) = path_matcher.strip_prefix(router_prefix) {
if let Some(p_str) = p.to_str() {
return self.has_route(&format!("/{}", p_str));
}
}
false
} }
} }