This commit is contained in:
Ali MJ Al-Nasrawy 2021-06-27 12:12:16 +03:00
parent 20f349d9c8
commit aa339d9f4b
1 changed files with 20 additions and 14 deletions

View File

@ -56,7 +56,7 @@ impl ResourceDef {
let mut re_set = Vec::new();
for path in set {
let (pattern, _, _) = ResourceDef::parse(&path, false);
let (pattern, _, _) = ResourceDef::parse(&path, false, true);
let re = match Regex::new(&pattern) {
Ok(re) => re,
@ -116,7 +116,7 @@ impl ResourceDef {
/// Parse path pattern and create new `Pattern` instance with custom prefix
fn with_prefix(path: &str, for_prefix: bool) -> Self {
let path = path.to_owned();
let (pattern, elements, is_dynamic) = ResourceDef::parse(&path, for_prefix);
let (pattern, elements, is_dynamic) = ResourceDef::parse(&path, for_prefix, false);
let tp = if is_dynamic {
let re = match Regex::new(&pattern) {
@ -400,18 +400,17 @@ impl ResourceDef {
)
}
fn parse(mut pattern: &str, mut for_prefix: bool) -> (String, Vec<PatternElement>, bool) {
if pattern.find('{').is_none() {
return if let Some(path) = pattern.strip_suffix('*') {
let re = format!("{}^{}(.*)", REGEX_FLAGS, path);
(re, vec![PatternElement::Const(String::from(path))], true)
} else {
(
fn parse(
mut pattern: &str,
mut for_prefix: bool,
force_dynamic: bool,
) -> (String, Vec<PatternElement>, bool) {
if !force_dynamic && pattern.find('{').is_none() && !pattern.ends_with('*') {
return (
String::from(pattern),
vec![PatternElement::Const(String::from(pattern))],
false,
)
};
);
}
let mut elements = Vec::new();
@ -433,6 +432,13 @@ impl ResourceDef {
dyn_elements += 1;
}
if let Some(path) = pattern.strip_suffix('*') {
elements.push(PatternElement::Const(String::from(path)));
re.push_str(&escape(path));
re.push_str("(.*)");
pattern = "";
}
elements.push(PatternElement::Const(String::from(pattern)));
re.push_str(&escape(pattern));