Crate replacer

Source
Expand description

A crate for creating templates from Rust source files.

§Features

§Valid Rust

  • All templates can be compilable Rust source files
  • Linting & language servers work

§Extensible

§Example

use replacer::{rule::StringRule, TemplateBuilder};

fn main() -> anyhow::Result<()> {
    let template = TemplateBuilder::new()
        .rule(StringRule::new("replace", "world")?)
        .build();

    assert_eq!(template.apply("Hello $$replace$$!")?, "Hello world!");

    Ok(())
}

§Rules

§rule::StringRule

let some_str = "Hello $$replace_with_world$$!";

// Also works in comments, hello $$replace_with_world$$

§rule::TypeRule

// Unfortunately the type needs to be wrapped with angle brackets here
let some_type = <replacer::rust_type!(replace_with_type; String;)>::new();

let some_generic_type: Vec<replacer::rust_type!(replace_with_type_in_vec; i32;)> = vec![];

§rule::StructRule

replacer::rust_struct!(replace_with_struct; Point{ x: i32, y: i32 };);

§rule::ExprRule

println!("1 + 1 = {}", replacer::rust_expr!(replace_with_expression; 1 + 2;));

Modules§

rule

Macros§

rust_expr
Template macro for replacing a Rust expression with a placeholder expression that can be compiled.
rust_struct
Template macro for replacing a Rust struct with a placeholder struct that can be compiled.
rust_type
Template macro for replacing a Rust type with a placeholder type that can be compiled.

Structs§

Template
Internal representation of the template file.
TemplateBuilder
Builder for the Template struct.