Skip to content

Commit

Permalink
feat: rename ExportEnum to Export
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogay committed Oct 8, 2023
1 parent aeac5af commit cb9087a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 168 deletions.
13 changes: 11 additions & 2 deletions gdnative-core/src/export/property/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,22 @@ impl EnumHint {
let mut s = String::new();

let mut iter = self.values.iter();
let write_item = |s: &mut String, item: &(String, Option<i64>)| match item {
(key, Some(val)) => {
write!(s, "{key}:{val}")
}
(key, None) => {
write!(s, "{key}")
}
};

if let Some(first) = iter.next() {
write!(s, "{first}").unwrap();
write_item(&mut s, first).unwrap();
}

for rest in iter {
write!(s, ",{rest}").unwrap();
write!(s, ",").unwrap();
write_item(&mut s, rest).unwrap();
}

s.into()
Expand Down
42 changes: 42 additions & 0 deletions gdnative-derive/src/export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use proc_macro2::TokenStream as TokenStream2;
use syn::DeriveInput;

pub(crate) fn derive_export(input: &DeriveInput) -> syn::Result<TokenStream2> {
let derived_enum = match &input.data {
syn::Data::Enum(data) => data,
_ => {
return Err(syn::Error::new(
input.ident.span(),
"#[derive(Export)] can only use on enum",
))
}
};

let export_impl = impl_export(&input.ident, derived_enum)?;
Ok(export_impl)
}

fn impl_export(enum_ty: &syn::Ident, data: &syn::DataEnum) -> syn::Result<TokenStream2> {
let mappings = data.variants.iter().map(|variant| {
let key = &variant.ident;
let val = quote! { #enum_ty::#key as i64 };
quote! { (stringify!(#key).to_string(), #val) }
});
let impl_block = quote! {
impl ::gdnative::export::Export for #enum_ty {
type Hint = ::gdnative::export::hint::IntHint<i64>;
#[inline]
fn export_info(hint: Option<Self::Hint>) -> ::gdnative::export::ExportInfo {
if let Some(hint) = hint {
return hint.export_info();
} else {
let mappings = vec![ #(#mappings),* ];
let enum_hint = ::gdnative::export::hint::EnumHint::with_numbers(mappings);
return ::gdnative::export::hint::IntHint::<i64>::Enum(enum_hint).export_info();
}
}
}
};

Ok(impl_block)
}
152 changes: 0 additions & 152 deletions gdnative-derive/src/export_enum.rs

This file was deleted.

22 changes: 9 additions & 13 deletions gdnative-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ use proc_macro2::TokenStream as TokenStream2;
use quote::ToTokens;
use syn::{parse::Parser, AttributeArgs, DeriveInput, ItemFn, ItemImpl, ItemType};

<<<<<<< HEAD
mod export;
mod init;
=======
mod export_enum;
mod extend_bounds;
>>>>>>> feat(derive): add `ExportEnum`
mod methods;
mod native_script;
mod profiled;
Expand Down Expand Up @@ -678,7 +674,7 @@ pub fn godot_wrap_method(input: TokenStream) -> TokenStream {
/// ```
/// use gdnative::prelude::*;
///
/// #[derive(Debug, PartialEq, Clone, Copy, ExportEnum)]
/// #[derive(Debug, PartialEq, Clone, Copy, Export)]
/// enum Dir {
/// Up = 1,
/// Down = -1,
Expand All @@ -692,32 +688,32 @@ pub fn godot_wrap_method(input: TokenStream) -> TokenStream {
/// }
/// ```
///
/// You can't derive `ExportEnum` on `enum` that has non-unit variant.
/// You can't derive `Export` on `enum` that has non-unit variant.
///
/// ```compile_fail
/// use gdnative::prelude::*;
///
/// #[derive(Debug, PartialEq, Clone, Copy, ExportEnum)]
/// #[derive(Debug, PartialEq, Clone, Copy, Export)]
/// enum Action {
/// Move((f32, f32, f32)),
/// Attack(u64),
/// }
/// ```
///
/// You can't derive `ExportEnum` on `struct` or `union`.
/// You can't derive `Export` on `struct` or `union`.
///
/// ```compile_fail
/// use gdnative::prelude::*;
///
/// #[derive(ExportEnum)]
/// #[derive(Export)]
/// struct Foo {
/// f1: i32
/// }
/// ```
#[proc_macro_derive(ExportEnum)]
pub fn derive_export_enum(input: TokenStream) -> TokenStream {
#[proc_macro_derive(Export)]
pub fn derive_export(input: TokenStream) -> TokenStream {
let derive_input = syn::parse_macro_input!(input as syn::DeriveInput);
match export_enum::derive_export_enum(&derive_input) {
match export::derive_export(&derive_input) {
Ok(stream) => stream.into(),
Err(err) => err.to_compile_error().into(),
}
Expand Down
2 changes: 1 addition & 1 deletion test/src/test_export_enum.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use gdnative::prelude::*;

#[derive(Debug, PartialEq, Clone, Copy, ExportEnum)]
#[derive(Debug, PartialEq, Clone, Copy, Export, ToVariant, FromVariant)]
enum Dir {
Up = 1,
Down = -1,
Expand Down

0 comments on commit cb9087a

Please sign in to comment.