4 releases
0.2.0 | Aug 5, 2022 |
---|---|
0.1.2 | Jun 9, 2022 |
0.1.1 | Jun 8, 2022 |
0.1.0 | Jun 8, 2022 |
#1533 in Procedural macros
25KB
531 lines
This crate provides procedural macrs to generate functions that return static slices of struct field names or enum variant names.
Examples
use fievar::Fields;
#[derive(Fields)]
struct File {
id: String,
name: String,
mime_type: String,
}
assert_eq!(&["id", "name", "mime_type"], File::fields());
You can also rename fields.
use fievar::Fields;
#[derive(Fields)]
struct File {
id: String,
name: String,
#[fievar(name = "mimeType")]
mime_type: String,
}
assert_eq!(&["id", "name", "mimeType"], File::fields());
Transformations
Syntax
Expression:
[[T][|
Sep]]
Sep: Word separator.
Can be any text.
T:
Determines how to transform field/variant.
[[TrCase][
NumAlign]]
NumAlign: Controls alignment of numerals.
1__
| __1
| _1_
TrCase: Controls the case of letters.
[TrWord[
TrWord[
TrWord]]]
TrWord: Controls the case of individual words.
[TrChar[TrChar[TrChar]]]
TrChar: Controls the case of individual characters in words.
c
| C
TrCase consists of upto three TrWords separated by a space. If there is only one TrWord then it is used to transform all words in field/variant. If there are two TrWords then the first TrWord is applied to the first word of the field/variant and the second TrWord is applied to the rest of the words. If there are three TrWords then the first and last TrWords are applied to the first and last words of the field/variant and the second TrWord is applied to the rest of the words. TrChars work similarly on characters in a word.
Examples
use fievar::Variants;
#[derive(Variants)]
enum E {
#[fievar(transform = "c")] // lowercase
AVeryLong0Variant,
#[fievar(transform = "C")] // uppercase
AVeryLong1Variant,
#[fievar(transform = "1__|_")] // align numeral left
AVeryLong2Variant,
#[fievar(transform = "__1|_")] // align numeral right
AVeryLong3Variant,
#[fievar(transform = "_1_|_")] // align numeral middle
AVeryLong4Variant,
#[fievar(transform = "c Cc")] // camelCase
AVeryLong5Variant,
#[fievar(transform = "c|_")] // snake_case
AVeryLong6Variant,
#[fievar(transform = "CcC cCc CcC _1_|*-*")] // something different
LastVeryLong7Variant,
}
let v = &[
"averylong0variant",
"AVERYLONG1VARIANT",
"A_Very_Long2_Variant",
"A_Very_Long_3Variant",
"A_Very_Long_4_Variant",
"aVeryLong5Variant",
"a_very_long6_variant",
"LasT*-*vERy*-*lONg*-*7*-*VarianT"
];
assert_eq!(v, E::variants());
Dependencies
~1.5MB
~37K SLoC