@@ -4,7 +4,12 @@ use syn::DeriveInput;
4
4
pub ( crate ) fn derive_export_enum ( input : & DeriveInput ) -> syn:: Result < TokenStream2 > {
5
5
let derived_enum = match & input. data {
6
6
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
+ }
8
13
} ;
9
14
10
15
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<
59
64
Ok ( impl_block)
60
65
}
61
66
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
+ } ) ;
63
74
let impl_block = quote ! {
64
75
impl :: gdnative:: export:: Export for #enum_ty {
65
76
type Hint = :: gdnative:: export:: hint:: IntHint <i64 >;
66
77
#[ inline]
67
78
fn export_info( hint: Option <Self :: Hint >) -> :: gdnative:: export:: ExportInfo {
68
79
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( ) ;
72
85
}
73
86
74
- :: gdnative:: export:: ExportInfo :: new( :: gdnative:: core_types:: VariantType :: I64 )
75
87
}
76
88
}
77
89
} ;
0 commit comments