Crate field_types

source ·
Expand description

This crate provides FieldName and FieldType derive macros for deriving enums, corresponding to the fields of structs.

Features

..FieldName enum

  • Variants with UpperCamelCase unit type names corresponding to the snake_case field names of the struct
  • Skipping fields with #[field_name(skip)] or #[field_types(skip)] attributes
  • Specifying some derives for generated enums with #[field_name_derive(..)] or #[field_types_derive(..)] structure attributes. By default, ..FieldName has derive Debug, PartialEq, Eq, Clone and Copy.
  • Associated function as_field_name_array that returns array of variants
  • From/Into convert the struct reference to an array of variants
  • name/by_name methods for convert enum variants to/from string representation field names

..FieldType enum

  • Variants with UpperCamelCase type names corresponding to the snake_case field names of the struct and with values corresponding to the value types of the struct fields
  • Skipping fields with #[field_type(skip)] or #[field_types(skip)] attributes
  • Specifying some derives for generated enums with #[field_type_derive(..)] or #[field_types_derive(..)] structure attributes
  • Associated function into_field_type_array that convert the struct into an array of variants with field values
  • Into convert the struct into an array of variants with field values

Example

extern crate variant_count;
extern crate field_types;

use variant_count::VariantCount;
use field_types::{FieldName, FieldType};

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

fn main() {
    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);

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

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

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

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

Usage

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

[dependencies]
field_types = "*"

Use FieldName and/or FieldType in derive struct attribute. !

Derive Macros