pub struct Rule { /* private fields */ }Expand description
Pair Matcher and Transcriber.
Implementations§
Source§impl Rule
impl Rule
Sourcepub fn new(from: Matcher, to: Transcriber) -> Result<Self>
pub fn new(from: Matcher, to: Transcriber) -> Result<Self>
Create a new Rule from Matcher and Transcriber.
Returns an error if the meta-variables of Matcher and Transcriber do not match.
Sourcepub fn nest(self, yes: bool) -> Self
pub fn nest(self, yes: bool) -> Self
Specifies whether to apply replacements to metavariable matches. (default is false.)
If false, only the outermost matched range is replaced.
If true, further substitutions are made for the range matched by meta-variables such as $e:expr.
use macro_rules_rt::Rule;
let from = "a + $e:expr".parse()?;
let to = "b + $e".parse()?;
let input = "a + a + x";
let rule = Rule::new(from, to)?;
let r_nest_no = rule.clone().replace_all(input)?;
let r_nest_yes = rule.nest(true).replace_all(input)?;
assert_eq!(r_nest_no, "b + a + x");
assert_eq!(r_nest_yes, "b + b + x");Sourcepub fn replace_all(&self, input: &str) -> Result<String>
pub fn replace_all(&self, input: &str) -> Result<String>
Replaces all non-overlapping matches in input with the provided transcriber.
Unlike creating TokenStream from str and then calling Rule::replace_all,
the original string is preserved as much as possible.
Sourcepub fn replace_all_tokens(&self, input: TokenStream) -> TokenStream
pub fn replace_all_tokens(&self, input: TokenStream) -> TokenStream
Replaces all non-overlapping matches in input with the provided transcriber.
Sourcepub fn match_all<'a>(&'a self, input: &'a str) -> Result<MatchAll<'a>>
pub fn match_all<'a>(&'a self, input: &'a str) -> Result<MatchAll<'a>>
Replaces all non-overlapping matches in input with the provided transcriber, and returns detailed information.
Sourcepub fn apply(&self, input: &str) -> Result<String>
pub fn apply(&self, input: &str) -> Result<String>
If the entire input matches the entire from, do the conversion. Otherwise, return an error.
Sourcepub fn apply_tokens(&self, input: TokenStream) -> Result<TokenStream>
pub fn apply_tokens(&self, input: TokenStream) -> Result<TokenStream>
If the entire input matches the entire from, do the conversion. Otherwise, return an error.