Skip to content

Commit cb9087a

Browse files
committed
feat: rename ExportEnum to Export
1 parent aeac5af commit cb9087a

File tree

5 files changed

+63
-168
lines changed

5 files changed

+63
-168
lines changed

gdnative-core/src/export/property/hint.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,22 @@ impl EnumHint {
140140
let mut s = String::new();
141141

142142
let mut iter = self.values.iter();
143+
let write_item = |s: &mut String, item: &(String, Option<i64>)| match item {
144+
(key, Some(val)) => {
145+
write!(s, "{key}:{val}")
146+
}
147+
(key, None) => {
148+
write!(s, "{key}")
149+
}
150+
};
143151

144152
if let Some(first) = iter.next() {
145-
write!(s, "{first}").unwrap();
153+
write_item(&mut s, first).unwrap();
146154
}
147155

148156
for rest in iter {
149-
write!(s, ",{rest}").unwrap();
157+
write!(s, ",").unwrap();
158+
write_item(&mut s, rest).unwrap();
150159
}
151160

152161
s.into()

gdnative-derive/src/export.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use proc_macro2::TokenStream as TokenStream2;
2+
use syn::DeriveInput;
3+
4+
pub(crate) fn derive_export(input: &DeriveInput) -> syn::Result<TokenStream2> {
5+
let derived_enum = match &input.data {
6+
syn::Data::Enum(data) => data,
7+
_ => {
8+
return Err(syn::Error::new(
9+
input.ident.span(),
10+
"#[derive(Export)] can only use on enum",
11+
))
12+
}
13+
};
14+
15+
let export_impl = impl_export(&input.ident, derived_enum)?;
16+
Ok(export_impl)
17+
}
18+
19+
fn impl_export(enum_ty: &syn::Ident, data: &syn::DataEnum) -> syn::Result<TokenStream2> {
20+
let mappings = data.variants.iter().map(|variant| {
21+
let key = &variant.ident;
22+
let val = quote! { #enum_ty::#key as i64 };
23+
quote! { (stringify!(#key).to_string(), #val) }
24+
});
25+
let impl_block = quote! {
26+
impl ::gdnative::export::Export for #enum_ty {
27+
type Hint = ::gdnative::export::hint::IntHint<i64>;
28+
#[inline]
29+
fn export_info(hint: Option<Self::Hint>) -> ::gdnative::export::ExportInfo {
30+
if let Some(hint) = hint {
31+
return hint.export_info();
32+
} else {
33+
let mappings = vec![ #(#mappings),* ];
34+
let enum_hint = ::gdnative::export::hint::EnumHint::with_numbers(mappings);
35+
return ::gdnative::export::hint::IntHint::<i64>::Enum(enum_hint).export_info();
36+
}
37+
}
38+
}
39+
};
40+
41+
Ok(impl_block)
42+
}

gdnative-derive/src/export_enum.rs

Lines changed: 0 additions & 152 deletions
This file was deleted.

gdnative-derive/src/lib.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ use proc_macro2::TokenStream as TokenStream2;
1010
use quote::ToTokens;
1111
use syn::{parse::Parser, AttributeArgs, DeriveInput, ItemFn, ItemImpl, ItemType};
1212

13-
<<<<<<< HEAD
13+
mod export;
1414
mod init;
15-
=======
16-
mod export_enum;
17-
mod extend_bounds;
18-
>>>>>>> feat(derive): add `ExportEnum`
1915
mod methods;
2016
mod native_script;
2117
mod profiled;
@@ -678,7 +674,7 @@ pub fn godot_wrap_method(input: TokenStream) -> TokenStream {
678674
/// ```
679675
/// use gdnative::prelude::*;
680676
///
681-
/// #[derive(Debug, PartialEq, Clone, Copy, ExportEnum)]
677+
/// #[derive(Debug, PartialEq, Clone, Copy, Export)]
682678
/// enum Dir {
683679
/// Up = 1,
684680
/// Down = -1,
@@ -692,32 +688,32 @@ pub fn godot_wrap_method(input: TokenStream) -> TokenStream {
692688
/// }
693689
/// ```
694690
///
695-
/// You can't derive `ExportEnum` on `enum` that has non-unit variant.
691+
/// You can't derive `Export` on `enum` that has non-unit variant.
696692
///
697693
/// ```compile_fail
698694
/// use gdnative::prelude::*;
699695
///
700-
/// #[derive(Debug, PartialEq, Clone, Copy, ExportEnum)]
696+
/// #[derive(Debug, PartialEq, Clone, Copy, Export)]
701697
/// enum Action {
702698
/// Move((f32, f32, f32)),
703699
/// Attack(u64),
704700
/// }
705701
/// ```
706702
///
707-
/// You can't derive `ExportEnum` on `struct` or `union`.
703+
/// You can't derive `Export` on `struct` or `union`.
708704
///
709705
/// ```compile_fail
710706
/// use gdnative::prelude::*;
711707
///
712-
/// #[derive(ExportEnum)]
708+
/// #[derive(Export)]
713709
/// struct Foo {
714710
/// f1: i32
715711
/// }
716712
/// ```
717-
#[proc_macro_derive(ExportEnum)]
718-
pub fn derive_export_enum(input: TokenStream) -> TokenStream {
713+
#[proc_macro_derive(Export)]
714+
pub fn derive_export(input: TokenStream) -> TokenStream {
719715
let derive_input = syn::parse_macro_input!(input as syn::DeriveInput);
720-
match export_enum::derive_export_enum(&derive_input) {
716+
match export::derive_export(&derive_input) {
721717
Ok(stream) => stream.into(),
722718
Err(err) => err.to_compile_error().into(),
723719
}

test/src/test_export_enum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use gdnative::prelude::*;
22

3-
#[derive(Debug, PartialEq, Clone, Copy, ExportEnum)]
3+
#[derive(Debug, PartialEq, Clone, Copy, Export, ToVariant, FromVariant)]
44
enum Dir {
55
Up = 1,
66
Down = -1,

0 commit comments

Comments
 (0)