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
use crate::attr;
use proc_macro2::Span;
use std::convert::TryFrom;
use syn::{
parse::{self, Parse, ParseStream},
parse_quote,
punctuated::Punctuated,
Attribute, Expr, Ident, Lit, Token, Visibility,
};
/// Represents the context for generating enum IDs, holding relevant attributes.
#[derive(Clone, Debug, Default)]
pub struct Context {
/// A list of attributes applied to the enum.
pub attrs: Vec<attr::Attr>,
}
impl Context {
/// Creates a new `Context` with the provided attributes.
///
/// # Arguments
///
/// * `attrs` - A vector of `Attr` attributes.
pub(self) fn new(attrs: Vec<attr::Attr>) -> Self {
Self { attrs }
}
/// Determines `display` is required
pub fn display_required(&self) -> bool {
self.attrs
.iter()
.any(|at| matches!(at, attr::Attr::Display))
}
/// Determines `display_variant` is required
pub fn display_variant(&self) -> bool {
self.attrs
.iter()
.any(|at| matches!(at, attr::Attr::DisplayVariant))
}
/// Determines `display_variant_snake` is required
pub fn display_variant_snake(&self) -> bool {
self.attrs
.iter()
.any(|at| matches!(at, attr::Attr::DisplayVariantSnake))
}
/// Determines `iterator` is required
pub fn iterator(&self) -> bool {
self.attrs
.iter()
.any(|at| matches!(at, attr::Attr::Iterator))
}
/// Determines `display_from_value` is required
pub fn display_from_value_required(&self) -> bool {
self.attrs
.iter()
.any(|at| matches!(at, attr::Attr::DisplayFromValue))
}
/// Determines the name of the generated ID enum.
///
/// If an `EnumName` attribute is present, its value is used.
/// Otherwise, the default naming convention (`ParentNameId`) is applied.
///
/// # Arguments
///
/// * `src` - The identifier of the source enum.
///
/// # Returns
///
/// * An `Ident` representing the name of the generated ID enum.
pub fn enum_name(&self, src: &Ident) -> Ident {
let name = self
.attrs
.iter()
.find_map(|at| {
if let attr::Attr::EnumName(name) = at {
Some(name.to_owned())
} else {
None
}
})
.unwrap_or(format!("{src}Id"));
Ident::new(&name, src.span())
}
/// Determines the name of the getter method for the ID.
///
/// If a `Getter` attribute is present, its value is used.
/// Otherwise, the default method name `id` is applied.
///
/// # Arguments
///
/// * `src` - The identifier of the source enum.
///
/// # Returns
///
/// * An `Ident` representing the name of the getter method.
pub fn getter_name(&self, src: &Ident) -> Ident {
let name = self
.attrs
.iter()
.find_map(|at| {
if let attr::Attr::Getter(name) = at {
Some(name.to_owned())
} else {
None
}
})
.unwrap_or(String::from("id"));
Ident::new(&name, src.span())
}
/// Determines the visibility of the generated ID enum.
///
/// - If the `Public` attribute is present, the enum is made public.
/// - If the `NotPublic` attribute is present, the enum is made private.
/// - Otherwise, the visibility is inherited from the source enum.
///
/// # Arguments
///
/// * `vis` - The visibility of the source enum.
///
/// # Returns
///
/// * A `Visibility` instance representing the desired visibility.
pub fn visibility(&self, vis: &Visibility) -> Visibility {
if self.attrs.iter().any(|at| matches!(at, attr::Attr::Public)) {
Visibility::Public(syn::token::Pub(proc_macro2::Span::call_site()))
} else if self
.attrs
.iter()
.any(|at| matches!(at, attr::Attr::NotPublic))
{
Visibility::Inherited
} else {
vis.clone()
}
}
/// Determines the derive attributes for the generated ID enum.
///
/// - If the `NoDerive` attribute is present, no derive attributes are added.
/// - If a `Derive` attribute is present, only the specified traits are derived.
/// - Otherwise, inherits derive attributes from the source enum.
///
/// # Arguments
///
/// * `attrs` - A slice of attributes from the source enum.
///
/// # Returns
///
/// * A vector of `Attribute` instances to be applied to the generated enum.
pub fn derive(&self, attrs: &[Attribute]) -> Vec<Attribute> {
if self
.attrs
.iter()
.any(|at| matches!(at, attr::Attr::NoDerive))
{
vec![]
} else if let Some(attr::Attr::Derive(list)) = self
.attrs
.iter()
.find(|at| matches!(at, attr::Attr::Derive(..)))
{
let traits: Vec<Ident> = list
.split(',')
.map(|s| Ident::new(s.trim(), Span::call_site()))
.collect();
vec![parse_quote! { #[derive(#(#traits),*)] }]
} else {
attrs
.iter()
.filter(|attr| attr.path().is_ident("derive"))
.cloned()
.collect()
}
}
}
impl Parse for Context {
/// Parses a stream of tokens into a `Context` struct.
///
/// The expected input can include:
/// - Attributes in the form of `key = "value"`
/// - Standalone attributes like `public`, `not_public`, `no_derive`
///
/// # Arguments
///
/// * `input` - The input stream of tokens.
///
/// # Returns
///
/// * A `Result` containing the parsed `Context` or a parsing error.
fn parse(input: ParseStream) -> parse::Result<Self> {
let mut attrs: Vec<attr::Attr> = vec![];
for expr in Punctuated::<Expr, Token![,]>::parse_terminated(input)? {
match expr {
Expr::Assign(a) => {
let link = a.clone();
if let (Expr::Path(left), Expr::Lit(right)) = (*a.left, *a.right) {
if let (Some(left), Lit::Str(value)) = (left.path.get_ident(), right.lit) {
let attr =
attr::Attr::try_from(left.to_string().as_ref()).map_err(|e| {
syn::Error::new(
left.span(),
format!("Cannot parse attribute \"{left}\": {e}"),
)
})?;
attrs.push(match attr {
attr::Attr::Derive(..) => attr::Attr::Derive(value.value()),
attr::Attr::Getter(..) => attr::Attr::Getter(value.value()),
attr::Attr::EnumName(..) => attr::Attr::EnumName(value.value()),
_ => {
return Err(syn::Error::new(
left.span(),
format!(
"Attribute \"{left}\" cannot be applied at this level"
),
));
}
});
} else {
return Err(syn::Error::new(
link.eq_token.span,
"Expecting expression like key = \"value as String\"",
));
}
} else {
return Err(syn::Error::new(
link.eq_token.span,
"Expecting expression like key = \"value as String\"",
));
}
}
Expr::Path(p) => {
if let Some(ident) = p.path.get_ident() {
let attr =
attr::Attr::try_from(ident.to_string().as_ref()).map_err(|e| {
syn::Error::new(
ident.span(),
format!("Cannot parse attribute: {ident} ({e})"),
)
})?;
attrs.push(match attr {
attr::Attr::NoDerive
| attr::Attr::NotPublic
| attr::Attr::Public
| attr::Attr::Display
| attr::Attr::DisplayVariant
| attr::Attr::DisplayVariantSnake
| attr::Attr::Iterator
| attr::Attr::DisplayFromValue => attr,
_ => {
return Err(syn::Error::new(
ident.span(),
format!(
"Attribute \"{ident}\" cannot be applied at this level"
),
));
}
});
} else {
return Err(syn::Error::new_spanned(p, "Cannot extract identifier"));
}
}
_ => {
return Err(syn::Error::new_spanned(
expr,
"Expecting expression like [key = \"value as String\"] or [key]",
));
}
}
}
Ok(Context::new(attrs))
}
}