Skip to content

Commit 54e7ea3

Browse files
authored
Merge branch 'master' into fix-stroke-alignment-outside
2 parents c6ff7e6 + b44a4fb commit 54e7ea3

File tree

3 files changed

+244
-253
lines changed

3 files changed

+244
-253
lines changed

node-graph/node-macro/src/codegen.rs

Lines changed: 57 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,20 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
4141
let struct_generics: Vec<Ident> = fields.iter().enumerate().map(|(i, _)| format_ident!("Node{}", i)).collect();
4242
let input_ident = &input.pat_ident;
4343

44-
let field_idents: Vec<_> = fields
45-
.iter()
46-
.map(|field| match field {
47-
ParsedField::Regular { pat_ident, .. } | ParsedField::Node { pat_ident, .. } => pat_ident,
48-
})
49-
.collect();
44+
let field_idents: Vec<_> = fields.iter().map(|f| &f.pat_ident).collect();
5045
let field_names: Vec<_> = field_idents.iter().map(|pat_ident| &pat_ident.ident).collect();
5146

5247
let input_names: Vec<_> = fields
5348
.iter()
54-
.map(|field| match field {
55-
ParsedField::Regular { name, .. } | ParsedField::Node { name, .. } => name,
56-
})
49+
.map(|f| &f.name)
5750
.zip(field_names.iter())
5851
.map(|zipped| match zipped {
5952
(Some(name), _) => name.value(),
6053
(_, name) => name.to_string().to_case(Case::Title),
6154
})
6255
.collect();
6356

64-
let input_descriptions: Vec<_> = fields
65-
.iter()
66-
.map(|field| match field {
67-
ParsedField::Regular { description, .. } | ParsedField::Node { description, .. } => description,
68-
})
69-
.collect();
57+
let input_descriptions: Vec<_> = fields.iter().map(|f| &f.description).collect();
7058

