aboutsummaryrefslogtreecommitdiff
path: root/azalea-buf/azalea-buf-macros/src
diff options
context:
space:
mode:
Diffstat (limited to 'azalea-buf/azalea-buf-macros/src')
-rw-r--r--azalea-buf/azalea-buf-macros/src/read.rs38
-rw-r--r--azalea-buf/azalea-buf-macros/src/write.rs36
2 files changed, 47 insertions, 27 deletions
diff --git a/azalea-buf/azalea-buf-macros/src/read.rs b/azalea-buf/azalea-buf-macros/src/read.rs
index c3e9595c..c5c484e7 100644
--- a/azalea-buf/azalea-buf-macros/src/read.rs
+++ b/azalea-buf/azalea-buf-macros/src/read.rs
@@ -38,24 +38,34 @@ fn read_named_fields(
pub fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenStream {
match data {
- syn::Data::Struct(syn::DataStruct { fields, .. }) => {
- let syn::Fields::Named(FieldsNamed { named, .. }) = fields else {
- panic!("#[derive(McBuf)] can only be used on structs with named fields")
- };
-
- let (read_fields, read_field_names) = read_named_fields(named);
+ syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
+ syn::Fields::Named(FieldsNamed { named, .. }) => {
+ let (read_fields, read_field_names) = read_named_fields(named);
- quote! {
- impl azalea_buf::McBufReadable for #ident {
- fn read_from(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
- #(#read_fields)*
- Ok(#ident {
- #(#read_field_names: #read_field_names),*
- })
+ quote! {
+ impl azalea_buf::McBufReadable for #ident {
+ fn read_from(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
+ #(#read_fields)*
+ Ok(Self {
+ #(#read_field_names: #read_field_names),*
+ })
+ }
+ }
}
}
+ syn::Fields::Unit => {
+ quote! {
+ impl azalea_buf::McBufReadable for #ident {
+ fn read_from(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
+ Ok(Self)
+ }
+ }
+ }
}
- }
+ _ => {
+ panic!("#[derive(McBuf)] can only be used on structs with named fields")
+ }
+ },
syn::Data::Enum(syn::DataEnum { variants, .. }) => {
let mut match_contents = quote!();
let mut variant_discrim: u32 = 0;
diff --git a/azalea-buf/azalea-buf-macros/src/write.rs b/azalea-buf/azalea-buf-macros/src/write.rs
index 35c2d49a..4d31f39e 100644
--- a/azalea-buf/azalea-buf-macros/src/write.rs
+++ b/azalea-buf/azalea-buf-macros/src/write.rs
@@ -39,23 +39,33 @@ fn write_named_fields(
pub fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenStream {
match data {
- syn::Data::Struct(syn::DataStruct { fields, .. }) => {
- let syn::Fields::Named(FieldsNamed { named, .. }) = fields else {
- panic!("#[derive(McBuf)] can only be used on structs with named fields")
- };
-
- let write_fields =
- write_named_fields(named, Some(&Ident::new("self", Span::call_site())));
+ syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields {
+ syn::Fields::Named(FieldsNamed { named, .. }) => {
+ let write_fields =
+ write_named_fields(named, Some(&Ident::new("self", Span::call_site())));
- quote! {
- impl azalea_buf::McBufWritable for #ident {
- fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
- #write_fields
- Ok(())
+ quote! {
+ impl azalea_buf::McBufWritable for #ident {
+ fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
+ #write_fields
+ Ok(())
+ }
}
}
}
- }
+ syn::Fields::Unit => {
+ quote! {
+ impl azalea_buf::McBufWritable for #ident {
+ fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
+ Ok(())
+ }
+ }
+ }
+ }
+ _ => {
+ panic!("#[derive(McBuf)] can only be used on structs with named fields")
+ }
+ },
syn::Data::Enum(syn::DataEnum { variants, .. }) => {
// remember whether it's a data variant so we can do an optimization later
let mut is_data_enum = false;