|
48 | 48 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
49 | 49 | * OR OTHER DEALINGS IN THE SOFTWARE.
|
50 | 50 | */
|
| 51 | + |
| 52 | +/// Serialization and Deserialization for full haystack regex matchers |
| 53 | +pub mod serde; |
| 54 | + |
| 55 | +use regex::{Regex, RegexSet}; |
| 56 | + |
| 57 | +fn into_full_haystack_pattern<S>(re: S) -> String |
| 58 | +where |
| 59 | + S: AsRef<str>, |
| 60 | +{ |
| 61 | + format!("^(?:{})$", re.as_ref()) |
| 62 | +} |
| 63 | + |
| 64 | +fn peel_full_haystack_pattern(re: &str) -> &str { |
| 65 | + match re.strip_prefix("^(?:") { |
| 66 | + Some(prefix_clean) => prefix_clean.strip_suffix(r")$").unwrap_or(re), |
| 67 | + None => re, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/// Compiles a full haystack regular expression |
| 72 | +/// |
| 73 | +/// This will augment (anchor) the given re so that it will match against |
| 74 | +/// full haystack. |
| 75 | +/// |
| 76 | +/// See `Regex::Regex::new` for actual documentation of this method. |
| 77 | +/// |
| 78 | +/// See `peeled_pattern_as_str` how to get back the original string |
| 79 | +/// |
| 80 | +/// # Examples |
| 81 | +/// ```rust |
| 82 | +/// # use std::error::Error; |
| 83 | +/// use tackler_rs::regex::new_full_haystack_regex; |
| 84 | +/// |
| 85 | +/// let re_foo = new_full_haystack_regex("foo")?; |
| 86 | +/// let re_bar = new_full_haystack_regex("bar")?; |
| 87 | +/// |
| 88 | +/// assert!(re_foo.is_match("foo")); |
| 89 | +/// assert!(re_bar.is_match("bar")); |
| 90 | +/// |
| 91 | +/// assert!(!re_foo.is_match("foobar")); |
| 92 | +/// assert!(!re_bar.is_match("foobar")); |
| 93 | +/// # Ok::<(), Box<dyn Error>>(()) |
| 94 | +/// ``` |
| 95 | +pub fn new_full_haystack_regex(re: &str) -> Result<Regex, regex::Error> { |
| 96 | + Regex::new(into_full_haystack_pattern(re).as_str()) |
| 97 | +} |
| 98 | + |
| 99 | +/// Returns the original string of this regex. |
| 100 | +/// # Examples |
| 101 | +/// ```rust |
| 102 | +/// # use std::error::Error; |
| 103 | +/// use tackler_rs::regex::new_full_haystack_regex; |
| 104 | +/// use tackler_rs::regex::peeled_pattern; |
| 105 | +/// |
| 106 | +/// let re_foo = new_full_haystack_regex(r"foo.*")?; |
| 107 | +/// |
| 108 | +/// assert_eq!(peeled_pattern(&re_foo), r"foo.*"); |
| 109 | +/// # Ok::<(), Box<dyn Error>>(()) |
| 110 | +/// ``` |
| 111 | +pub fn peeled_pattern(regex: &Regex) -> &str { |
| 112 | + peel_full_haystack_pattern(regex.as_str()) |
| 113 | +} |
| 114 | + |
| 115 | +/// Compiles a set of full haystack regular expressions |
| 116 | +/// |
| 117 | +/// This will augment (anchor) the given expressions so |
| 118 | +/// that each of those will match against full haystack. |
| 119 | +/// |
| 120 | +/// See `Regex::RegexSet::new` for actual documentation of this method. |
| 121 | +/// |
| 122 | +/// See `peeled_pattern` how to get back the original string |
| 123 | +/// |
| 124 | +/// # Examples |
| 125 | +/// ```rust |
| 126 | +/// # use std::error::Error; |
| 127 | +/// use tackler_rs::regex::new_full_haystack_regex_set; |
| 128 | +/// |
| 129 | +/// let re_set = new_full_haystack_regex_set(["foo", "bar"])?; |
| 130 | +/// |
| 131 | +/// assert!(re_set.is_match("foo")); |
| 132 | +/// assert!(re_set.is_match("bar")); |
| 133 | +/// |
| 134 | +/// assert!(!re_set.is_match("foobar")); |
| 135 | +/// assert!(!re_set.is_match("foobar")); |
| 136 | +/// # Ok::<(), Box<dyn Error>>(()) |
| 137 | +/// ``` |
| 138 | +pub fn new_full_haystack_regex_set<I, S>(exprs: I) -> Result<RegexSet, regex::Error> |
| 139 | +where |
| 140 | + S: AsRef<str>, |
| 141 | + I: IntoIterator<Item = S>, |
| 142 | +{ |
| 143 | + RegexSet::new(exprs.into_iter().map(|re| into_full_haystack_pattern(re))) |
| 144 | +} |
| 145 | + |
| 146 | +/// Returns the peeled regex patterns that this regex set was constructed from. |
| 147 | +/// |
| 148 | +/// # Examples |
| 149 | +/// ```rust |
| 150 | +/// # use std::error::Error; |
| 151 | +/// use tackler_rs::regex::new_full_haystack_regex_set; |
| 152 | +/// use tackler_rs::regex::peeled_patterns; |
| 153 | +/// |
| 154 | +/// let re_set = new_full_haystack_regex_set(["foo", "bar"])?; |
| 155 | +/// |
| 156 | +/// assert_eq!(peeled_patterns(&re_set), vec!["foo", "bar"]); |
| 157 | +/// # Ok::<(), Box<dyn Error>>(()) |
| 158 | +/// ``` |
| 159 | +pub fn peeled_patterns(regex_set: &RegexSet) -> Vec<String> { |
| 160 | + regex_set |
| 161 | + .patterns() |
| 162 | + .iter() |
| 163 | + .map(|re| peel_full_haystack_pattern(re).to_string()) |
| 164 | + .collect::<Vec<_>>() |
| 165 | +} |
| 166 | + |
| 167 | +#[cfg(test)] |
| 168 | +mod tests { |
| 169 | + use super::*; |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn test_peel_full_haystack_pattern() { |
| 173 | + assert_eq!(peel_full_haystack_pattern("abc"), "abc"); |
| 174 | + assert_eq!(peel_full_haystack_pattern(".*"), ".*"); |
| 175 | + assert_eq!(peel_full_haystack_pattern("(.*)"), "(.*)"); |
| 176 | + assert_eq!(peel_full_haystack_pattern("^(?:.*)"), "^(?:.*)"); |
| 177 | + assert_eq!(peel_full_haystack_pattern("(.*)$"), "(.*)$"); |
| 178 | + assert_eq!(peel_full_haystack_pattern("^(?:.*)$"), ".*"); |
| 179 | + } |
| 180 | + |
| 181 | + #[test] |
| 182 | + fn test_full_haystack_pattern() { |
| 183 | + let re = new_full_haystack_regex(r"o.a").unwrap(/*:test:*/); |
| 184 | + assert_eq!(re.as_str(), r"^(?:o.a)$"); |
| 185 | + |
| 186 | + assert!(!re.is_match("foobar")); |
| 187 | + assert!(!re.is_match("ooba")); |
| 188 | + assert!(!re.is_match("obar")); |
| 189 | + assert!(re.is_match("oba")); |
| 190 | + } |
| 191 | + |
| 192 | + #[test] |
| 193 | + fn test_full_haystack_pattern_anchored() { |
| 194 | + let re = new_full_haystack_regex(r"^o.a$").unwrap(/*:test:*/); |
| 195 | + assert_eq!(re.as_str(), r"^(?:^o.a$)$"); |
| 196 | + |
| 197 | + assert!(!re.is_match("foobar")); |
| 198 | + assert!(!re.is_match("ooba")); |
| 199 | + assert!(!re.is_match("obar")); |
| 200 | + assert!(re.is_match("oba")); |
| 201 | + } |
| 202 | + |
| 203 | + #[test] |
| 204 | + fn test_full_haystack_pattern_peeled() { |
| 205 | + let re_str = r"^(?:o.a)$"; |
| 206 | + let re = new_full_haystack_regex(re_str).unwrap(/*:test:*/); |
| 207 | + assert_eq!(re.as_str(), r"^(?:^(?:o.a)$)$"); |
| 208 | + |
| 209 | + assert!(!re.is_match("foobar")); |
| 210 | + assert!(!re.is_match("ooba")); |
| 211 | + assert!(!re.is_match("obar")); |
| 212 | + assert!(re.is_match("oba")); |
| 213 | + |
| 214 | + assert_eq!(peeled_pattern(&re), re_str); |
| 215 | + } |
| 216 | + |
| 217 | + #[test] |
| 218 | + fn test_full_haystack_patterns() { |
| 219 | + let re_set = new_full_haystack_regex_set([r".*foo", r"bar.*"]).unwrap(/*:test:*/); |
| 220 | + assert_eq!(re_set.patterns(), [r"^(?:.*foo)$", r"^(?:bar.*)$"]); |
| 221 | + |
| 222 | + assert!(!re_set.is_match("foobar")); |
| 223 | + assert!(re_set.is_match("foo")); |
| 224 | + assert!(re_set.is_match("bar")); |
| 225 | + } |
| 226 | + |
| 227 | + #[test] |
| 228 | + fn test_full_haystack_patterns_anchored() { |
| 229 | + let re_set = new_full_haystack_regex_set([r"^.*foo$", r"^bar.*$"]).unwrap(/*:test:*/); |
| 230 | + assert_eq!(re_set.patterns(), [r"^(?:^.*foo$)$", r"^(?:^bar.*$)$"]); |
| 231 | + |
| 232 | + assert!(!re_set.is_match("foobar")); |
| 233 | + assert!(re_set.is_match("foo")); |
| 234 | + assert!(re_set.is_match("bar")); |
| 235 | + } |
| 236 | + |
| 237 | + #[test] |
| 238 | + fn test_full_haystack_patterns_peeled() { |
| 239 | + let re_set_str = [r"^(?:.*foo)$", r"^(?:bar.*)$"]; |
| 240 | + let re_set = new_full_haystack_regex_set(re_set_str).unwrap(/*:test:*/); |
| 241 | + assert_eq!( |
| 242 | + re_set.patterns(), |
| 243 | + [r"^(?:^(?:.*foo)$)$", r"^(?:^(?:bar.*)$)$"] |
| 244 | + ); |
| 245 | + |
| 246 | + assert!(!re_set.is_match("foobar")); |
| 247 | + assert!(re_set.is_match("foo")); |
| 248 | + assert!(re_set.is_match("bar")); |
| 249 | + |
| 250 | + assert_eq!(peeled_patterns(&re_set), re_set_str); |
| 251 | + } |
| 252 | +} |
0 commit comments