Skip to content

Commit

Permalink
feat(Export): report compile error for all non-fieldless enum variants
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogay committed Oct 8, 2023
1 parent 7d7bdd7 commit a12fd4d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
38 changes: 25 additions & 13 deletions gdnative-derive/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub(crate) fn derive_export(input: &DeriveInput) -> syn::Result<TokenStream2> {
_ => {
return Err(syn::Error::new(
input.ident.span(),
"#[derive(Export)] can only use on enum",
"#[derive(Export)] only supports fieldless enums",
))
}
};
Expand All @@ -17,23 +17,35 @@ pub(crate) fn derive_export(input: &DeriveInput) -> syn::Result<TokenStream2> {
}

fn impl_export(enum_ty: &syn::Ident, data: &syn::DataEnum) -> syn::Result<TokenStream2> {
let mappings = {
let mut m = Vec::with_capacity(data.variants.len());

for variant in &data.variants {
if !matches!(variant.fields, Fields::Unit) {
return Err(syn::Error::new(
let err = data
.variants
.iter()
.filter_map(|variant| {
(!matches!(variant.fields, Fields::Unit)).then(|| {
syn::Error::new(
variant.ident.span(),
"#[derive(Export)] only supports fieldless enums",
));
}
)
})
})
.reduce(|mut acc, err| {
acc.combine(err);
acc
});
if let Some(err) = err {
return Err(err);
}

let mappings = data
.variants
.iter()
.map(|variant| {
let key = &variant.ident;
let val = quote! { #enum_ty::#key as i64 };
m.push(quote! { (stringify!(#key).to_string(), #val) });
}
quote! { (stringify!(#key).to_string(), #val) }
})
.collect::<Vec<_>>();

m
};
let impl_block = quote! {
impl ::gdnative::export::Export for #enum_ty {
type Hint = ::gdnative::export::hint::IntHint<i64>;
Expand Down
1 change: 1 addition & 0 deletions gdnative/tests/ui/export_fail_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use gdnative::prelude::*;
#[derive(Export, ToVariant)]
pub enum Foo {
Bar(String),
Baz { a: i32, b: u32 },
}

fn main() {}
6 changes: 6 additions & 0 deletions gdnative/tests/ui/export_fail_01.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ error: #[derive(Export)] only supports fieldless enums
|
5 | Bar(String),
| ^^^

error: #[derive(Export)] only supports fieldless enums
--> tests/ui/export_fail_01.rs:6:5
|
6 | Baz { a: i32, b: u32 },
| ^^^

0 comments on commit a12fd4d

Please sign in to comment.