Crates.io | struct-field-names-as-array-derive |
lib.rs | struct-field-names-as-array-derive |
version | 0.3.0 |
source | src |
created_at | 2023-10-10 09:01:48.269663 |
updated_at | 2023-10-10 09:01:48.269663 |
description | Procedural macros for the struct-field-names-as-array crate |
homepage | https://github.com/jofas/struct_field_names_as_array |
repository | https://github.com/jofas/struct_field_names_as_array |
max_upload_size | |
id | 998943 |
size | 15,997 |
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.
You can derive the FieldNamesAsArray
and FieldNamesAsSlice
macros
like this:
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"]);
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"]);
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 are global attributes that change the behavior of the whole field names collection, rather than that of a single field.
The rename_all
attribute renames every field of the struct according
to the provided naming convention.
This attribute works exactly like the serde
equivalent.
Supported are these naming conventions:
lowercase
UPPERCASE
PascalCase
camelCase
snake_case
SCREAMING_SNAKE_CASE
kebab-case
SCREAMING-KEBAB-CASE
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 can be added to the fields of a named struct and change the behavior of a single field.
The skip
attribute removes the field from the generated constant.
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"]);