From cd7e3b42248d9dd53a74d35f7ddd44e22f98bcf4 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 26 Nov 2022 10:38:27 -0800 Subject: [PATCH] Simplify named field handling code --- instant-xml-macros/src/de.rs | 17 +++++++---------- instant-xml-macros/src/ser.rs | 8 ++++---- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/instant-xml-macros/src/de.rs b/instant-xml-macros/src/de.rs index 3f1f115..e7ba7e9 100644 --- a/instant-xml-macros/src/de.rs +++ b/instant-xml-macros/src/de.rs @@ -190,15 +190,12 @@ fn deserialize_struct( let mut declare_values = TokenStream::new(); let mut return_val = TokenStream::new(); - match data.fields { - syn::Fields::Named(ref fields) => { - fields.named.iter().enumerate().for_each(|(index, field)| { + match &data.fields { + syn::Fields::Named(fields) => { + for (index, field) in fields.named.iter().enumerate() { let field_meta = match FieldMeta::from_field(field, &container_meta) { Ok(meta) => meta, - Err(err) => { - return_val.extend(err.into_compile_error()); - return; - } + Err(err) => return err.into_compile_error(), }; let tokens = match field_meta.attribute { @@ -206,7 +203,7 @@ fn deserialize_struct( false => &mut elements_tokens, }; - process_field( + named_field( field, index, &mut declare_values, @@ -215,7 +212,7 @@ fn deserialize_struct( field_meta, &container_meta, ); - }); + } } syn::Fields::Unnamed(_) => panic!("unamed"), syn::Fields::Unit => {} @@ -312,7 +309,7 @@ fn deserialize_struct( } #[allow(clippy::too_many_arguments)] -fn process_field( +fn named_field( field: &syn::Field, index: usize, declare_values: &mut TokenStream, diff --git a/instant-xml-macros/src/ser.rs b/instant-xml-macros/src/ser.rs index 858ef6e..7429ccf 100644 --- a/instant-xml-macros/src/ser.rs +++ b/instant-xml-macros/src/ser.rs @@ -180,10 +180,10 @@ fn serialize_struct( let mut body = TokenStream::new(); let mut attributes = TokenStream::new(); - match data.fields { - syn::Fields::Named(ref fields) => { + match &data.fields { + syn::Fields::Named(fields) => { for field in &fields.named { - if let Err(err) = process_named_field(field, &mut body, &mut attributes, &meta) { + if let Err(err) = named_field(field, &mut body, &mut attributes, &meta) { return err.to_compile_error(); } } @@ -252,7 +252,7 @@ fn serialize_struct( ) } -fn process_named_field( +fn named_field( field: &syn::Field, body: &mut TokenStream, attributes: &mut TokenStream,