diff options
Diffstat (limited to 'bzipper_macros/src')
-rw-r--r-- | bzipper_macros/src/impls/deserialise_enum.rs | 34 | ||||
-rw-r--r-- | bzipper_macros/src/impls/serialise_enum.rs | 46 |
2 files changed, 36 insertions, 44 deletions
diff --git a/bzipper_macros/src/impls/deserialise_enum.rs b/bzipper_macros/src/impls/deserialise_enum.rs index 7bf0220..7065c86 100644 --- a/bzipper_macros/src/impls/deserialise_enum.rs +++ b/bzipper_macros/src/impls/deserialise_enum.rs @@ -35,29 +35,25 @@ pub fn deserialise_enum(data: &DataEnum) -> TokenStream { let discriminant = Discriminant::unwrap_from(index); - let block = if matches!(variant.fields, Fields::Unit) { - quote! { Self } - } else { - let mut chain_commands = Punctuated::<TokenStream, Token![,]>::new(); + let mut chain_commands = Punctuated::<TokenStream, Token![,]>::new(); - for field in &variant.fields { - let field_ty = &field.ty; + for field in &variant.fields { + let field_ty = &field.ty; - let command = field.ident - .as_ref() - .map_or_else( - || quote! { stream.take::<#field_ty>()? }, - |field_name| quote! { #field_name: stream.take::<#field_ty>()? } - ); + let command = field.ident + .as_ref() + .map_or_else( + || quote! { stream.take::<#field_ty>()? }, + |field_name| quote! { #field_name: stream.take::<#field_ty>()? } + ); - chain_commands.push(command); - } + chain_commands.push(command); + } - match variant.fields { - Fields::Named( ..) => quote! { Self::#variant_name { #chain_commands } }, - Fields::Unnamed(..) => quote! { Self::#variant_name(#chain_commands) }, - Fields::Unit => unreachable!(), - } + let block = match variant.fields { + Fields::Named( ..) => quote! { Self::#variant_name { #chain_commands } }, + Fields::Unnamed(..) => quote! { Self::#variant_name(#chain_commands) }, + Fields::Unit => quote! { Self::#variant_name }, }; match_arms.push(quote! { #discriminant => #block }); diff --git a/bzipper_macros/src/impls/serialise_enum.rs b/bzipper_macros/src/impls/serialise_enum.rs index 8f0693a..a554351 100644 --- a/bzipper_macros/src/impls/serialise_enum.rs +++ b/bzipper_macros/src/impls/serialise_enum.rs @@ -35,7 +35,7 @@ pub fn serialise_enum(data: &DataEnum) -> TokenStream { for (index, variant) in data.variants.iter().enumerate() { let mut serialised_size = Punctuated::<TokenStream, Token![+]>::new(); - let name = &variant.ident; + let variant_name = &variant.ident; let discriminant = u32::try_from(index) .expect("enumeration discriminants must be representable in `u32`"); @@ -43,38 +43,34 @@ pub fn serialise_enum(data: &DataEnum) -> TokenStream { // Discriminant size: serialised_size.push(quote! { <u32 as ::bzipper::Serialise>::SERIALISED_SIZE }); - let arm = if matches!(variant.fields, Fields::Unit) { - quote! { Self::#name => stream.append(&#discriminant)? } - } else { - let mut captures = Punctuated::<Capture, Token![,]>::new(); + let mut captures = Punctuated::<Capture, Token![,]>::new(); - let mut chain_commands = Punctuated::<TokenStream, Token![;]>::new(); - chain_commands.push(quote! { stream.append(&#discriminant)? }); + let mut chain_commands = Punctuated::<TokenStream, Token![;]>::new(); + chain_commands.push(quote! { stream.append(&#discriminant)? }); - for (index, field) in variant.fields.iter().enumerate() { - let field_ty = &field.ty; + for (index, field) in variant.fields.iter().enumerate() { + let field_ty = &field.ty; - let field_name = field.ident - .as_ref() - .map_or_else(|| Ident::new(&format!("v{index}"), Span::call_site()), Clone::clone); + let field_name = field.ident + .as_ref() + .map_or_else(|| Ident::new(&format!("v{index}"), Span::call_site()), Clone::clone); - serialised_size.push(quote! { <#field_ty as ::bzipper::Serialise>::SERIALISED_SIZE }); + serialised_size.push(quote! { <#field_ty as ::bzipper::Serialise>::SERIALISED_SIZE }); - captures.push(Capture { - ref_token: Token![ref](Span::call_site()), - ident: field_name.clone(), - }); + captures.push(Capture { + ref_token: Token![ref](Span::call_site()), + ident: field_name.clone(), + }); - chain_commands.push(quote! { stream.append(#field_name)? }); - } + chain_commands.push(quote! { stream.append(#field_name)? }); + } - chain_commands.push_punct(Token![;](Span::call_site())); + chain_commands.push_punct(Token![;](Span::call_site())); - match variant.fields { - Fields::Named( ..) => quote! { Self::#name { #captures } => { #chain_commands } }, - Fields::Unnamed(..) => quote! { Self::#name(#captures) => { #chain_commands } }, - Fields::Unit => unreachable!(), - } + let arm = match variant.fields { + Fields::Named( ..) => quote! { Self::#variant_name { #captures } => { #chain_commands } }, + Fields::Unnamed(..) => quote! { Self::#variant_name(#captures) => { #chain_commands } }, + Fields::Unit => quote! { Self::#variant_name => { #chain_commands } }, }; sizes.push(serialised_size); |