1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use crate::parser::RegexSyntax;
pub trait RegExp: Sized {
fn syntax() -> RegexSyntax;
/// Generates a regexp pattern for the given string. If the pattern is
/// invalid, the parse function should return an error.
fn parse(pattern: &str) -> Result<Self, ()>;
/// Matches the given text against the regular expression and returns the list
/// of captures. The matches are returned in the order they appear in the
/// regular expression. It is **not** prefixed with the full match. For groups
/// that occur in the regular expression, but did not match, the corresponding
/// capture should be the empty string ("").
///
/// Returns `None` if the text does not match the regular expression.
fn matches<'a>(&self, text: &'a str) -> Option<Vec<&'a str>>;
}
impl RegExp for regex::Regex {
fn syntax() -> RegexSyntax {
RegexSyntax::Rust
}
fn parse(pattern: &str) -> Result<Self, ()> {
regex::Regex::new(pattern).map_err(|_| ())
}
fn matches<'a>(&self, text: &'a str) -> Option<Vec<&'a str>> {
let captures = self.captures(text)?;
let captures = captures
.iter()
.skip(1)
.map(|c| c.map(|m| m.as_str()).unwrap_or(""))
.collect();
Some(captures)
}
}