enum-methods
Enum getter/is_XXX method generation.
crates.io
docs.rs
Usage
In your Cargo.toml
, add this line under your [dependencies]
section:
= "0.0.4"
To use, simply derive and call methods (see the example below).
Why?
Usually when you write an enum with one or zero values, you might want to add a set of getters for them. As such:
But this gets tedious, and adds a lot code for this simple functionality.
Enter enum-methods
.
Instead of doing the above with the if let ... else { panic!(...) }
, you
simply derive from the EnumGetters
extern crate enum_methods;
Requirements and gotchas
Right now, enum-methods
has only two derivable options:
EnumGetters
EnumIsA
EnumGetters
has a couple of limitations.
- Any enum variant which has exactly 1 member will have a getter generated for it. All other variants are ignored.
- Enums which derive from
EnumGetters
must also derive fromDebug
- this is for when a method is called for the wrong variant and needs topanic!
.
EnumIsA
is much simpler than the previous; it simply adds is_XXX
methods returning a boolean for whether the variant matches or not.
For both methods, all names are automatically converted to snake_case.
License
This software is released under the Apache license 2.0. See the LICENSE file for more details.