Struct macro_rules_rt::Rule

source ·
pub struct Rule { /* private fields */ }
Expand description

Pair Matcher and Transcriber.

Implementations§

source§

impl Rule

source

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.

source

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");
source

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.

source

pub fn replace_all_tokens(&self, input: TokenStream) -> TokenStream

Replaces all non-overlapping matches in input with the provided transcriber.

source

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.

source

pub fn apply(&self, input: &str) -> Result<String>

If the entire input matches the entire from, do the conversion. Otherwise, return an error.

source

pub fn apply_tokens(&self, input: TokenStream) -> Result<TokenStream>

If the entire input matches the entire from, do the conversion. Otherwise, return an error.

Trait Implementations§

source§

impl Clone for Rule

source§

fn clone(&self) -> Rule

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Rule

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Rule

§

impl !Send for Rule

§

impl !Sync for Rule

§

impl Unpin for Rule

§

impl UnwindSafe for Rule

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.