1use crate::parser::RegexSyntax;
2
3pub trait RegExp: Sized {
4 fn syntax() -> RegexSyntax;
5
6 fn parse(pattern: &str, flags: &str) -> Result<Self, ()>;
9
10 fn matches<'a>(&self, text: &'a str) -> Option<Vec<Option<&'a str>>>;
18}
19
20impl RegExp for regex::Regex {
21 fn syntax() -> RegexSyntax {
22 RegexSyntax::Rust
23 }
24
25 fn parse(pattern: &str, flags: &str) -> Result<Self, ()> {
26 regex::Regex::new(&format!("(?{flags}){pattern}")).map_err(|_| ())
27 }
28
29 fn matches<'a>(&self, text: &'a str) -> Option<Vec<Option<&'a str>>> {
30 let captures = self.captures(text)?;
31
32 let captures = captures
33 .iter()
34 .skip(1)
35 .map(|c| c.map(|m| m.as_str()))
36 .collect();
37
38 Some(captures)
39 }
40}