enum_fun/lib.rs
1//! Silly enum helpers.
2
3#[cfg(feature = "name-trait")]
4pub trait Name {
5 fn name(&self) -> &'static str;
6
7 #[cfg(feature = "name-includes-plural")]
8 fn name_plural(&self) -> &'static str;
9
10 #[cfg(feature = "name-includes-plural")]
11 fn name_pluralized(&self, count: usize) -> &'static str;
12}
13
14#[cfg(all(feature = "name-trait", not(feature = "name-includes-plural")))]
15pub trait NamePlural: Name {
16 fn name_plural(&self) -> &'static str;
17 fn name_pluralized(&self, count: usize) -> &'static str;
18}
19
20/// Generate a method `name(&self) -> &'static str` that
21/// returns the name of a given enum variant.
22///
23/// If the `name-trait` feature is enabled, an implementation
24/// of a `Name` trait is generated. Otherwise, an inherent
25/// implementation is used.
26///
27/// The name of an enum variant is its identifier in Title Case.
28/// `Foo` becomes `"Foo"` and `HelloWorld` becomes `"Hello World"`.
29/// This can be overridden by using the `#[name = "..."]` attribute
30/// on a variant, e.g.:
31///
32/// ```rust
33/// use enum_fun::Name;
34///
35/// #[derive(Name)]
36/// enum Words {
37/// Foo,
38/// HelloWorld,
39/// #[name = "Baz"]
40/// Bar,
41/// }
42///
43/// use Words::*;
44///
45/// assert_eq!(Foo.name(), "Foo");
46/// assert_eq!(HelloWorld.name(), "Hello World");
47/// assert_eq!(Bar.name(), "Baz");
48/// ```
49pub use enum_fun_macros::Name;
50
51/// Generate a method `name_plural(&self) -> &'static str` that
52/// returns the pluralized name of a given enum variant.
53///
54/// If the `name-trait` feature is enabled, an implementation
55/// of a `NamePlural` trait is generated. Otherwise,
56/// an inherent implementation is used.
57///
58/// The plural name of an enum variant is the same name that
59/// [`derive(Name)`](derive@Name) gave it, with an additional
60/// `"s"` appended. `Foo` becomes `"Foos"` and `HelloWorld`
61/// becomes `"Hello Worlds"`. This can be overridden by using
62/// the `#[name(plural = "...")]` attribute on a variant, e.g.:
63///
64/// ```rust
65/// use enum_fun::{Name, NamePlural};
66///
67/// #[derive(Name, NamePlural)]
68/// enum Words {
69/// Foo,
70/// HelloWorld,
71/// #[name = "Baz"]
72/// Bar,
73/// #[name(plural = "Quuxes")]
74/// Quux,
75/// }
76///
77/// use Words::*;
78///
79/// assert_eq!(Foo.name_plural(), "Foos");
80/// assert_eq!(HelloWorld.name_plural(), "Hello Worlds");
81/// assert_eq!(Bar.name_plural(), "Bazs");
82/// assert_eq!(Quux.name_plural(), "Quuxes");
83/// ```
84///
85/// A utility method `name_pluralized(&self, n: usize) -> &'static str`
86/// is also generated. It will return `name()` if `n`
87/// is `1`, and `name_plural()` otherwise. It does not
88/// prepend the provided number to the string.
89///
90/// ```rust
91/// # use enum_fun::{Name, NamePlural};
92/// # #[derive(Name, NamePlural)]
93/// # enum Words {
94/// # Foo,
95/// # }
96/// # use Words::Foo;
97/// assert_eq!(Foo.name_pluralized(1), "Foo");
98/// assert_eq!(Foo.name_pluralized(/* anything non-1 */ 2), "Foos");
99/// ```
100///
101/// If the `name-includes-plural` feature is enabled,
102/// `NamePlural` does not exist and all of its features
103/// are part of [`Name`](derive@Name) instead. You may use
104/// this if all your named enums must also support
105/// pluralization, to save a derive.
106#[cfg(not(feature = "name-includes-plural"))]
107pub use enum_fun_macros::NamePlural;
108
109/// Generates inherent constants and an iterator method
110/// to enable iterating and indexing the enum variants.
111///
112/// ```rust
113/// use enum_fun::Variants;
114///
115/// #[derive(Debug, PartialEq, Variants)]
116/// enum Words {
117/// Foo,
118/// Bar,
119/// Baz,
120/// }
121///
122/// use Words::*;
123///
124/// assert_eq!(Words::VARIANT_COUNT, 3);
125/// assert_eq!(Words::VARIANTS, [Foo, Bar, Baz]);
126/// assert_eq!(Words::variants().collect::<Vec<_>>(), vec![Foo, Bar, Baz]);
127/// ```
128///
129/// The return type of the `variants()` method is an `impl ExactSizeIterator<Item = Self>`.
130///
131/// Only enums without fields are supported.
132pub use enum_fun_macros::Variants;