From d616e92414072a396e448f7b8cd39607b69fbbbe Mon Sep 17 00:00:00 2001 From: Dominik Nakamura Date: Fri, 6 Oct 2023 11:48:35 +0900 Subject: [PATCH] fix: missing semicolon in tuple structs --- crates/stef-build/src/definition.rs | 6 ++- .../compiler__compile@struct-tuple.stef.snap | 46 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 crates/stef-build/tests/snapshots/compiler__compile@struct-tuple.stef.snap diff --git a/crates/stef-build/src/definition.rs b/crates/stef-build/src/definition.rs index d6bb5c1..552534b 100644 --- a/crates/stef-build/src/definition.rs +++ b/crates/stef-build/src/definition.rs @@ -226,7 +226,11 @@ fn compile_fields(fields: &Fields<'_>, for_struct: bool) -> TokenStream { quote! { #ty } }); - quote! { (#(#fields,)*) } + if for_struct { + quote! { (#(#fields,)*); } + } else { + quote! { (#(#fields,)*) } + } } Fields::Unit => { if for_struct { diff --git a/crates/stef-build/tests/snapshots/compiler__compile@struct-tuple.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@struct-tuple.stef.snap new file mode 100644 index 0000000..6e34243 --- /dev/null +++ b/crates/stef-build/tests/snapshots/compiler__compile@struct-tuple.stef.snap @@ -0,0 +1,46 @@ +--- +source: crates/stef-build/tests/compiler.rs +expression: "/// Basic struct.\nstruct Sample(u32 @1, bool @2)" +input_file: crates/stef-parser/tests/inputs/struct-tuple.stef +--- +/// Basic struct. +#[derive(Clone, Debug, PartialEq, PartialOrd)] +pub struct Sample(u32, bool); +#[automatically_derived] +impl ::stef::Encode for Sample { + fn encode(&self, w: &mut impl ::stef::BufMut) { + ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, self.0) }); + ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_bool(w, self.1) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); + } +} +#[automatically_derived] +impl ::stef::Decode for Sample { + fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { + let mut n0: Option = None; + let mut n1: Option = None; + loop { + match ::stef::buf::decode_id(r)? { + ::stef::buf::END_MARKER => break, + 1 => n0 = Some(::stef::buf::decode_u32(r)?), + 2 => n1 = Some(::stef::buf::decode_bool(r)?), + _ => continue, + } + } + Ok( + Self( + n0 + .ok_or(::stef::buf::Error::MissingField { + id: 1, + name: None, + })?, + n1 + .ok_or(::stef::buf::Error::MissingField { + id: 2, + name: None, + })?, + ), + ) + } +} +