diff --git a/src/app.rs b/src/app.rs index 06ff799..7b200e4 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,3 +1,4 @@ +use anyhow::Error; use fancy_regex::Regex as FancyRegex; use ignore::WalkState; use itertools::Itertools; @@ -321,6 +322,22 @@ impl SearchFields { }; Ok(result) } + + pub fn path_pattern_parsed(&self) -> anyhow::Result> { + let path_patt_text = &self.path_pattern().text; + let result = if path_patt_text.is_empty() { + None + } else { + Some({ + if self.advanced_regex { + SearchType::PatternAdvanced(FancyRegex::new(path_patt_text)?) + } else { + SearchType::Pattern(Regex::new(path_patt_text)?) + } + }) + }; + Ok(result) + } } enum ValidatedField { @@ -627,13 +644,18 @@ impl App { }) } + fn is_regex_error(e: &Error) -> bool { + e.downcast_ref::().is_some() + || e.downcast_ref::().is_some() + } + fn validate_fields( &mut self, background_processing_sender: UnboundedSender, ) -> anyhow::Result> { let search_pattern = match self.search_fields.search_type() { Err(e) => { - if e.downcast_ref::().is_some() { + if Self::is_regex_error(&e) { self.search_fields .search_mut() .set_error("Couldn't parse regex".to_owned(), e.to_string()); @@ -645,19 +667,14 @@ impl App { Ok(p) => ValidatedField::Parsed(p), }; - let path_pattern_text = self.search_fields.path_pattern().text(); - let path_pattern = if path_pattern_text.is_empty() { - ValidatedField::Parsed(None) - } else { - match Regex::new(path_pattern_text.as_str()) { - Err(e) => { - self.search_fields - .path_pattern_mut() - .set_error("Couldn't parse regex".to_owned(), e.to_string()); - ValidatedField::Error - } - Ok(r) => ValidatedField::Parsed(Some(r)), + let path_pattern = match self.search_fields.path_pattern_parsed() { + Err(e) => { + self.search_fields + .path_pattern_mut() + .set_error("Couldn't parse regex".to_owned(), e.to_string()); + ValidatedField::Error } + Ok(r) => ValidatedField::Parsed(r), }; let (search_pattern, path_pattern) = match (search_pattern, path_pattern) { diff --git a/src/parsed_fields.rs b/src/parsed_fields.rs index ea467bf..8236b51 100644 --- a/src/parsed_fields.rs +++ b/src/parsed_fields.rs @@ -26,7 +26,7 @@ pub enum SearchType { pub struct ParsedFields { search_pattern: SearchType, replace_string: String, - path_pattern: Option, + path_pattern: Option, // TODO: `root_dir` and `include_hidden` are duplicated across this and App root_dir: PathBuf, include_hidden: bool, @@ -38,7 +38,7 @@ impl ParsedFields { pub fn new( search_pattern: SearchType, replace_string: String, - path_pattern: Option, + path_pattern: Option, root_dir: PathBuf, include_hidden: bool, background_processing_sender: UnboundedSender, @@ -55,7 +55,14 @@ impl ParsedFields { pub fn handle_path(&self, path: &Path) { if let Some(ref p) = self.path_pattern { - let matches_pattern = p.is_match(relative_path_from(&self.root_dir, path).as_str()); + let relative_path = relative_path_from(&self.root_dir, path); + let relative_path = relative_path.as_str(); + + let matches_pattern = match p { + SearchType::Pattern(ref p) => p.is_match(relative_path), + SearchType::PatternAdvanced(ref p) => p.is_match(relative_path).unwrap(), + SearchType::Fixed(ref s) => relative_path.contains(s), + }; if !matches_pattern { return; }