Skip to main content

string_patterns/
pattern_replace.rs

1use regex::Error;
2use crate::pattern_cache::with_cached_regex;
3use std::borrow::ToOwned;
4
5/// Core regular expression replacement methods 
6pub trait PatternReplace where Self:Sized {
7
8  /// Replace all matches of the pattern within a longer text with a boolean case_insensitive flag
9  /// NB: If the regex doesn't compile it will return an Error, otherwise in Ok result.
10  /// If the regex fails, nothing will be replaced
11  fn pattern_replace_result(&self, pattern: &str, replacement: &str,case_insensitive: bool) -> Result<Self, Error> where Self:Sized;
12
13  /// Replace only the first match of the pattern within a longer text with a boolean case_insensitive flag
14  /// NB: If the regex doesn't compile it will return an Error, otherwise in Ok result.
15  /// If the regex fails, nothing will be replaced
16  fn pattern_replace_first_result(&self, pattern: &str, replacement: &str,case_insensitive: bool) -> Result<Self, Error> where Self:Sized;
17
18  /// Replace all matches of the pattern within a longer text with a boolean case_insensitive flag
19  /// Returns a copy of the same data type. If the regex fails, nothing will be replaced.
20  fn pattern_replace(&self, pattern: &str, replacement: &str, case_insensitive: bool) -> Self where Self:Sized;
21
22
23  /// Replace only the first match of the pattern within a longer text with a boolean case_insensitive flag
24  /// Returns a copy of the same data type. If the regex fails, nothing will be replaced.
25  fn pattern_replace_first(&self, pattern: &str, replacement: &str, case_insensitive: bool) -> Self where Self:Sized;
26
27
28  /// Replace all matches of the pattern within a longer text in case-insensitive mode
29  /// If the regex fails, nothing will be replaced
30  fn pattern_replace_ci(&self, pattern: &str, replacement: &str) -> Self where Self:Sized {
31    self.pattern_replace(pattern, replacement, true)
32  }
33
34  /// Replace all matches of the pattern within a longer text in case-sensitive mode
35  /// If the regex fails, nothing will be replaced
36  fn pattern_replace_cs(&self, pattern: &str, replacement: &str) -> Self where Self:Sized {
37    self.pattern_replace(pattern, replacement, false)
38  }
39
40  /// Replace the first match only of the pattern within a longer text in case-insensitive mode
41  /// If the regex fails, nothing will be replaced
42  fn pattern_replace_first_ci(&self, pattern: &str, replacement: &str) -> Self where Self:Sized {
43    self.pattern_replace_first(pattern, replacement, true)
44  }
45
46  /// Replace the first match only of the pattern within a longer text in case-sensitive mode
47  /// If the regex fails, nothing will be replaced
48  fn pattern_replace_first_cs(&self, pattern: &str, replacement: &str) -> Self where Self:Sized {
49    self.pattern_replace_first(pattern, replacement, false)
50  }
51
52}
53
54/// Core regex replacement methods for Strings
55impl PatternReplace for String {
56
57  /// Regex-enabled replace method that will return an OK String result if successful and an error if the regex fails
58  fn pattern_replace_result(&self, pattern: &str, replacement: &str, case_insensitive: bool) -> Result<String, Error> {
59    with_cached_regex(pattern, case_insensitive, |re| re.replace_all(self, replacement).to_string())
60  }
61
62  /// Regex-enabled replace method that will return an OK String result if successful and an error if the regex fails
63  fn pattern_replace_first_result(&self, pattern: &str, replacement: &str, case_insensitive: bool) -> Result<String, Error> {
64    with_cached_regex(pattern, case_insensitive, |re| re.replace(self, replacement).to_string())
65  }
66
67  /// Simple regex-enabled replace-all method that will return the same string if the regex fails
68  fn pattern_replace(&self, pattern: &str, replacement: &str, case_insensitive: bool) -> String {
69    self.pattern_replace_result(pattern, replacement, case_insensitive).unwrap_or(self.to_owned())
70  }
71
72  /// Regex-enabled single replace method that will return an OK String result if successful and an error if the regex fails
73  fn pattern_replace_first(&self, pattern: &str, replacement: &str, case_insensitive: bool) -> String {
74    self.pattern_replace_first_result(pattern, replacement, case_insensitive).unwrap_or(self.to_owned())
75  }
76
77}
78
79/// Implemented separately of arrays / vectors of strings to ensure the regex is only compiled once
80impl PatternReplace for Vec<String> {
81  ///
82  /// Optional regex-enabled replace method that will return None if the regex fails
83  /// 
84  fn pattern_replace_result(&self, pattern: &str, replacement: &str, case_insensitive: bool) -> Result<Vec<String>, Error> {
85    with_cached_regex(pattern, case_insensitive, |re| {
86      self.into_iter()
87          .map(|segment| re.replace_all(segment, replacement).to_string())
88          .collect::<Vec<String>>()
89    })
90  }
91
92  ///
93  /// Optional regex-enabled replace method that will return None if the regex fails
94  ///
95  fn pattern_replace_first_result(&self, pattern: &str, replacement: &str, case_insensitive: bool) -> Result<Vec<String>, Error> {
96    with_cached_regex(pattern, case_insensitive, |re| {
97      self.into_iter()
98          .map(|segment| re.replace(segment, replacement).to_string())
99          .collect::<Vec<String>>()
100    })
101  }
102
103  /// Simple regex-enabled replace-all method that will return the same string if the regex fails
104  fn pattern_replace(&self, pattern: &str, replacement: &str, case_insensitive: bool) -> Vec<String> {
105    self.pattern_replace_result(pattern, replacement, case_insensitive).unwrap_or(self.to_owned())
106  }
107
108  /// Simple regex-enabled replace-first method that will return the same string if the regex fails
109  fn pattern_replace_first(&self, pattern: &str, replacement: &str, case_insensitive: bool) -> Vec<String> {
110    self.pattern_replace_first_result(pattern, replacement, case_insensitive).unwrap_or(self.to_owned())
111  }
112
113}