1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//! # `delve`
//!
//! A bunch of macros to improve working with enums and strings.
//!
//! [![github]](https://github.com/DexterHill0/delve) [![crates-io]](https://crates.io/crates/delve) [![docs-rs]](https://docs.rs/delve)
//!
//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
//!
// only for documentation
/// Implements `delve::VariantCount` which provides the number of variants within an enum.
/// See [`VariantCount`] for an example implementation.
pub use EnumVariantCount;
/// Implements `delve::VariantNames` which provides the names of all the variants within an enum.
/// See [`VariantNames`] for an example implementation.
pub use EnumVariantNames;
/// Implements `core::str::FromStr` on an enum.
///
/// # Example
///
/// ```rust
/// use core::str::FromStr;
/// use delve::EnumFromStr;
///
/// #[derive(EnumFromStr)]
/// enum Week {
/// Sunday,
/// Monday,
/// Tuesday,
/// // ...
/// }
///
/// assert_eq!(Ok(Week::Tuesday), Week::from_str("Tuesday"));
/// ```
pub use EnumFromStr;
/// Implements `delve::FieldNames` which provides the names of the fields within a struct variant.
/// See [`FieldNames`] for an example implementation.
pub use EnumFields;
/// Implements `delve::TupleCount` which provides the number of type arguments in a tuple variant.
/// See [`TupleCount`] for an example implementation.
pub use EnumTuples;
/// Implements `delve::ModifyField` which allows the modification of arguments within a tuple or struct variant.
/// See [`ModifyField`] for an example implementation.
pub use EnumModify;
/// Implements `From<TheEnum> for &'static str` and `From<&TheEnum> for &'static str` on an enum.
/// The Rust `std` provides a blanket implementation for the reverse so `Into<&'static str> for (&)TheEnum` is also implemented.
///
/// # Example
///
/// ```rust
/// use delve::EnumToStr;
///
/// #[derive(EnumToStr)]
/// enum Week {
/// Sunday,
/// Monday,
/// Tuesday,
/// // ...
/// }
///
/// assert_eq!("Sunday", <Week as Into<&'static str>>::into(Week::Sunday));
/// ```
pub use EnumToStr;
/// Implements `core::fmt::Display` on an enum.
///
/// # Example
///
/// ```rust
/// use delve::EnumDisplay;
///
/// #[derive(EnumDisplay)]
/// enum Week {
/// Sunday,
/// Monday,
/// Tuesday,
/// }
///
/// assert_eq!("Monday", format!("{}", Week::Monday));
/// ```
pub use EnumDisplay;
/// Implements `delve::HasVariant` which returns whether a given variant name exists in the enum.
/// See [`HasVariant`] for an example implementation.
pub use EnumHasVariant;
/// The error returned in `FromStr` if it failed to parse the string into a variant.
/// A trait for getting the number of variants in an enum.
/// Use [`delve_derive::EnumVariantCount`] to automatically derive this trait.
/// A trait for getting the names of the variants in an enum.
/// Use [`delve_derive::EnumVariantNames`] to automatically derive this trait.
/// A trait that returns whether a given variant name exists in the enum. Unlike checking if the variant name exists
/// using the `VariantNames` trait, this is *not* affected by inflections.
/// Use [`delve_derive::EnumHasVariant`] to automatically derive this trait.
///
/// # Example
///
/// ```rust
/// use delve::{EnumHasVariant, HasVariant};
///
/// #[derive(EnumHasVariant)]
/// enum Operators {
/// Add,
/// Div,
/// Sub,
/// Mul
/// }
///
/// assert!(Operators::has_variant("Div"));
/// ```
/// A trait that returns the field names from within a struct variant.
/// Use [`delve_derive::EnumFields`] to automatically derive this trait.
///
/// # Example
///
/// ```rust
/// use delve::{EnumFields, FieldNames};
///
/// #[derive(EnumFields)]
/// enum Color {
/// Red,
/// Custom {
/// r: usize,
/// g: usize,
/// b: usize,
/// }
/// }
///
/// assert_eq!(Some(&["r", "g", "b"][..]), Color::Custom { r: 255, g: 50, b: 100, }.field_names());
/// ```
/// A trait that returns the number of types within a tuple variant.
/// Use [`delve_derive::EnumTuples`] to automatically derive this trait.
///
/// # Example
///
/// ```rust
/// use delve::{EnumTuples, TupleCount};
///
/// #[derive(EnumTuples)]
/// enum Week {
/// Day(String, usize),
/// }
///
/// assert_eq!(Some(2), Week::Day("saturday".into()).tuple_count());
/// ```
/// A trait that allows the modification of arguments within a tuple or struct variant.
/// Use [`delve_derive::EnumModify`] to automatically derive this trait.
///
/// # Example
///
/// ```rust
/// use delve::{EnumModify, ModifyField};
///
/// #[derive(EnumTuples)]
/// enum Thing {
/// Object {
/// x: usize,
/// y: usize,
/// }
/// }
///
/// let obj = Thing::Object { x: 0, y: 0 }
///
/// assert_eq!(Some(&0), obj.get_field("x"));
/// obj.set_field("x", 100);
/// assert_eq!(Some(&100), obj.get_field("x"));
/// ```
///
/// # Note
///
/// Generics and lifetimes will cause conflicting implementations if used within a tuple / struct variant that is not skipped. The `#[delve(skip)]`
/// attribute can be applied inside of the tuple / struct variant so the type is skipped:
///
/// ```rust
/// #[derive(EnumTuples)]
/// enum Week<'w, W> {
/// Day(#[delve(skip)] &'w String)
/// Other {
/// #[delve(skip)]
/// name: W,
/// }
/// }
/// ```