Description
rust_writer is a crate designed to simplify meta programming in Rust—but in a different way than the typical procedural macro crates (such as [syn], quote, and [proc_macro2]).
While those crates excel at writing procedural macros, rust_writer leverages their
capabilities to modify Rust source files. This makes it ideal for tasks that require in-place
source code modifications rather than merely generating new code visible only to the compiler.
The crate is divided into two modules: the [preserver] module and the [ast] module. Although
these modules can be used separately and even for purposes other than the crate's primary
objective, using them together unlocks the full potential of the crate.
- The [
preserver] module ensures that the original structure of the source code is maintained when it is parsed into an AST. - The [
ast] module provides various tools to simplify AST interactions, allowing precise modifications exactly where needed.
For further details, please refer to the individual module documentation. A complete example is often the best way to illustrate the functionality:
use quote;
use ;
use ;
use TestBuilder;
// A mutator defined with the given implementors.
;
default
.with_complete_file
.with_expanded_file
.execute;
Important Note
To simplify testing of this crate, a crate called test_builder is included as a dev
dependency. This crate makes it easy to set up tests that load different files and ASTs
seamlessly, allowing tests to focus on the specific functionality being verified.
This crate is widely used across the documentation, so it's worthy to mention that the sample
files used by TestBuilder are available in
the repository and can be consulted as needed.
The syntax for TestBuilder is straightforward: methods like with_something_ast load an AST
from a file named accordingly, while with_something_file load the entire file.