Crates.io | field_types |
lib.rs | field_types |
version | 1.1.0 |
source | src |
created_at | 2018-11-01 15:53:34.824174 |
updated_at | 2018-11-02 19:06:49.315368 |
description | Some derive macros for deriving enums, corresponding to the fields of structs. |
homepage | |
repository | https://github.com/XX/field_types |
max_upload_size | |
id | 94045 |
size | 31,927 |
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.
If you're using Cargo, just add it to your Cargo.toml:
[dependencies]
field_types = "*"
MIT