generate_code

Macro generate_code 

Source
macro_rules! generate_code {
    (
        template = $template:literal,
        placeholders = { $($key:literal => $value:literal),* $(,)? }
    ) => { ... };
}
Expand description

Generate code using a template at compile time.

This is an advanced macro that allows you to specify placeholder values and generate code directly at compile time. The generated code is inlined into your program.

Note: This macro requires the template to be fully resolved at compile time, so all placeholder values must be string literals.

§Examples

use tron::generate_code;

// Generate a simple function
generate_code!(
    template = "fn @[name]@() -> @[return_type]@ { @[body]@ }",
    placeholders = {
        "name" => "hello_world",
        "return_type" => "&'static str", 
        "body" => "\"Hello, World!\""
    }
);
 
// The above generates:
// fn hello_world() -> &'static str { "Hello, World!" }

§Complex Example

use tron::generate_code;

generate_code!(
    template = r#"
        #[derive(@[derives]@)]
        pub struct @[name]@ {
            @[fields]@
        }
    "#,
    placeholders = {
        "derives" => "Debug, Clone, PartialEq",
        "name" => "Person",
        "fields" => "pub name: String,\n    pub age: u32"
    }
);