Skip to content

Commit 7a9e667

Browse files
committed
fix(ExportEnum): handle non-enum input
1 parent 107ca28 commit 7a9e667

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

gdnative-derive/src/export_enum.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ use syn::DeriveInput;
44
pub(crate) fn derive_export_enum(input: &DeriveInput) -> syn::Result<TokenStream2> {
55
let derived_enum = match &input.data {
66
syn::Data::Enum(data) => data,
7-
_ => todo!("return error"),
7+
_ => {
8+
return Err(syn::Error::new(
9+
input.ident.span(),
10+
"#[derive(ExportEnum)] can only use on enum",
11+
))
12+
}
813
};
914

1015
let to_variant_impl = impl_to_variant(&input.ident, derived_enum)?;
@@ -59,19 +64,26 @@ fn impl_from_variant(enum_ty: &syn::Ident, data: &syn::DataEnum) -> syn::Result<
5964
Ok(impl_block)
6065
}
6166

62-
fn impl_export(enum_ty: &syn::Ident, _data: &syn::DataEnum) -> syn::Result<TokenStream2> {
67+
fn impl_export(enum_ty: &syn::Ident, data: &syn::DataEnum) -> syn::Result<TokenStream2> {
68+
let mappings = data.variants.iter().map(|variant| {
69+
let ident = &variant.ident;
70+
let key = stringify!(ident);
71+
let val = quote! { #enum_ty::#ident as i64 };
72+
quote! { (#key.to_string(), #val) }
73+
});
6374
let impl_block = quote! {
6475
impl ::gdnative::export::Export for #enum_ty {
6576
type Hint = ::gdnative::export::hint::IntHint<i64>;
6677
#[inline]
6778
fn export_info(hint: Option<Self::Hint>) -> ::gdnative::export::ExportInfo {
6879
if let Some(hint) = hint {
69-
if matches!(hint, ::gdnative::export::hint::IntHint::<i64>::Enum(_)) {
70-
return hint.export_info();
71-
}
80+
return hint.export_info();
81+
} else {
82+
let mappings = vec![ #(#mappings),* ];
83+
let enum_hint = ::gdnative::export::hint::EnumHint::with_numbers(mappings);
84+
return ::gdnative::export::hint::IntHint::<i64>::Enum(enum_hint).export_info();
7285
}
7386

74-
::gdnative::export::ExportInfo::new(::gdnative::core_types::VariantType::I64)
7587
}
7688
}
7789
};

0 commit comments

Comments
 (0)