pub struct FMBuilder<'a> { /* private fields */ }
Expand description
Build up a FMatcher
allowing the setting of options.
use {fm::FMBuilder, regex::Regex};
let ptn_re = Regex::new(r"\$.+?\b").unwrap();
let text_re = Regex::new(r".+?\b").unwrap();
let matcher = FMBuilder::new("$1 $1")
.unwrap()
.name_matcher(ptn_re, text_re)
.build()
.unwrap();
assert!(matcher.matches("a a").is_ok());
assert!(matcher.matches("a b").is_err());
Implementations§
source§impl<'a> FMBuilder<'a>
impl<'a> FMBuilder<'a>
sourcepub fn new(ptn: &'a str) -> Result<Self, Box<dyn Error>>
pub fn new(ptn: &'a str) -> Result<Self, Box<dyn Error>>
Create a new FMBuilder
with default options.
pub fn output_formatter(self, output_formatter: OutputFormatter) -> Self
sourcepub fn name_matcher(self, ptn_re: Regex, text_re: Regex) -> Self
pub fn name_matcher(self, ptn_re: Regex, text_re: Regex) -> Self
Add a name matcher (ptn_re, text_re)
. Name matchers allow you to ensure that different
parts of the text match without specifying precisely what they match. For example, if you
have output where you want to ensure that two locations always match the same name, but the
name is non-deterministic you can allow the use of $
wildcards in your pattern:
use {fm::FMBuilder, regex::Regex};
let ptn_re = Regex::new(r"\$.+?\b").unwrap();
let text_re = Regex::new(r".+?\b").unwrap();
let matcher = FMBuilder::new("$1 b $1")
.unwrap()
.name_matcher(ptn_re, text_re)
.build()
.unwrap();
assert!(matcher.matches("a b a").is_ok());
assert!(matcher.matches("a b b").is_err());
Note that if a line in the pattern uses name matching, it can only use the wildcard
operator at the end of the line (so, for the above name matcher, $1...
is allowed but
...$1
or ...$1...
is not allowed). Invalid combinations of wildcards and name matching
are caught when a pattern is built.
Multiple name matchers are allowed: they are matched in the order they were added to
FMBuilder
.
sourcepub fn distinct_name_matching(self, yes: bool) -> Self
pub fn distinct_name_matching(self, yes: bool) -> Self
If yes
, then different names cannot match the same text value. For example if $1
binds
to a
then $2
will refuse to match against a
(though $1
will continue to match
against only a
). Defaults to false
.
sourcepub fn ignore_leading_whitespace(self, yes: bool) -> Self
pub fn ignore_leading_whitespace(self, yes: bool) -> Self
If yes
, then each line’s leading whitespace will be ignored in both pattern and text;
otherwise leading whitespace must match. Defaults to true
.
sourcepub fn ignore_trailing_whitespace(self, yes: bool) -> Self
pub fn ignore_trailing_whitespace(self, yes: bool) -> Self
If yes
, then each line’s trailing whitespace will be ignored in both pattern and text;
otherwise trailing whitespace must match. Defaults to true
.
sourcepub fn ignore_surrounding_blank_lines(self, yes: bool) -> Self
pub fn ignore_surrounding_blank_lines(self, yes: bool) -> Self
If yes
, blank lines at the start and end of both the pattern and text are ignored for
matching purposes. Defaults to true
.