enum-strings
#[derive(EnumStrings)] gives a unit enum one const fn per named string,
and refuses to compile unless every variant supplies every string.
use EnumStrings;
const HOME: &str = Home.path;
assert_eq!;
assert_eq!;
The derive expands to:
Why
Two match blocks do this job fine. What they don't do is keep the path and
the label next to the variant they belong to, and once a few of these string
tables pile up on one enum, the attribute form is easier to read and to
review.
Runtime property lookups (get_str("label") returning Option) put the
strings on the variant too, but a forgotten one is None at runtime. Here it
is a compile error on the variant that forgot it:
error: variant `Docs` is missing `label`
--> src/route.rs:8:5
|
8 | Docs,
| ^^^^
Rules
- The first variant declares which strings exist. Every other variant must supply exactly that set: no missing, no extra, no repeats.
- Keys become method names, so a typo at a call site is an ordinary "no method named" error instead of a silent miss.
- Only unit variants. Generated methods take
selfby value, so the enum should beCopy. - The methods are
pubwhen the enum is,#[must_use], and documented, somissing_docsstays quiet.
License
MIT OR Apache-2.0, at your option.