7159
let struct_fields = field_names.iter().zip(struct_generics.iter()).map(|(name, r#gen)| {
7260
quote! { pub(super) #name: #r#gen }
@@ -84,9 +72,9 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
8472

8573
let field_types: Vec<_> = fields
8674
.iter()
87-
.map(|field| match field {
88-
ParsedField::Regular { ty, .. } => ty.clone(),
89-
ParsedField::Node { output_type, input_type, .. } => match parsed.is_async {
75+
.map(|field| match &field.ty {
76+
ParsedFieldType::Regular(RegularParsedField { ty, .. }) => ty.clone(),
77+
ParsedFieldType::Node(NodeParsedField { output_type, input_type, .. }) => match parsed.is_async {
9078
true => parse_quote!(&'n impl #graphene_core::Node<'n, #input_type, Output = impl core::future::Future<Output=#output_type>>),
9179
false => parse_quote!(&'n impl #graphene_core::Node<'n, #input_type, Output = #output_type>),
9280
},
@@ -95,24 +83,18 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
9583

9684
let widget_override: Vec<_> = fields
9785
.iter()
98-
.map(|field| {
99-
let parsed_widget_override = match field {
100-
ParsedField::Regular { widget_override, .. } => widget_override,
101-
ParsedField::Node { widget_override, .. } => widget_override,
102-
};
103-
match parsed_widget_override {
104-
ParsedWidgetOverride::None => quote!(RegistryWidgetOverride::None),
105-
ParsedWidgetOverride::Hidden => quote!(RegistryWidgetOverride::Hidden),
106-
ParsedWidgetOverride::String(lit_str) => quote!(RegistryWidgetOverride::String(#lit_str)),
107-
ParsedWidgetOverride::Custom(lit_str) => quote!(RegistryWidgetOverride::Custom(#lit_str)),
108-
}
86+
.map(|field| match &field.widget_override {
87+
ParsedWidgetOverride::None => quote!(RegistryWidgetOverride::None),
88+
ParsedWidgetOverride::Hidden => quote!(RegistryWidgetOverride::Hidden),
89+
ParsedWidgetOverride::String(lit_str) => quote!(RegistryWidgetOverride::String(#lit_str)),
90+
ParsedWidgetOverride::Custom(lit_str) => quote!(RegistryWidgetOverride::Custom(#lit_str)),
10991
})
11092
.collect();
11193

11294
let value_sources: Vec<_> = fields
11395
.iter()
114-
.map(|field| match field {
115-
ParsedField::Regular { value_source, .. } => match value_source {
96+
.map(|field| match &field.ty {
97+
ParsedFieldType::Regular(RegularParsedField { value_source, .. }) => match value_source {
11698
ParsedValueSource::Default(data) => quote!(RegistryValueSource::Default(stringify!(#data))),
11799
ParsedValueSource::Scope(data) => quote!(RegistryValueSource::Scope(#data)),
118100
_ => quote!(RegistryValueSource::None),
@@ -123,8 +105,8 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
123105

124106
let default_types: Vec<_> = fields
125107
.iter()
126-
.map(|field| match field {
127-
ParsedField::Regular { implementations, .. } => match implementations.first() {
108+
.map(|field| match &field.ty {
109+
ParsedFieldType::Regular(RegularParsedField { implementations, .. }) => match implementations.first() {
128110
Some(ty) => quote!(Some(concrete!(#ty))),
129111
_ => quote!(None),
130112
},
@@ -134,8 +116,8 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
134116

135117
let number_min_values: Vec<_> = fields
136118
.iter()
137-
.map(|field| match field {
138-
ParsedField::Regular { number_soft_min, number_hard_min, .. } => match (number_soft_min, number_hard_min) {
119+
.map(|field| match &field.ty {
120+
ParsedFieldType::Regular(RegularParsedField { number_soft_min, number_hard_min, .. }) => match (number_soft_min, number_hard_min) {
139121
(Some(soft_min), _) => quote!(Some(#soft_min)),
140122
(None, Some(hard_min)) => quote!(Some(#hard_min)),
141123
(None, None) => quote!(None),
@@ -145,8 +127,8 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
145127
.collect();
146128
let number_max_values: Vec<_> = fields
147129
.iter()
148-
.map(|field| match field {
149-
ParsedField::Regular { number_soft_max, number_hard_max, .. } => match (number_soft_max, number_hard_max) {
130+
.map(|field| match &field.ty {
131+
ParsedFieldType::Regular(RegularParsedField { number_soft_max, number_hard_max, .. }) => match (number_soft_max, number_hard_max) {
150132
(Some(soft_max), _) => quote!(Some(#soft_max)),
151133
(None, Some(hard_max)) => quote!(Some(#hard_max)),
152134
(None, None) => quote!(None),
@@ -156,77 +138,45 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
156138
.collect();
157139
let number_mode_range_values: Vec<_> = fields
158140
.iter()
159-
.map(|field| match field {
160-
ParsedField::Regular {
141+
.map(|field| match &field.ty {
142+
ParsedFieldType::Regular(RegularParsedField {
161143
number_mode_range: Some(number_mode_range),
162144
..
163-
} => quote!(Some(#number_mode_range)),
145+
}) => quote!(Some(#number_mode_range)),
164146
_ => quote!(None),
165147
})
166148
.collect();
167149
let number_display_decimal_places: Vec<_> = fields
168150
.iter()
169-
.map(|field| match field {
170-
ParsedField::Regular {
171-
number_display_decimal_places: Some(decimal_places),
172-
..
173-
}
174-
| ParsedField::Node {
175-
number_display_decimal_places: Some(decimal_places),
176-
..
177-
} => {
178-
quote!(Some(#decimal_places))
179-
}
180-
_ => quote!(None),
181-
})
182-
.collect();
183-
let number_step: Vec<_> = fields
184-
.iter()
185-
.map(|field| match field {
186-
ParsedField::Regular { number_step: Some(step), .. } | ParsedField::Node { number_step: Some(step), .. } => {
187-
quote!(Some(#step))
188-
}
189-
_ => quote!(None),
190-
})
151+
.map(|field| field.number_display_decimal_places.as_ref().map_or(quote!(None), |i| quote!(Some(#i))))
191152
.collect();
153+
let number_step: Vec<_> = fields.iter().map(|field| field.number_step.as_ref().map_or(quote!(None), |i| quote!(Some(#i)))).collect();
192154

193-
let unit_suffix: Vec<_> = fields
194-
.iter()
195-
.map(|field| match field {
196-
ParsedField::Regular { unit: Some(unit), .. } | ParsedField::Node { unit: Some(unit), .. } => {
197-
quote!(Some(#unit))
198-
}
199-
_ => quote!(None),
200-
})
201-
.collect();
155+
let unit_suffix: Vec<_> = fields.iter().map(|field| field.unit.as_ref().map_or(quote!(None), |i| quote!(Some(#i)))).collect();
202156

203157
let exposed: Vec<_> = fields
204158
.iter()
205-
.map(|field| match field {
206-
ParsedField::Regular { exposed, .. } => quote!(#exposed),
159+
.map(|field| match &field.ty {
160+
ParsedFieldType::Regular(RegularParsedField { exposed, .. }) => quote!(#exposed),
207161
_ => quote!(true),
208162
})
209163
.collect();
210164

211-
let eval_args = fields.iter().map(|field| match field {
212-
ParsedField::Regular { pat_ident, .. } => {
213-
let name = &pat_ident.ident;
214-
quote! { let #name = self.#name.eval(__input.clone()).await; }
215-
}
216-
ParsedField::Node { pat_ident, .. } => {
217-
let name = &pat_ident.ident;
218-
quote! { let #name = &self.#name; }
165+
let eval_args = fields.iter().map(|field| {
166+
let name = &field.pat_ident.ident;
167+
match &field.ty {
168+
ParsedFieldType::Regular { .. } => {
169+
quote! { let #name = self.#name.eval(__input.clone()).await; }
170+
}
171+
ParsedFieldType::Node { .. } => {
172+
quote! { let #name = &self.#name; }
173+
}
219174
}
220175
});
221176

222-
let min_max_args = fields.iter().map(|field| match field {
223-
ParsedField::Regular {
224-
pat_ident,
225-
number_hard_min,
226-
number_hard_max,
227-
..
228-
} => {
229-
let name = &pat_ident.ident;
177+
let min_max_args = fields.iter().map(|field| match &field.ty {
178+
ParsedFieldType::Regular(RegularParsedField { number_hard_min, number_hard_max, .. }) => {
179+
let name = &field.pat_ident.ident;
230180
let mut tokens = quote!();
231181
if let Some(min) = number_hard_min {
232182
tokens.extend(quote_spanned! {min.span()=>
@@ -241,15 +191,13 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
241191
}
242192
tokens
243193
}
244-
ParsedField::Node { .. } => {
245-
quote!()
246-
}
194+
ParsedFieldType::Node { .. } => quote!(),
247195
});
248196

249-
let all_implementation_types = fields.iter().flat_map(|field| match field {
250-
ParsedField::Regular { implementations, .. } => implementations.into_iter().cloned().collect::<Vec<_>>(),
251-
ParsedField::Node { implementations, .. } => implementations
252-
.into_iter()
197+
let all_implementation_types = fields.iter().flat_map(|field| match &field.ty {
198+
ParsedFieldType::Regular(RegularParsedField { implementations, .. }) => implementations.iter().cloned().collect::<Vec<_>>(),
199+
ParsedFieldType::Node(NodeParsedField { implementations, .. }) => implementations
200+
.iter()
253201
.flat_map(|implementation| [implementation.input.clone(), implementation.output.clone()])
254202
.collect(),
255203
});
@@ -260,11 +208,11 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
260208
let mut clampable_clauses = Vec::new();
261209

262210
for (field, name) in fields.iter().zip(struct_generics.iter()) {
263-
clauses.push(match (field, *is_async) {
211+
clauses.push(match (&field.ty, *is_async) {
264212
(
265-
ParsedField::Regular {
213+
ParsedFieldType::Regular(RegularParsedField {
266214
ty, number_hard_min, number_hard_max, ..
267-
},
215+
}),
268216
_,
269217
) => {
270218
let all_lifetime_ty = substitute_lifetimes(ty.clone(), "all");
@@ -284,7 +232,7 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
284232
#name: #graphene_core::Node<'n, #input_type, Output = #fut_ident> + #graphene_core::WasmNotSync
285233
)
286234
}
287-
(ParsedField::Node { input_type, output_type, .. }, true) => {
235+
(ParsedFieldType::Node(NodeParsedField { input_type, output_type, .. }), true) => {
288236
let id = future_idents.len();
289237
let fut_ident = format_ident!("F{}", id);
290238
future_idents.push(fut_ident.clone());
@@ -294,7 +242,7 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
294242
#name: #graphene_core::Node<'n, #input_type, Output = #fut_ident > + #graphene_core::WasmNotSync
295243
)
296244
}
297-
(ParsedField::Node { .. }, false) => unreachable!(),
245+
(ParsedFieldType::Node { .. }, false) => unreachable!(),
298246
});
299247
}
300248
let where_clause = where_clause.clone().unwrap_or(WhereClause {
@@ -454,9 +402,9 @@ fn generate_node_input_references(
454402
let (mut modified, mut generic_collector) = FilterUsedGenerics::new(fn_generics);
455403

456404
for (input_index, (parsed_input, input_ident)) in parsed.fields.iter().zip(field_idents).enumerate() {
457-
let mut ty = match parsed_input {
458-
ParsedField::Regular { ty, .. } => ty,
459-
ParsedField::Node { output_type, .. } => output_type,
405+
let mut ty = match &parsed_input.ty {
406+
ParsedFieldType::Regular(RegularParsedField { ty, .. }) => ty,
407+
ParsedFieldType::Node(NodeParsedField { output_type, .. }) => output_type,
460408
}
461409
.clone();
462410

@@ -540,20 +488,20 @@ fn generate_register_node_impl(parsed: &ParsedNodeFn, field_names: &[&Ident], st
540488
.fields
541489
.iter()
542490
.map(|field| {
543-
match field {
544-
ParsedField::Regular { implementations, ty, .. } => {
491+
match &field.ty {
492+
ParsedFieldType::Regular(RegularParsedField { implementations, ty, .. }) => {
545493
if !implementations.is_empty() {
546494
implementations.iter().map(|ty| (&unit, ty)).collect()
547495
} else {
548496
vec![(&unit, ty)]
549497
}
550498
}
551-
ParsedField::Node {
499+
ParsedFieldType::Node(NodeParsedField {
552500
implementations,
553501
input_type,
554502
output_type,
555503
..
556-
} => {
504+
}) => {
557505
if !implementations.is_empty() {
558506
implementations.iter().map(|impl_| (&impl_.input, &impl_.output)).collect()
559507
} else {
@@ -578,7 +526,7 @@ fn generate_register_node_impl(parsed: &ParsedNodeFn, field_names: &[&Ident], st
578526
let field_name = field_names[j];
579527
let (input_type, output_type) = &types[i.min(types.len() - 1)];
580528

581-
let node = matches!(parsed.fields[j], ParsedField::Node { .. });
529+
let node = matches!(parsed.fields[j].ty, ParsedFieldType::Node { .. });
582530

583531
let downcast_node = quote!(
584532
let #field_name: DowncastBothNode<#input_type, #output_type> = DowncastBothNode::new(args[#j].clone());

0 commit comments

Comments
 (0)