Skip to content

Commit b9da077

Browse files
committed
collecting errors from different fileds
1 parent 6231d2a commit b9da077

File tree

1 file changed

+51
-27
lines changed

1 file changed

+51
-27
lines changed

pyo3-macros-backend/src/pyclass.rs

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -230,34 +230,58 @@ pub fn build_py_class(
230230
For an explanation, see https://pyo3.rs/latest/class.html#no-generic-parameters"
231231
);
232232

233-
let mut field_options: Vec<(&syn::Field, FieldPyO3Options)> = match &mut class.fields {
234-
syn::Fields::Named(fields) => fields
235-
.named
236-
.iter_mut()
237-
.map(|field| {
238-
FieldPyO3Options::take_pyo3_options(&mut field.attrs)
239-
.map(move |options| (&*field, options))
240-
})
241-
.collect::<Result<_>>()?,
242-
syn::Fields::Unnamed(fields) => fields
243-
.unnamed
244-
.iter_mut()
245-
.map(|field| {
246-
FieldPyO3Options::take_pyo3_options(&mut field.attrs)
247-
.map(move |options| (&*field, options))
248-
})
249-
.collect::<Result<_>>()?,
250-
syn::Fields::Unit => {
251-
if let Some(attr) = args.options.set_all {
252-
return Err(syn::Error::new_spanned(attr, UNIT_SET));
253-
};
254-
if let Some(attr) = args.options.get_all {
255-
return Err(syn::Error::new_spanned(attr, UNIT_GET));
256-
};
257-
// No fields for unit struct
258-
Vec::new()
233+
let mut field_options_res: Vec<Result<(&syn::Field, FieldPyO3Options)>> =
234+
match &mut class.fields {
235+
syn::Fields::Named(fields) => fields
236+
.named
237+
.iter_mut()
238+
.map(|field| {
239+
FieldPyO3Options::take_pyo3_options(&mut field.attrs)
240+
.map(move |options| (&*field, options))
241+
})
242+
.collect::<Vec<_>>(),
243+
syn::Fields::Unnamed(fields) => fields
244+
.unnamed
245+
.iter_mut()
246+
.map(|field| {
247+
FieldPyO3Options::take_pyo3_options(&mut field.attrs)
248+
.map(move |options| (&*field, options))
249+
})
250+
.collect::<Vec<_>>(),
251+
syn::Fields::Unit => {
252+
if let Some(attr) = args.options.set_all {
253+
return Err(syn::Error::new_spanned(attr, UNIT_SET));
254+
};
255+
if let Some(attr) = args.options.get_all {
256+
return Err(syn::Error::new_spanned(attr, UNIT_GET));
257+
};
258+
// No fields for unit struct
259+
Vec::new()
260+
}
261+
};
262+
263+
// handle error here
264+
265+
let mut allerr = Vec::new();
266+
267+
let mut field_options: Vec<(&syn::Field, FieldPyO3Options)> = field_options_res
268+
.drain(..)
269+
.filter_map(|result| match result {
270+
Err(err) => {
271+
allerr.push(err.clone());
272+
None
273+
}
274+
Ok(options) => Some(options),
275+
})
276+
.collect::<Vec<_>>();
277+
278+
if !allerr.is_empty() {
279+
let mut error = allerr[0].clone();
280+
for err in &allerr[1..] {
281+
error.combine(err.clone());
259282
}
260-
};
283+
return Err(error);
284+
}
261285

262286
if let Some(attr) = args.options.get_all {
263287
for (_, FieldPyO3Options { get, .. }) in &mut field_options {

0 commit comments

Comments
 (0)