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
//! Derive macros that generate enums from struct fields.
//!
//! | Macro | Generated type | Conversion |
//! |-------|---------------|------------|
//! | [`FieldName`] | Unit enum — one variant per field name | `From<&Struct>` → `[FieldName; N]` |
//! | [`FieldType`] | Tuple enum — one variant per field value | `From<Struct>` → `[FieldType; N]` |
use DeriveFieldName;
use DeriveFieldType;
use TokenStream;
use DeriveInput;
/// Generates `{StructName}FieldType`, an enum whose variants wrap struct field values.
/// For enum variants without values use `FieldName` instead.
///
/// Each non-skipped field becomes a variant `VariantName(FieldType)` where the variant name is
/// the field name in `PascalCase`. The variants are ordered by field declaration order.
///
/// **No derives are added by default.** Add derives with `#[stem_type_derive(...)]`.
///
/// # Attributes
/// | Attribute | Target | Description |
/// |-----------|--------|-------------|
/// | `#[stem_type(skip)]` | field | Exclude this field from the generated enum. |
/// | `#[stem_type(nested)]` | field | Flatten the nested struct's `FieldType` variants into this enum. |
/// | `#[stem_type_derive(...)]` | struct | Derives for the generated enum. None are added by default. |
/// | `#[stem_type_attr(...)]` | struct | Extra attributes applied verbatim to the generated enum. |
///
/// All `stem_type*` attributes have short aliases: `ste_type`, `ste_type_derive`, `ste_type_attr`.
///
/// # Generated items
///
/// For a struct `Foo` with `N` non-skipped fields, this macro generates:
///
/// ```text
/// enum FooFieldType { Field1(T1), Field2(T2), ... }
///
/// impl From<Foo> for [FooFieldType; N] { ... }
/// ```
///
/// # Example
///
/// ```rust
/// use struct_to_enum::FieldType;
///
/// #[derive(Clone)]
/// #[derive(FieldType)]
/// #[stem_type_derive(Debug, PartialEq, Clone)]
/// struct Config {
/// width: u32,
/// height: u32,
/// #[stem_type(skip)]
/// name: String,
/// }
///
/// // Generated: enum ConfigFieldType { Width(u32), Height(u32) }
///
/// let cfg = Config { width: 1920, height: 1080, name: "hd".into() };
/// let fields: [ConfigFieldType; 2] = cfg.into();
///
/// assert_eq!(fields[0], ConfigFieldType::Width(1920));
/// assert_eq!(fields[1], ConfigFieldType::Height(1080));
/// ```
///
/// # Flattening nested structs
///
/// Mark a field with `#[stem_type(nested)]` to inline the variants of a nested struct
/// (which must also derive `FieldType`) directly into the parent enum. Nesting can be
/// arbitrarily deep.
///
/// ```rust
/// use struct_to_enum::FieldType;
///
/// #[derive(FieldType)]
/// #[stem_type_derive(Debug, PartialEq)]
/// struct Color {
/// r: u8,
/// g: u8,
/// b: u8,
/// }
///
/// #[derive(FieldType)]
/// #[stem_type_derive(Debug, PartialEq)]
/// struct Pixel {
/// x: i32,
/// y: i32,
/// #[stem_type(nested)]
/// color: Color,
/// }
///
/// // PixelFieldType has variants: X(i32), Y(i32), R(u8), G(u8), B(u8)
///
/// let p = Pixel { x: 10, y: 20, color: Color { r: 255, g: 128, b: 0 } };
/// let fields: [PixelFieldType; 5] = p.into();
/// assert_eq!(fields[0], PixelFieldType::X(10));
/// assert_eq!(fields[2], PixelFieldType::R(255));
/// ```
///
/// # Generics
///
/// Generic structs are supported. The generated enum carries the same type parameters:
///
/// ```rust
/// use struct_to_enum::FieldType;
///
/// #[derive(FieldType)]
/// #[stem_type_derive(Debug, PartialEq)]
/// struct Pair<A, B> {
/// first: A,
/// second: B,
/// }
///
/// // Generated: enum PairFieldType<A, B> { First(A), Second(B) }
///
/// let fields: [PairFieldType<i32, &str>; 2] = Pair { first: 42_i32, second: "hi" }.into();
/// assert_eq!(fields[0], PairFieldType::First(42));
/// ```
///
/// # Combining with other derives
///
/// Use `#[stem_type_derive]` and `#[stem_type_attr]` to pass anything to the generated enum.
/// This works with crates like [`strum`](https://docs.rs/strum):
///
/// ```rust
/// use struct_to_enum::FieldType;
/// use strum::VariantNames;
///
/// #[derive(FieldType)]
/// #[stem_type_derive(Debug, strum_macros::VariantNames)]
/// #[stem_type_attr(strum(serialize_all = "SCREAMING-KEBAB-CASE"))]
/// struct Request {
/// user_id: u64,
/// payload: Vec<u8>,
/// }
///
/// assert_eq!(RequestFieldType::VARIANTS, ["USER-ID", "PAYLOAD"]);
/// ```
/// Generates `{StructName}FieldName`, an enum whose variants represent struct field names.
/// For enum variants with values use `FieldType` instead.
///
/// Each non-skipped field becomes a unit variant in `PascalCase`. The variants are ordered by
/// field declaration order.
///
/// The generated enum derives `Debug`, `PartialEq`, `Eq`, `Clone`, and `Copy` by default.
/// They can be removed by adding `no_defaults` to the `stem_name_derive` attribute.
/// Use `#[stem_name_derive(...)]` to add more derives - the defaults are merged with whatever
/// you specify, so you only need to list derives not already in the default set.
///
/// # Attributes
/// | Attribute | Target | Description |
/// |-----------|--------|-------------|
/// | `#[stem_name(skip)]` | field | Exclude this field from the generated enum. |
/// | `#[stem_name(nested)]` | field | Flatten the nested struct's `FieldName` variants into this enum. |
/// | `#[stem_name_derive(...)]` | struct | Merge additional derives onto the generated enum (defaults are kept). |
/// | `#[stem_name_attr(...)]` | struct | Extra attributes applied verbatim to the generated enum. |
///
/// All `stem_name*` attributes have short aliases: `ste_name`, `ste_name_derive`, `ste_name_attr`.
///
/// # Generated items
///
/// For a struct `Foo` with `N` non-skipped fields, the macro generates:
///
/// ```text
/// enum FooFieldName { Field1, Field2, ... }
///
/// impl FieldNames<N> for Foo { ... }
/// ```
///
/// # Example
///
/// ```rust
/// use struct_to_enum::{FieldName, FieldNames};
///
/// #[derive(FieldName)]
/// struct User {
/// id: u64,
/// user_name: String,
/// #[stem_name(skip)]
/// internal_token: String,
/// }
///
/// // Generated: enum UserFieldName { Id, UserName } (Debug, PartialEq, Eq, Clone, Copy)
///
/// let names: [UserFieldName; 2] = User::field_names();
/// assert_eq!(names, [UserFieldName::Id, UserFieldName::UserName]);
/// ```
///
/// # Flattening nested structs
///
/// Mark a field with `#[stem_name(nested)]` to inline the variants of a nested struct
/// (which must also derive `FieldName`) directly into the parent enum. Nesting can be
/// arbitrarily deep.
///
/// ```rust
/// use struct_to_enum::{FieldName, FieldNames};
///
/// #[derive(FieldName)]
/// pub struct Address {
/// pub street: String,
/// pub city: String,
/// }
///
/// #[derive(FieldName)]
/// struct Person {
/// name: String,
/// #[stem_name(nested)]
/// address: Address,
/// }
///
/// // PersonFieldName: Name, Street, City (Address's variants are inlined)
///
/// let fields: [PersonFieldName; 3] = Person::field_names();
/// assert_eq!(fields, [PersonFieldName::Name, PersonFieldName::Street, PersonFieldName::City]);
/// ```
///
/// # Generics
///
/// Generic structs are supported. The `FieldNames` impl carries the same type parameters:
///
/// ```rust
/// use struct_to_enum::{FieldName, FieldNames};
///
/// #[derive(FieldName)]
/// struct Pair<A, B> {
/// first: A,
/// second: B,
/// }
///
/// // Generated: enum PairFieldName { First, Second }
/// assert_eq!(PairFieldName::First, PairFieldName::First);
/// ```
///
/// # Combining with other derives
///
/// Use `#[stem_name_derive]` and `#[stem_name_attr]` to pass anything to the generated enum.
/// This works with crates like [`strum`](https://docs.rs/strum):
///
/// ```rust
/// use struct_to_enum::FieldName;
/// use strum_macros::EnumString;
///
/// #[derive(FieldName)]
/// #[stem_name_derive(EnumString)]
/// #[stem_name_attr(strum(ascii_case_insensitive))]
/// struct Query {
/// user_id: u64,
/// status: String,
/// }
///
/// // Default derives (Debug, PartialEq, Eq, Clone, Copy) are merged with EnumString.
/// let variant: QueryFieldName = "userid".parse().unwrap(); // case-insensitive
/// assert_eq!(variant, QueryFieldName::UserId);
/// ```