| Crates.io | field-meta |
| lib.rs | field-meta |
| version | 0.1.1 |
| created_at | 2025-09-12 03:20:15.781493+00 |
| updated_at | 2025-09-12 03:34:49.816091+00 |
| description | A procedural macro to derive metadata for struct fields |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1835012 |
| size | 9,784 |
A Rust procedural macro that provides compile-time field metadata access for structs.
field-meta is a derive macro that automatically generates methods to introspect struct fields at compile time. It allows you to:
Add this to your Cargo.toml:
[dependencies]
field-meta = "0.1.1"
use field_meta::FieldMeta;
#[derive(FieldMeta)]
struct User {
id: u64,
name: String,
email: String,
age: Option<u32>,
}
fn main() {
// Get all field names
let fields = User::fields();
assert_eq!(fields, &["id", "name", "email", "age"]);
// Check if a field exists
assert!(User::contains_field("name"));
assert!(!User::contains_field("password"));
// Get field count
assert_eq!(User::field_count(), 4);
}
You can exclude fields from the metadata using the #[field_meta(skip)] attribute:
use field_meta::FieldMeta;
#[derive(FieldMeta)]
struct Config {
public_key: String,
#[field_meta(skip)]
private_key: String, // This field won't be included in metadata
timeout: u64,
}
fn main() {
let fields = Config::fields();
assert_eq!(fields, &["public_key", "timeout"]);
assert!(!Config::contains_field("private_key"));
assert_eq!(Config::field_count(), 2);
}
Use #[field_meta(alias = "...")] to create an alias that contains_field() will recognize:
use field_meta::FieldMeta;
#[derive(FieldMeta)]
struct Person {
#[field_meta(alias = "username")]
name: String,
email: String,
}
fn main() {
// Both the original name and alias work
assert!(Person::contains_field("name"));
assert!(Person::contains_field("username"));
}
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.