// Copyright 2024 Gabriel Bjørnager Jensen. // // This file is part of bzipper. // // bzipper is free software: you can redistribute // it and/or modify it under the terms of the GNU // Lesser General Public License as published by // the Free Software Foundation, either version 3 // of the License, or (at your option) any later // version. // // bzipper is distributed in the hope that it will // be useful, but WITHOUT ANY WARRANTY; without // even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Less- // er General Public License along with bzipper. If // not, see . use crate::Capture; use proc_macro2::{Span, TokenStream}; use quote::quote; use syn::{DataEnum, Fields, Ident, Token}; use syn::punctuated::Punctuated; #[must_use] pub fn serialise_enum(data: &DataEnum) -> TokenStream { let mut sizes = Vec::new(); let mut match_arms = Punctuated::::new(); for (index, variant) in data.variants.iter().enumerate() { let mut serialised_size = Punctuated::::new(); let name = &variant.ident; let discriminant = u32::try_from(index) .expect("enumeration discriminants must be representable in `u32`"); // Discriminant size: serialised_size.push(quote! { ::SERIALISED_SIZE }); let arm = if matches!(variant.fields, Fields::Unit) { quote! { Self::#name => stream.append(&#discriminant)? } } else { let mut captures = Punctuated::::new(); let mut chain_commands = Punctuated::::new(); chain_commands.push(quote! { stream.append(&#discriminant)? }); 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); 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(), }); chain_commands.push(quote! { stream.append(#field_name)? }); } 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!(), } }; sizes.push(serialised_size); match_arms.push(arm); } let mut size_tests = Punctuated::::new(); for size in &sizes { let mut test = Punctuated::::new(); for other_size in &sizes { test.push(quote! { #size >= #other_size }) } size_tests.push(quote! { if #test { #size } }); } size_tests.push(quote! { { core::unreachable!(); } }); quote! { const SERIALISED_SIZE: usize = const { #size_tests }; fn serialise(&self, buf: &mut [u8]) -> ::bzipper::Result<()> { ::core::debug_assert_eq!(buf.len(), Self::SERIALISED_SIZE); let mut stream = ::bzipper::Sstream::new(buf); match (*self) { #match_arms } Ok(()) } } }