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