Rule

Struct 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 duplicate 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 Freeze for Rule

§

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 T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.