Orz - Rust Procedural Macro for Struct Field Information
A powerful and practical Rust procedural macro for automatically generating field information methods for structs. The name "Orz" expresses admiration while hinting at the library's powerful capabilities.
Features
- 🔤 Custom field display names - Define custom names for struct fields
- 🚫 Skip specified fields - Exclude certain fields from output
- 🔧 Custom formatting functions - Apply custom logic to format field values
- 🐛 Debug trait formatting - Format values using Debug trait when needed
- 📊 Field information & statistics - Get comprehensive field metadata
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Quick Start
use Orz;
let user = User ;
println!;
// Output: ["Username", "Age"]
println!;
// Output: ["alice", "30"]
Complete Example
use Orz;
// Custom formatting functions
Attribute Reference
#[name("Display Name")]
Sets the display name for a field. If not set, the field identifier is used by default.
username: String,
email: String,
#[skip]
Skips the field, excluding it from all outputs.
password_hash: String,
internal_id: u64,
#[to_string_with("function_name")]
Uses a custom function to format the field value. Function signature should be fn(&T) -> String.
created_at: NaiveDate,
#[debug]
Formats the field value using the Debug trait ({:?}).
metadata: ,
options: ,
Generated Methods
field_names() -> Vec<&'static str>
Returns display names of all fields.
let names = field_names;
// ["Product Name", "Price", "Stock Quantity", "description", "Debug Info"]
field_values(&self) -> Vec<String>
Returns string representations of all field values.
let values = product.field_values;
// ["Laptop", "¥5999.99", "100 items", "High-performance gaming laptop", "[\"data1\", \"data2\"]"]
field_info(&self) -> Vec<(&'static str, String)>
Returns a mapping of field names to field values.
let info = product.field_info;
// [("Product Name", "Laptop"), ("Price", "¥5999.99"), ...]
field_count() -> usize
Returns the number of fields.
let count = field_count;
// 5
field_values_ref(&self) -> Vec<&str> (Conditionally Generated)
Automatically generated when all field types implement AsRef<str> (like &str, String), returning references to field values.
let log = Log ;
let refs = log.field_values_ref;
// ["INFO", "Hello"]
Priority Rules
Field value formatting priority (highest to lowest):
#[to_string_with("function_name")]- Custom function#[debug]- Debug trait formatting- Default
to_string()- Display trait formatting
Why "Orz"?
Orz is often used online to express admiration or kneeling in respect. The library name reflects its powerful capabilities:
- O - Object field information
- R - Rapid retrieval
- Z - Zero boilerplate code
Using the Orz procedural macro, you'll be impressed by its convenience!
Notes
- Only supports named field structs
- Does not support enums or unions
- Field display names support Unicode characters
- Custom formatting functions must be visible in the current scope
- Only supports
#[name("value")]and#[to_string_with("function_name")]formats
Running Examples
# Run example
# Run tests
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues for any bugs or feature requests.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Orz - Making struct field reflection in Rust effortless! 🦀