#macro #proc-macro

macro struct-field-names-as-array

Provides a procedural macro that generates an array of the field names of a named struct

5 releases

0.1.4 Nov 15, 2022
0.1.3 Jan 19, 2022
0.1.2 Oct 12, 2021
0.1.1 Oct 8, 2021
0.1.0 Oct 7, 2021

#78 in Procedural macros

Download history 473/week @ 2022-10-08 559/week @ 2022-10-15 783/week @ 2022-10-22 685/week @ 2022-10-29 798/week @ 2022-11-05 776/week @ 2022-11-12 753/week @ 2022-11-19 789/week @ 2022-11-26 991/week @ 2022-12-03 846/week @ 2022-12-10 538/week @ 2022-12-17 186/week @ 2022-12-24 352/week @ 2022-12-31 823/week @ 2023-01-07 544/week @ 2023-01-14 587/week @ 2023-01-21

2,314 downloads per month
Used in 4 crates (3 directly)

MIT license

13KB
254 lines

struct-field-names-as-array

Build Status Codecov Latest Version Downloads Docs License: MIT

Provides the FieldNamesAsArray procedural macro. The macro adds the FIELD_NAMES_AS_ARRAY constant to the struct the macro is dervied on. The FIELD_NAMES_AS_ARRAY contains the field names of the given struct.

Note: The macro can only be derived from named structs.

Table of Contents

Usage

You can derive the FieldNamesAsArray macro 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"]);

Attributes

The FieldNamesAsArray macro supports the field_names_as_array attribute. field_names_as_array can be applied to the container or to a field with different arguments listed below.

Container Attributes

Container attributes are global attributes that change the behavior of the whole field names array, 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, that all field names must be given in snake_case. If not, 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 FIELD_NAMES_AS_ARRAY.

use struct_field_names_as_array::FieldNamesAsArray;

#[derive(FieldNamesAsArray)]
struct Foo {
  bar: String,
  baz: String,
  #[field_names_as_array(skip)]
  bat: String,
}

assert_eq!(Foo::FIELD_NAMES_AS_ARRAY, ["bar", "baz"]);

Visibility

The visibility of the FIELD_NAMES_AS_ARRAY is the same as the corresponding struct. E.g. is it pub struct Foo { ... }, the FIELD_NAMES_AS_ARRAY will be public as well. This, for example, will work:

mod foo {
  use struct_field_names_as_array::FieldNamesAsArray;

  #[derive(FieldNamesAsArray)]
  pub(super) struct Foo {
    bar: String,
    baz: String,
    bat: String,
  }
}

assert_eq!(foo::Foo::FIELD_NAMES_AS_ARRAY, ["bar", "baz", "bat"]);

Whereas this will not, since FIELD_NAMES_AS_ARRAY is private:

mod foo {
  use struct_field_names_as_array::FieldNamesAsArray;

  #[derive(FieldNamesAsArray)]
  struct Foo {
    bar: String,
    baz: String,
    bat: String,
  }
}

assert_eq!(foo::Foo::FIELD_NAMES_AS_ARRAY, ["bar", "baz", "bat"]);

Dependencies

~195–590KB
~14K SLoC