1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-31 08:12:07 +01:00

add ResourceDef::root_prefix, insert slash to the beggining of the pattern

This commit is contained in:
Nikolay Kim 2019-03-05 21:03:53 -08:00
parent 1153715149
commit 25f1eae51f

View File

@ -64,6 +64,17 @@ impl ResourceDef {
ResourceDef::with_prefix(path, true) ResourceDef::with_prefix(path, true)
} }
/// Parse path pattern and create new `Pattern` instance.
/// Inserts `/` to begging of the pattern.
///
///
/// Use `prefix` type instead of `static`.
///
/// Panics if path regex pattern is wrong.
pub fn root_prefix(path: &str) -> Self {
ResourceDef::with_prefix(&insert_slash(path), true)
}
/// Construct external resource def /// Construct external resource def
/// ///
/// Panics if path pattern is malformed. /// Panics if path pattern is malformed.
@ -313,6 +324,14 @@ impl From<String> for ResourceDef {
} }
} }
pub(crate) fn insert_slash(path: &str) -> String {
let mut path = path.to_owned();
if !path.is_empty() && !path.starts_with('/') {
path.insert(0, '/');
};
path
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -408,6 +427,11 @@ mod tests {
assert!(re.is_match("/name/")); assert!(re.is_match("/name/"));
assert!(re.is_match("/name/gs")); assert!(re.is_match("/name/gs"));
assert!(!re.is_match("/name")); assert!(!re.is_match("/name"));
let re = ResourceDef::root_prefix("name/");
assert!(re.is_match("/name/"));
assert!(re.is_match("/name/gs"));
assert!(!re.is_match("/name"));
} }
#[test] #[test]