#field-name #struct-fields #reflection #constants #macro-derive #introspection

struct-field-names-as-array

Crate for generating the field names of named structs as constants

7 releases

0.3.0 Oct 10, 2023
0.2.0 Apr 27, 2023
0.1.4 Nov 15, 2022
0.1.3 Jan 19, 2022
0.1.2 Oct 12, 2021

#8 in #field-name

Download history 1612/week @ 2024-07-21 1683/week @ 2024-07-28 1924/week @ 2024-08-04 1507/week @ 2024-08-11 1756/week @ 2024-08-18 2068/week @ 2024-08-25 1806/week @ 2024-09-01 1813/week @ 2024-09-08 1571/week @ 2024-09-15 2154/week @ 2024-09-22 2819/week @ 2024-09-29 3154/week @ 2024-10-06 2903/week @ 2024-10-13 2754/week @ 2024-10-20 2431/week @ 2024-10-27 2324/week @ 2024-11-03

10,482 downloads per month
Used in 6 crates (5 directly)

MIT license

9KB

struct-field-names-as-array

Build Status Codecov Latest Version Downloads Docs License: 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

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"]);

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 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

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.

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"]);

Dependencies

~105KB