Enum Group
enum-group
is a simple derive macro crate that helps enum types to group their variants.
Annotate an enum with #[derive(EnumGroup)]
,
and mark variants some group label names with #[groups(label1, label2)]
will auto generate function is_label1
and is_label2
.
These functions will tell you if a variant of the enum belongs to this grouping.
Example
Group your variants
use EnumGroup;
// Will auto generate function `fn is_odd(&self) -> bool`
assert!;
assert!;
assert!;
assert!;
// Will auto generate function `fn is_even(&self) -> bool`
assert!;
assert!;
assert!;
assert!;
// Will auto generate function `fn is_prime(&self) -> bool`
assert!;
assert!;
assert!;
assert!;
Label name with prefix
Sometimes, you may have a large number of group label names with the same prefix, and you can use this nested grouping to reduce code duplication. It support multi-level nesting.
use EnumGroup;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
assert!;
Variant judgment functions
If you just want to determine which variant the current variant is and don't want to write too many cumbersome match expressions, You can use the following functions to get help quickly. Each variant provides a lowercase judgment function.
use EnumGroup;
assert!;
assert!;
assert!;
assert!;
Other help functions
Sometimes you may want to print each variant's name string,
you can use variant_name()
to get &str
.
use EnumGroup;
assert_eq!;
assert_eq!;
Usage Restrictions
Each character of the group label name for each variant must be lower case alphanumeric or _
.
Panic
use EnumGroup;