Crates.io | typlate |
lib.rs | typlate |
version | 0.2.3 |
created_at | 2025-09-15 08:45:34.496392+00 |
updated_at | 2025-09-16 13:12:35.16371+00 |
description | Type-safe string templates with compile-time validation. |
homepage | |
repository | https://github.com/shigma/typlate |
max_upload_size | |
id | 1839637 |
size | 15,486 |
A Rust library for type-safe string templates with compile-time validation.
Add to your Cargo.toml
:
[dependencies]
typlate = { version = "0.2", features = ["full"] }
use typlate::{TemplateParams, TemplateString};
#[derive(TemplateParams)]
struct User {
name: String,
age: u32,
}
fn main() {
// Create a template string
let template: TemplateString<User> = "Hello {name}, you are {age} years old!".parse().unwrap();
// Create an instance with actual values
let user = User {
name: "Alice".to_string(),
age: 30,
};
// Format the template with the values
assert_eq!(template.format(&user), "Hello Alice, you are 30 years old!");
}
{variable_name}
{{
for {
and }}
for }
With the serde
feature enabled, templates can be serialized and deserialized using serde:
use typlate::{TemplateParams, TemplateString};
use serde::{Serialize, Deserialize};
#[derive(TemplateParams)]
struct Data {
value: u32,
}
#[derive(Serialize, Deserialize)]
struct Messages {
foo: TemplateString<Data>,
}
let json = r#"{"foo": "Value is {value}"}"#;
let messages: Messages = serde_json::from_str(json).unwrap();
let data = Data { value: 42 };
assert_eq!(messages.foo.format(&data), "Value is 42");
Template parsing will fail if: