Expand description
§typlate
A Rust library for type-safe string templates with compile-time validation.
§Installation
Add to your Cargo.toml:
[dependencies]
typlate = { version = "0.2", features = ["full"] }§Quick Start
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!");
}§Template Syntax
- Variables are enclosed in curly braces:
{variable_name} - To include literal braces, double them:
{{for{and}}for} - Variable names must match the field names of the target type
§Serde Support
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");§Error Handling
Template parsing will fail if:
- Variable names don’t match any field in the target type
- Brackets are not properly matched
Structs§
- Template
String - A type-safe template string that can be formatted with values of type
T.
Traits§
- Template
Params - A trait for types that can provide template parameters.