string-patterns 0.4.0

Makes it easier to work with common string patterns and regular expressions in Rust, adding convenient regex match and replace methods (pattern_match and pattern_replace) to the standard String type as well to vectors of strings
Documentation
use crate::pattern_cache::with_cached_regex;
use regex::Error;

/// Core regular expression match methods
pub trait PatternMatch {
  /// Apply a regular expression match on the current string
  /// If the regex doesn't compile it will return an error
  fn pattern_match_result(&self, pattern: &str, case_insensitive: bool) -> Result<bool, Error>;

  /// Apply a regular expression match on the current string with a boolean case_insensitive flag
  /// NB: If the regex doesn't compile it will return false
  fn pattern_match(&self, pattern: &str, case_insensitive: bool) -> bool {
    if let Ok(matched) = self.pattern_match_result(pattern, case_insensitive){
      matched
    } else {
      false
    }
  }

  /// if the pattern does not match the source string or the regex fails
  fn pattern_match_ci(&self, pattern: &str) -> bool {
    self.pattern_match(pattern, true)
  }

  /// Simple case-sensitive regex-compatible match method that will return false
  /// if the pattern does not match the source string or the regex fails
  fn pattern_match_cs(&self, pattern: &str) -> bool {
    self.pattern_match(pattern, false)
  }

}

/// Implement regular expression match methods for any string-like type (&str, String,
/// &String, `Cow<str>`, etc.). The regex is compiled only once per distinct
/// (pattern, case_insensitive) pair for the life of the process — see `cached_regex` —
/// so calling this repeatedly with the same pattern, e.g. once per row of a large batch,
/// never recompiles after the first call.
impl<T: AsRef<str>> PatternMatch for T {

  ///
  /// Simple regex-compatible match method that will return an optional boolean
  /// - Some(true) means the regex is valid and the string matches
  /// - Some(false) means the regex is valid and the string does not match
  /// - None means the regex is not valid and can this not be evaluated
  /// Only the pattern_match_result needs to be implemented
  fn pattern_match_result(&self, pattern: &str, case_insensitive: bool) -> Result<bool, Error> {
    with_cached_regex(pattern, case_insensitive, |re| re.is_match(self.as_ref()))
  }
}

/// Boolean methods to match a pattern within an array of any string-like type
impl<T: AsRef<str>> PatternMatch for [T] {
  /// The regex is only compiled once across all elements, and only once ever per distinct
  /// (pattern, case_insensitive) pair across all calls, thanks to the shared regex cache.
  fn pattern_match_result(&self, pattern: &str, case_insensitive: bool) -> Result<bool, Error> {
    with_cached_regex(pattern, case_insensitive, |re| {
      self.iter().any(|segment| re.is_match(segment.as_ref()))
    })
  }
}

/// Pattern methods for arrays or vectors only, return vectors of booleans matching each input string
pub trait PatternMatches {

  /// Returns result with a vector of tuples with matched status and string slice
  /// for an array or vector of strings with a case-insensitive flag
  /// or an error if the regex does not compile
  fn pattern_matched_pairs_result(&self, pattern: &str, case_insensitive: bool) -> Result<Vec<(bool, &str)>, Error>;


  /// Return a default vector of paired tuples if the regular expression fails in pattern_matched_pairs or pattern_matches
  /// This has to implemented separately for all other derived methods returning vectors to work correctly
  fn pattern_matched_pairs_default(&self) -> Vec<(bool, &str)>;

  /// Returns a vector of tuples with matched status and string slice
  /// for an array or vector of strings with a case-insensitive flag
  /// If the regular expression fails all items will be false
  fn pattern_matched_pairs(&self, pattern: &str, case_insensitive: bool) -> Vec<(bool, &str)> {
    match self.pattern_matched_pairs_result(pattern, case_insensitive) {
      Ok(results) => results,
      Err(_error) => self.pattern_matched_pairs_default()
    }
  }

  /// Returns result with a vector of boolean matches for an array or vector of strings with case-insensitive flag
  /// or an error if the regex does not compile
  fn pattern_matches_result(&self, pattern: &str, case_insensitive: bool) -> Result<Vec<bool>, Error> {
    match self.pattern_matched_pairs_result(pattern, case_insensitive) {
      Ok(items) => Ok(items.into_iter().map(|(result, _item)| result).collect::<Vec<bool>>()),
      Err(error) => Err(error)
    }
  }

  /// Returns a filtered vector of matched string slices (&str) with case-insensitive flag
  fn pattern_matches_filtered(&self, pattern: &str, case_insensitive: bool) -> Vec<&str> {
    self.pattern_matched_pairs(pattern, case_insensitive).into_iter().filter(|(is_matched, _item)| *is_matched).map(|(_is_matched, item)| item).collect()
  }

  /// Returns a filtered vector of matched string slices (&str) in case-insensitive mode
  fn pattern_matches_filtered_ci(&self, pattern: &str) -> Vec<&str> {
    self.pattern_matched_pairs(pattern, true).into_iter().filter(|(is_matched, _item)| *is_matched).map(|(_is_matched, item)| item).collect()
  }

  /// Returns a filtered vector of matched string slices (&str) in case-sensitive mode
  fn pattern_matches_filtered_cs(&self, pattern: &str) -> Vec<&str> {
    self.pattern_matched_pairs(pattern, false).into_iter().filter(|(is_matched, _item)| *is_matched).map(|(_is_matched, item)| item).collect()
  }


  /// Returns vector of boolean matches for an array or vector of strings with case-insensitive flag
  /// must be reimplemented from pattern_matches_result owing to trait bound constraints on unsized arrays
  fn pattern_matches(&self, pattern: &str, case_insensitive: bool) -> Vec<bool> {
    self.pattern_matched_pairs(pattern, case_insensitive).into_iter().map(|(matched, _item)| matched).collect()
  }

  /// Returns vector of boolean matches for an array or vector of strings in case-insensitive mode
  fn pattern_matches_ci(&self, pattern: &str) -> Vec<bool> {
    self.pattern_matches(pattern, true)
  }

  /// Returns vector of boolean matches for an array or vector of strings in case-sensitive mode
  fn pattern_matches_cs(&self, pattern: &str) -> Vec<bool> {
    self.pattern_matches(pattern, false)
  }
}

/// Multiple match methods for arrays or vectors of any string-like type
impl<T: AsRef<str>> PatternMatches for [T] {

  /// Returns an Ok result with a vector of boolean matches for an array or vector of strings with a case-insensitive flag
  /// and an error only if the regex fails to compile.
  fn pattern_matched_pairs_result(&self, pattern: &str, case_insensitive: bool) -> Result<Vec<(bool, &str)>, Error> {
    with_cached_regex(pattern, case_insensitive, |re| {
      self.iter().map(|segment| (re.is_match(segment.as_ref()), segment.as_ref())).collect::<Vec<(bool, &str)>>()
    })
  }

  // Implement default vector of (bool, &str) results for [T]
  fn pattern_matched_pairs_default(&self) -> Vec<(bool, &str)> {
    self.iter().map(|item| (false, item.as_ref())).collect()
  }

}