#struct-fields #field-name #type #field #enums #name #struct

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

#37 in #field-name

Download history 128/week @ 2024-03-13 90/week @ 2024-03-20 97/week @ 2024-03-27 110/week @ 2024-04-03 136/week @ 2024-04-10 232/week @ 2024-04-17 192/week @ 2024-04-24 141/week @ 2024-05-01 184/week @ 2024-05-08 332/week @ 2024-05-15 344/week @ 2024-05-22 409/week @ 2024-05-29 341/week @ 2024-06-05 412/week @ 2024-06-12 282/week @ 2024-06-19 417/week @ 2024-06-26

1,516 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
~54K SLoC