# field-meta
A Rust procedural macro that provides compile-time field metadata access for structs.
## Overview
`field-meta` is a derive macro that automatically generates methods to introspect struct fields at compile time. It allows you to:
- Get a list of all field names in a struct
- Check if a field exists by name
- Count the number of fields
- Optionally skip fields from being included in metadata
## Installation
Add this to your `Cargo.toml`:
```toml
[dependencies]
field-meta = "0.1.1"
```
## Usage
### Basic Example
```rust
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);
}
```
### Skipping Fields
You can exclude fields from the metadata using the `#[field_meta(skip)]` attribute:
```rust
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);
}
```
### Field Aliases
Use `#[field_meta(alias = "...")]` to create an alias that `contains_field()` will recognize:
```rust
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"));
}
```
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## License
This project is licensed under the MIT License - see the LICENSE file for details.