diff options
Diffstat (limited to 'bzipper_macros/src/impls/deserialise_enum.rs')
-rw-r--r-- | bzipper_macros/src/impls/deserialise_enum.rs | 20 |
1 files changed, 7 insertions, 13 deletions
diff --git a/bzipper_macros/src/impls/deserialise_enum.rs b/bzipper_macros/src/impls/deserialise_enum.rs index 7065c86..4c88a41 100644 --- a/bzipper_macros/src/impls/deserialise_enum.rs +++ b/bzipper_macros/src/impls/deserialise_enum.rs @@ -38,36 +38,30 @@ pub fn deserialise_enum(data: &DataEnum) -> TokenStream { let mut chain_commands = Punctuated::<TokenStream, Token![,]>::new(); 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>()? } + || quote! { Deserialise::deserialise(stream)? }, + |field_name| quote! { #field_name: Deserialise::deserialise(stream)? } ); chain_commands.push(command); } - let block = match variant.fields { + let value = 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 }); + match_arms.push(quote! { #discriminant => #value }); } - match_arms.push(quote! { value => return Err(::bzipper::Error::InvalidDiscriminant { value }) }); + match_arms.push(quote! { value => return Err(::bzipper::Error::InvalidDiscriminant(value)) }); quote! { - fn deserialise(data: &[u8]) -> ::bzipper::Result<Self> { - ::core::debug_assert_eq!(data.len(), <Self as ::bzipper::Serialise>::SERIALISED_SIZE); - - let mut stream = ::bzipper::Dstream::new(data); - - let value = match (stream.take::<u32>()?) { #match_arms }; + fn deserialise(stream: &::bzipper::Dstream) -> ::bzipper::Result<Self> { + let value = match (<u32 as ::bzipper::Deserialise>::deserialise(stream)?) { #match_arms }; Ok(value) } } |