7 releases
| 0.3.2 | Jun 22, 2025 |
|---|---|
| 0.3.1 | Apr 4, 2025 |
| 0.2.2 | Jan 26, 2025 |
| 0.1.1 | Oct 29, 2024 |
#156 in Template engine
88 downloads per month
Used in 8 crates
54KB
545 lines
Anvil
Anvil is a modular templating system for creating user-defined scaffolding systems. It provides a composable API for file operations like generating, appending, transforming, and moving files.
Core Concepts
Anvil is built around two primary traits:
Anvil- The base trait for template rendering enginesForge- The base trait for file operations using rendered templates
Think of Anvil as the template you render, and Forge as what you do with that rendered content (create a file, append to a file, transform a file, etc.).
Design Philosophy
- Configuration is code: Your scaffolding logic is defined directly in code, enabling compile-time checking and integration with your application.
- Compile time errors are better than runtime errors: Detect issues at compile time whenever possible.
- The library provides the building blocks, not the solutions: Anvil gives you composable components to build your own custom scaffolding systems.
Example Usage
use anvil::{Anvil, Forge, generate::Generate};
use std::io::Write;
// Simple implementation of the Anvil trait
struct SimpleTemplate {
content: String,
}
impl Anvil for SimpleTemplate {
type Error = std::io::Error;
fn anvil(&self, writer: &mut (impl Write + Sized)) -> Result<(), Self::Error> {
writer.write_all(self.content.as_bytes())?;
Ok(())
}
}
// Using Generate for file creation
fn main() -> Result<(), Box<dyn std::error::Error>> {
let template = SimpleTemplate {
content: "Hello, Anvil!".to_string(),
};
// Create a file generator using our template
let generator = Generate::new(template);
// Generate the file
generator.forge("./output.txt")?;
println!("File generated successfully!");
Ok(())
}
Inspiration and Credits
Dependencies
~170–590KB
~14K SLoC