#struct-fields #field-name #enums #type-name #deriving #structs #macros

macro field_types

Some derive macros for deriving enums, corresponding to the fields of structs

4 stable releases

Uses old Rust 2015

1.1.0 Nov 2, 2018
1.0.3 Nov 1, 2018

#17 in #type-name

Download history 47/week @ 2023-11-20 35/week @ 2023-11-27 36/week @ 2023-12-04 56/week @ 2023-12-11 52/week @ 2023-12-18 24/week @ 2023-12-25 35/week @ 2024-01-01 67/week @ 2024-01-08 54/week @ 2024-01-15 65/week @ 2024-01-22 25/week @ 2024-01-29 58/week @ 2024-02-05 51/week @ 2024-02-12 70/week @ 2024-02-19 86/week @ 2024-02-26 90/week @ 2024-03-04

298 downloads per month
Used in 11 crates (4 directly)

MIT license

17KB
241 lines

Field Types

Crates.io Docs

This crate provides FieldName and FieldType derive macros for deriving StructFieldName and StructFieldType enums for any struct Struct with some fields.

The ..FieldName enum contains unit types with names corresponding to the names of the structure fields. Additionally, you can get static string representation of a field name with name method and get ..FieldName variant by string with by_name method.

The FieldName usage example:

use field_types::FieldName;

#[derive(FieldName)]
struct Test {
    first: i32,
    second_field: Option<String>,
    #[field_name(skip)]
    third: bool,
}

assert_eq!(TestFieldName::First.name(), "first");
assert_eq!(TestFieldName::SecondField.name(), "second_field");

assert_eq!(Some(TestFieldName::First), TestFieldName::by_name("first"));
assert_eq!(Some(TestFieldName::SecondField), TestFieldName::by_name("second_field"));
assert_eq!(None, TestFieldName::by_name("third"));

let fields = Test::as_field_name_array();
assert_eq!([TestFieldName::First, TestFieldName::SecondField], fields);

The ..FieldType enum contains some types with names corresponding to the names of the structure fields and with values corresponding to the value types of the structure fields.

The FieldType usage example:

use field_types::FieldType;
use variant_count::VariantCount;

#[derive(FieldType)]
#[field_type_derive(VariantCount)]
struct Test {
    first: i32,
    second_field: Option<String>,
    #[field_type(skip)]
    third: bool,
}

let test = Test {
    first: 1,
    second_field: Some("test".to_string()),
    third: true,
};

let fields: [TestFieldType; TestFieldType::VARIANT_COUNT] = test.into();
// or
// let fields = test.into_field_type_array();

assert!(match fields {
    [TestFieldType::First(1), TestFieldType::SecondField(Some(ref s))] if s == "test" => true,
    _ => false,
});

In both cases you can skip fields with #[attr(skip)] or #[attr = "skip"] field attributes, where attr is field_name for FieldName, field_type for FieldType or field_types for any field type derives. You can also specifying some derives for generated enums with #[attr_derive(..)] structure attribute, where attr_derive is field_name_derive, field_type_derive or field_types_derive. For example:

#[derive(FieldType, FieldName)]
#[field_types_derive(VariantCount, Debug, Clone, PartialEq)]
struct Test {
    first: i32,
    second: Option<String>,
    #[field_types(skip)]
    third: bool,
    #[field_name = "skip"]
    fourth: bool,
}

By default, FieldName has derive Debug, PartialEq, Eq, Clone and Copy. More usage examples see in tests directory.

Usage

If you're using Cargo, just add it to your Cargo.toml:

[dependencies]
field_types = "*"

License

MIT

Dependencies

~2.5MB
~53K SLoC