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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! # enum-stringify
//!
//! A procedural macro that derives implementations for:
//! - [`std::fmt::Display`]: Converts enum variants to their string representations.
//! - [`std::str::FromStr`]: Parses a string into an enum variant.
//! - [`TryFrom<&str>`] and [`TryFrom<String>`]: Alternative conversion methods.
//!
//! ## Example
//!
//! ```
//! use enum_stringify::EnumStringify;
//! use std::str::FromStr;
//!
//! #[derive(EnumStringify, Debug, PartialEq)]
//! enum Numbers {
//! One,
//! Two,
//! }
//!
//! assert_eq!(Numbers::One.to_string(), "One");
//! assert_eq!(Numbers::Two.to_string(), "Two");
//!
//!
//! assert_eq!(Numbers::try_from("One").unwrap(), Numbers::One);
//! assert_eq!(Numbers::try_from("Two").unwrap(), Numbers::Two);
//!
//! assert!(Numbers::try_from("Three").is_err());
//! ```
//!
//! ## Custom Prefix and Suffix
//!
//! You can add a prefix and/or suffix to the string representation:
//!
//! ```
//! use enum_stringify::EnumStringify;
//!
//! #[derive(EnumStringify, Debug, PartialEq)]
//! #[enum_stringify(prefix = "Pre", suffix = "Post")]
//! enum Numbers {
//! One,
//! Two,
//! }
//!
//! assert_eq!(Numbers::One.to_string(), "PreOnePost");
//! assert_eq!(Numbers::try_from("PreOnePost").unwrap(), Numbers::One);
//! ```
//!
//! ## Case Conversion
//!
//! Convert enum variant names to different cases using the [`convert_case`] crate.
//!
//! ```
//! use enum_stringify::EnumStringify;
//!
//! #[derive(EnumStringify, Debug, PartialEq)]
//! #[enum_stringify(case = "flat")]
//! enum Numbers {
//! One,
//! Two,
//! }
//!
//! assert_eq!(Numbers::One.to_string(), "one");
//! assert_eq!(Numbers::try_from("one").unwrap(), Numbers::One);
//! ```
//!
//! ## Rename Variants
//!
//! Customize the string representation of specific variants:
//!
//! ```
//! use enum_stringify::EnumStringify;
//!
//! #[derive(EnumStringify, Debug, PartialEq)]
//! enum Istari {
//! #[enum_stringify(rename = "Ólorin")]
//! Gandalf,
//! Saruman,
//! }
//!
//! assert_eq!(Istari::Gandalf.to_string(), "Ólorin");
//! assert_eq!(Istari::try_from("Ólorin").unwrap(), Istari::Gandalf);
//! ```
//!
//! This takes precedence over the other attributes :
//!
//! ```
//! use enum_stringify::EnumStringify;
//!
//! #[derive(EnumStringify, Debug, PartialEq)]
//! #[enum_stringify(prefix = "Pre", suffix = "Post", case = "upper")]
//! enum Istari {
//! #[enum_stringify(rename = "Ólorin")]
//! Gandalf,
//! }
//!
//! assert_eq!(Istari::Gandalf.to_string(), "Ólorin");
//! assert_eq!(Istari::try_from("Ólorin").unwrap(), Istari::Gandalf);
//! ```
//!
//! ## Using All Options Together
//!
//! You can combine all options: renaming, prefix, suffix, and case conversion.
//!
//! ```
//! use enum_stringify::EnumStringify;
//!
//! #[derive(EnumStringify, Debug, PartialEq)]
//! #[enum_stringify(prefix = "Pre", suffix = "Post", case = "upper_flat")]
//! enum Status {
//! #[enum_stringify(rename = "okay")]
//! Okk,
//! Error3,
//! }
//!
//! assert_eq!(Status::Okk.to_string(), "okay");
//! assert_eq!(Status::Error3.to_string(), "PREERROR3POST");
//!
//! assert_eq!(Status::try_from("okay").unwrap(), Status::Okk);
//! assert_eq!(Status::try_from("PREERROR3POST").unwrap(), Status::Error3);
//! ```
//!
//! And using another case :
//!
//!
//! ```
//! use enum_stringify::EnumStringify;
//!
//! #[derive(EnumStringify, Debug, PartialEq)]
//! #[enum_stringify(prefix = "Pre", suffix = "Post", case = "upper")]
//! enum Status {
//! #[enum_stringify(rename = "okay")]
//! Okk,
//! Error3,
//! }
//!
//! assert_eq!(Status::Okk.to_string(), "okay");
//! assert_eq!(Status::Error3.to_string(), "PRE ERROR 3 POST");
//!
//! assert_eq!(Status::try_from("okay").unwrap(), Status::Okk);
//! assert_eq!(Status::try_from("PRE ERROR 3 POST").unwrap(), Status::Error3);
//! ```
//!
//! ## Error Handling
//!
//! When conversion from a string fails, the error type is `String`, containing a descriptive message:
//!
//! ```
//! use enum_stringify::EnumStringify;
//!
//! #[derive(EnumStringify, Debug, PartialEq)]
//! #[enum_stringify(case = "lower")]
//! enum Numbers {
//! One,
//! Two,
//! }
//!
//! let result = Numbers::try_from("Three");
//! assert!(result.is_err());
//! assert_eq!(result.unwrap_err(), "Failed to parse string 'Three' for enum Numbers");
//! ```
//!
//! ## Generated Implementations
//!
//! The macro generates the following trait implementations:
//!
//! ```rust, no_run
//! enum Numbers { One, Two }
//!
//! impl ::std::fmt::Display for Numbers {
//! fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//! match self {
//! Self::One => write!(f, "One"),
//! Self::Two => write!(f, "Two"),
//! }
//! }
//! }
//!
//! impl TryFrom<&str> for Numbers {
//! type Error = String;
//!
//! fn try_from(s: &str) -> Result<Self, Self::Error> {
//! match s {
//! "One" => Ok(Self::One),
//! "Two" => Ok(Self::Two),
//! _ => Err(format!("Invalid value '{}'", s)),
//! }
//! }
//! }
//!
//! impl TryFrom<String> for Numbers {
//! type Error = String;
//!
//! fn try_from(s: String) -> Result<Self, Self::Error> {
//! s.as_str().try_into()
//! }
//! }
//!
//! impl ::std::str::FromStr for Numbers {
//! type Err = String;
//!
//! fn from_str(s: &str) -> Result<Self, Self::Err> {
//! s.try_into()
//! }
//! }
//! ```
use ;
use TokenStream;
use quote;
use ;
/// Generates the implementation of `Display`, `FromStr`, `TryFrom<&str>`, and `TryFrom<String>`
/// for the given enum.
/// Implementation of [`std::fmt::Display`].
/// Implementation of [`TryFrom<&str>`].
/// Implementation of [`TryFrom<String>`].
/// Implementation of [`std::str::FromStr`].