2 releases
new 0.1.1 | Apr 5, 2025 |
---|---|
0.1.0 | Feb 5, 2025 |
#11 in #template-string
107 downloads per month
Used in srtemplate
29KB
452 lines
srtemplate-macros Library Documentation
This library provides procedural macros for generating function and variable handling code, particularly useful for template processing systems.
Features
- Function attribute macro: Transforms regular functions into argument-parsing functions
- Variable derive macro: Converts structs into collections of key-value variables
- Text case conversion: Comprehensive text case handling utilities
Macros
#[function]
Attribute Macro
Transforms a function to accept string arguments and perform automatic parsing.
Example:
#[srtemplate::function]
fn greet(name: String, age: i32) -> String {
format!("Hello {}, you're {} years old", name, age)
}
Generated Code:
fn greet(args: &[String]) -> srtemplate::prelude::FuncResult {
srtemplate::prelude::validations::args_min_len(args, 2)?;
srtemplate::prelude::validations::args_max_len(args, 2)?;
let name = args[0].parse::<String>().map_err(|_| srtemplate::prelude::FromArgsError::ParseFailed(0))?;
let age = args[1].parse::<i32>().map_err(|_| srtemplate::prelude::FromArgsError::ParseFailed(1))?;
format!("Hello {}, you're {} years old", name, age)
}
#[derive(Variable)]
Macro
Generates a variables()
method for structs that returns field names and values as strings.
Example:
#[derive(srtemplate::Variable)]
#[template(case = "snake")]
struct User {
#[template(rename = "first_name")]
name: String,
age: u32,
}
Generated Implementation:
impl<'variable> srtemplate::Variable<'variable> for User {
fn variables(&self) -> impl Iterator<Item = (std::borrow::Cow<'variable, str>, String)> {
[
("user.first_name".into(), self.name.to_string()),
("user.age".into(), self.age.to_string())
].into_iter()
}
}
Text Case Conversion
The library provides comprehensive text case conversion through the TextCase
enum:
Supported Cases
Case | Example | Conversion Method |
---|---|---|
Lower |
hello world |
to_lowercase() |
Upper |
HELLO WORLD |
to_uppercase() |
Snake |
hello_world |
Lowercase with underscores |
Camel |
helloWorld |
First word lowercase, others capitalized |
Pascal |
HelloWorld |
All words capitalized |
Kebab |
hello-world |
Lowercase with hyphens |
ScreamingSnake |
HELLO_WORLD |
Uppercase with underscores |
Usage Example:
use srtemplate::TextCase;
let text = "hello_world-example";
let pascal_case = TextCase::Pascal.convert(text);
assert_eq!(pascal_case, "HelloWorldExample");
Error Handling
The macros provide clear error messages for:
- Invalid function signatures in
#[function]
- Unsupported types in
#[derive(Variable)]
- Malformed attribute syntax
- Invalid case type specifications
Limitations
-
#[function]
macro:- Doesn't support methods (functions with
self
parameter) - Requires explicit type annotations
- Doesn't support methods (functions with
-
#[derive(Variable)]
macro:- Currently only supports structs (not enums)
- Field types must implement
ToString
Dependencies
~270KB