https://github.com/jofas/struct_field_names_as_array
Procedural macro that generates an array of the field names of a named struct
https://github.com/jofas/struct_field_names_as_array
macros rust rust-crate
Last synced: about 1 year ago
JSON representation
Procedural macro that generates an array of the field names of a named struct
- Host: GitHub
- URL: https://github.com/jofas/struct_field_names_as_array
- Owner: jofas
- License: other
- Created: 2021-10-07T14:31:41.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-11-26T12:52:29.000Z (over 1 year ago)
- Last Synced: 2025-03-28T22:35:26.614Z (over 1 year ago)
- Topics: macros, rust, rust-crate
- Language: Rust
- Homepage:
- Size: 91.8 KB
- Stars: 34
- Watchers: 3
- Forks: 4
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# struct-field-names-as-array
[](https://github.com/jofas/struct_field_names_as_array/actions/workflows/build.yml)
[](https://codecov.io/gh/jofas/struct_field_names_as_array)
[](https://crates.io/crates/struct-field-names-as-array)
[](https://crates.io/crates/struct-field-names-as-array)
[](https://docs.rs/struct-field-names-as-array/latest/struct_field_names_as_array)
[](https://opensource.org/licenses/MIT)
Provides the `FieldNamesAsArray` and `FieldNamesAsSlice` traits and
procedural macros for deriving them.
The traits contain associated constants
(`FieldNamesAsArray::FIELD_NAMES_AS_ARRAY` and `FieldNamesAsSlice::FIELD_NAMES_AS_SLICE`)
listing the field names of a struct.
**Note:** The macros can only be derived from named structs.
## Table of Contents
* [Usage](#usage)
* [Attributes](#attributes)
* [Container Attributes](#container-attributes)
* [Rename all](#rename-all)
* [Field Attributes](#field-attributes)
* [Skip](#skip)
* [Rename](#rename)
## Usage
You can derive the `FieldNamesAsArray` and `FieldNamesAsSlice` macros
like this:
```rust
use struct_field_names_as_array::FieldNamesAsArray;
#[derive(FieldNamesAsArray)]
struct Foo {
bar: String,
baz: String,
bat: String,
}
assert_eq!(Foo::FIELD_NAMES_AS_ARRAY, ["bar", "baz", "bat"]);
```
```rust
use struct_field_names_as_array::FieldNamesAsSlice;
#[derive(FieldNamesAsSlice)]
struct Foo {
bar: String,
baz: String,
bat: String,
}
assert_eq!(Foo::FIELD_NAMES_AS_SLICE, ["bar", "baz", "bat"]);
```
## Attributes
The `FieldNamesAsArray` macro comes with the
`field_names_as_array` attribute.
Orthogonally, `FieldNamesAsSlice` supports the `field_names_as_slice`
attribute with the same arguments.
The arguments are listed below.
### Container Attributes
Container attributes are global attributes that change the behavior
of the whole field names collection, rather than that of a single field.
#### Rename all
The `rename_all` attribute renames every field of the struct according
to the provided naming convention.
This attribute works exactly like the [serde][serde_rename_all]
equivalent.
Supported are these naming conventions:
- `lowercase`
- `UPPERCASE`
- `PascalCase`
- `camelCase`
- `snake_case`
- `SCREAMING_SNAKE_CASE`
- `kebab-case`
- `SCREAMING-KEBAB-CASE`
```rust
use struct_field_names_as_array::FieldNamesAsArray;
#[derive(FieldNamesAsArray)]
#[field_names_as_array(rename_all = "SCREAMING-KEBAB-CASE")]
struct Foo {
field_one: String,
field_two: String,
field_three: String,
}
assert_eq!(
Foo::FIELD_NAMES_AS_ARRAY,
["FIELD-ONE", "FIELD-TWO", "FIELD-THREE"],
);
```
**Note:** Same as serde's implementation of `rename_all`, it is
assumed that your field names follow the rust naming convention.
Namely, all field names must be given in `snake_case`.
If you don't follow this convention, applying `rename_all` may result
in unexpected field names.
### Field Attributes
Field attributes can be added to the fields of a named struct and
change the behavior of a single field.
#### Skip
The `skip` attribute removes the field from the generated constant.
```rust
use struct_field_names_as_array::FieldNamesAsSlice;
#[derive(FieldNamesAsSlice)]
struct Foo {
bar: String,
baz: String,
#[field_names_as_slice(skip)]
bat: String,
}
assert_eq!(Foo::FIELD_NAMES_AS_SLICE, ["bar", "baz"]);
```
#### Rename
The `rename` attribute renames the field in the generated constant.
```rust
use struct_field_names_as_array::FieldNamesAsArray;
#[derive(FieldNamesAsArray)]
struct Foo {
bar: String,
baz: String,
#[field_names_as_array(rename = "foo")]
bat: String,
}
assert_eq!(Foo::FIELD_NAMES_AS_ARRAY, ["bar", "baz", "foo"]);
```
[serde_rename_all]: https://serde.rs/container-attrs.html#rename_all