Skip to main content

Replacer

Trait Replacer 

Source
pub trait Replacer<D: Doc> {
    // Required method
    fn generate_replacement(&self, nm: &NodeMatch<'_, D>) -> Underlying<D>;

    // Provided method
    fn get_replaced_range(
        &self,
        nm: &NodeMatch<'_, D>,
        matcher: impl Matcher,
    ) -> Range<usize> { ... }
}
Expand description

Generate replacement content for matched AST nodes.

The Replacer trait defines how to transform a matched node into new content. Implementations can use template strings with meta-variables, structural replacement with other AST nodes, or custom logic.

§Type Parameters

  • D: Doc - The document type containing source code and language information

§Example Implementation

struct CustomReplacer;

impl<D: Doc> Replacer<D> for CustomReplacer {
    fn generate_replacement(&self, nm: &NodeMatch<'_, D>) -> Underlying<D> {
        // Custom replacement logic here
        "new_code".as_bytes().to_vec()
    }
}

Required Methods§

Source

fn generate_replacement(&self, nm: &NodeMatch<'_, D>) -> Underlying<D>

Generate replacement content for a matched node.

Takes a NodeMatch containing the matched node and its captured meta-variables, then returns the raw bytes that should replace the matched content in the source code.

§Parameters
  • nm - The matched node with captured meta-variables
§Returns

Raw bytes representing the replacement content

Provided Methods§

Source

fn get_replaced_range( &self, nm: &NodeMatch<'_, D>, matcher: impl Matcher, ) -> Range<usize>

Determine the exact range of source code to replace.

By default, replaces the entire matched node’s range. Some matchers may want to replace only a portion of the matched content.

§Parameters
  • nm - The matched node
  • matcher - The matcher that found this node (may provide custom range info)
§Returns

Byte range in the source code to replace

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<D, T> Replacer<D> for &T
where D: Doc, T: Replacer<D> + ?Sized,

Source§

impl<D: Doc> Replacer<D> for str

Implementors§

Source§

impl<D: Doc> Replacer<D> for TemplateFix

Source§

impl<D: Doc> Replacer<D> for Node<'_, D>

Source§

impl<D: Doc> Replacer<D> for Root<D>