| Crates.io | template_rs_cli |
| lib.rs | template_rs_cli |
| version | 0.1.0 |
| created_at | 2025-01-15 18:31:33.278191+00 |
| updated_at | 2025-01-15 18:31:33.278191+00 |
| description | A CLI tool to generate and run rust files from rust templates in the .tmrs format |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1517898 |
| size | 29,749 |
A command-line interface for managing and executing Rust code templates using the rust_templates library.
cargo install template_rs_cli
Or build from source:
git clone https://github.com/tristanpoland/template_rs_cli
cd template-cli
cargo install --path .
For template execution, ensure rust-script is installed:
cargo install rust-script
Create a new template file with inline content:
template_rs_cli new -o hello.template_rs -c 'fn main() { println!("@[message]@"); }'
Create a template from an existing file:
template_rs_cli new -o output.template_rs -f input.rs
Render a template with placeholder values:
template_rs_cli render \
-t hello.template_rs \
-v message="Hello, World!" \
-o hello.rs
Multiple values can be provided:
template_rs_cli render \
-t struct.template_rs \
-v struct_name=User \
-v fields="name: String, age: u32" \
-o user.rs
Execute a template with rust-script:
template_rs_cli execute \
-t math.template_rs \
-v number_type=f64 \
-v value=42.0 \
-d "num=0.4"
Add multiple dependencies:
template_rs_cli execute \
-t web.template_rs \
-v url="https://api.example.com" \
-d "reqwest=0.11" \
-d "tokio=1.0" \
-d "serde_json=1.0"
Combine multiple templates with shared values:
template_rs_cli assemble \
-t header.template_rs \
-t implementation.template_rs \
-t tests.template_rs \
-v module_name=calculator \
-o combined.rs
Templates use @[placeholder_name]@ syntax for placeholders:
// Example template
fn @[function_name]@() -> @[return_type]@ {
let value = @[value]@;
println!("@[message]@: {}", value);
value
}
Values are provided using key=value pairs:
name=valuemessage="Hello World"\n for newlinesDependencies for template execution use name=version format:
-d "serde=1.0"
-d "tokio=1.0"
The CLI provides clear error messages for common issues:
# Create template
template_rs_cli new -o hello.template_rs -c '
fn main() {
println!("@[greeting]@ @[name]@!");
}
'
# Render template
template_rs_cli render \
-t hello.template_rs \
-v greeting=Hello \
-v name=World \
-o hello.rs
# Execute template
template_rs_cli execute \
-t hello.template_rs \
-v greeting=Hello \
-v name=World
# Create a struct template
template_rs_cli new -o struct.template_rs -c '
#[derive(Debug)]
struct @[struct_name]@ {
@[fields]@
}
fn main() {
let instance = @[struct_name]@ {
@[field_values]@
};
println!("{:?}", instance);
}
'
# Render with values
template_rs_cli render \
-t struct.template_rs \
-v struct_name=User \
-v fields="name: String,\n age: u32" \
-v field_values="name: String::from(\"Alice\"),\n age: 30" \
-o user.rs
# Create template with dependencies
template_rs_cli execute \
-t api.template_rs \
-v url="https://api.example.com" \
-v method="get" \
-d "reqwest=0.11" \
-d "tokio=1.0"
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.