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/// #[name = "Baz"]
39/// Bar,
40/// }
41///
42/// assert_eq!(Words::Foo.name(), "Foo");
43/// assert_eq!(Words::Bar.name(), "Baz");
44/// ```
45pub use enum_fun_macros::Name;
46
47/// Generate a method `name_plural(&self) -> &'static str` that
48/// returns the pluralized name of a given enum variant.
49///
50/// If the `name-trait` feature is enabled, an implementation
51/// of a `NamePlural` trait is generated. Otherwise,
52/// an inherent implementation is used.
53///
54/// The plural name of an enum variant is the same name that
55/// [`derive(Name)`](derive@Name) gave it, with an additional
56/// `"s"` appended. `Foo` becomes `"Foos"` and `HelloWorld`
57/// becomes `"Hello Worlds"`. This can be overridden by using
58/// the `#[name(plural = "...")]` attribute on a variant, e.g.:
59///
60/// ```rust
61/// use enum_fun::{Name, NamePlural};
62///
63/// #[derive(Name, NamePlural)]
64/// enum Words {
65/// Foo,
66/// #[name = "Baz"]
67/// Bar,
68/// #[name(plural = "Quuxes")]
69/// Quux,
70/// }
71///
72/// assert_eq!(Words::Foo.name_plural(), "Foos");
73/// assert_eq!(Words::Bar.name_plural(), "Bazs");
74/// assert_eq!(Words::Quux.name_plural(), "Quuxes");
75/// ```
76///
77/// A utility function `name_pluralized(&self, usize) -> &'static str`
78/// is also generated. It will return `name()` if the given
79/// value is `1`, and `name_plural()` otherwise. It does not
80/// prepend the provided number to the string.
81///
82/// ```rust
83/// # use enum_fun::{Name, NamePlural};
84/// # #[derive(Name, NamePlural)]
85/// # enum Words {
86/// # Foo,
87/// # }
88/// assert_eq!(Words::Foo.name_pluralized(1), "Foo");
89/// assert_eq!(Words::Foo.name_pluralized(2), "Foos");
90/// ```
91///
92/// If the `name-includes-plural` feature is enabled,
93/// `NamePlural` does not exist and all of its features
94/// are part of [`Name`](derive@Name) instead. You may use
95/// this if all your named enums must also support
96/// pluralization, to save a derive.
97#[cfg(not(feature = "name-includes-plural"))]
98pub use enum_fun_macros::NamePlural;
99
100/// Generates inherent constants and an iterator method
101/// to enable iterating and indexing the enum variants.
102///
103/// ```rust
104/// use enum_fun::Variants;
105///
106/// #[derive(Debug, PartialEq, Variants)]
107/// enum Words {
108/// Foo,
109/// Bar,
110/// Baz,
111/// }
112///
113/// use Words::*;
114///
115/// assert_eq!(Words::VARIANT_COUNT, 3);
116/// assert_eq!(Words::VARIANTS, [Foo, Bar, Baz]);
117/// assert_eq!(Words::variants().collect::<Vec<_>>(), vec![Foo, Bar, Baz]);
118/// ```
119///
120/// The return type of the `variants()` method is an `impl ExactSizeIterator<Item = Self>`.
121///
122/// Only enums without fields are supported.
123pub use enum_fun_macros::Variants;