enum_helper/lib.rs
1//! [](https://github.com/uimataso/enum-helper/actions/workflows/ci.yml)
2//! [](https://crates.io/crates/enum-helper)
3//! [](https://docs.rs/enum-helper)
4//! 
5//! 
6//!
7//! Yet another enum helper.
8//!
9//! This crate provides derive macros that generate common boilerplate for enums.
10//!
11//! - [`EnumStr`]: convert between enum and string
12//! - [`EnumAll`]: get an array of all variants
13//! - [`EnumKind`]: generate a unit kind enum from a data-carrying enum
14//!
15//! ## Feature flags
16//!
17//! - `derive` (default): re-exports derive macros
18//! - `serde`: provides serde helpers for use with [`EnumStr`]
19//!
20//! [`EnumStr`]: derive@enum_helper_derive::EnumStr
21//! [`EnumAll`]: derive@enum_helper_derive::EnumAll
22//! [`EnumKind`]: derive@enum_helper_derive::EnumKind
23
24#[cfg(feature = "serde")]
25pub mod serde;
26
27#[cfg(feature = "derive")]
28pub use enum_helper_derive::EnumAll;
29#[cfg(feature = "derive")]
30pub use enum_helper_derive::EnumKind;
31#[cfg(feature = "derive")]
32pub use enum_helper_derive::EnumStr;
33
34/// Convert between enum and string.
35///
36/// Derive this trait on unit enums via [`#[derive(EnumStr)]`](derive@enum_helper_derive::EnumStr).
37pub trait EnumStr: Sized {
38 /// Returns the primary string name of this variant.
39 fn as_name(&self) -> &'static str;
40
41 /// Returns all string names and aliases of this variant.
42 ///
43 /// The first element is always the primary name (returned by [`as_name`](EnumStr::as_name)).
44 /// Subsequent elements are aliases added via `alias_all` and `alias`.
45 fn as_aliases(&self) -> &'static [&'static str];
46}
47
48/// Get an array of all variants of an enum.
49///
50/// Derive this trait on unit enums via [`#[derive(EnumAll)]`](derive@enum_helper_derive::EnumAll).
51pub trait EnumAll: Sized {
52 /// Array type that holds every variant.
53 type All;
54
55 /// An array containing all variants of the enum.
56 const ALL: Self::All;
57}
58
59/// Get the kind of an enum variant.
60///
61/// Derive this trait on enums via [`#[derive(EnumKind)]`](derive@enum_helper_derive::EnumKind).
62pub trait EnumKind: Sized {
63 /// The unit kind enum representing the kinds of this enum.
64 type Kind;
65
66 /// Returns the kind of this variant.
67 fn kind(&self) -> Self::Kind;
68}