-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
261 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use proc_macro2::{Span, TokenStream as TokenStream2}; | ||
use syn::spanned::Spanned; | ||
use syn::{DeriveInput, Fields}; | ||
|
||
fn err_only_supports_fieldless_enums(span: Span) -> syn::Error { | ||
syn::Error::new(span, "#[derive(Export)] only supports fieldless enums") | ||
} | ||
|
||
pub(crate) fn derive_export(input: &DeriveInput) -> syn::Result<TokenStream2> { | ||
let derived_enum = match &input.data { | ||
syn::Data::Enum(data) => data, | ||
syn::Data::Struct(data) => { | ||
return Err(err_only_supports_fieldless_enums(data.struct_token.span())); | ||
} | ||
syn::Data::Union(data) => { | ||
return Err(err_only_supports_fieldless_enums(data.union_token.span())); | ||
} | ||
}; | ||
|
||
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 err = data | ||
.variants | ||
.iter() | ||
.filter(|variant| !matches!(variant.fields, Fields::Unit)) | ||
.map(|variant| err_only_supports_fieldless_enums(variant.ident.span())) | ||
.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 }; | ||
quote! { (stringify!(#key).to_string(), #val) } | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use gdnative::prelude::*; | ||
|
||
#[derive(Export, ToVariant)] | ||
pub enum Foo { | ||
Bar(String), | ||
Baz { a: i32, b: u32 }, | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: #[derive(Export)] only supports fieldless enums | ||
--> tests/ui/export_fail_01.rs:5:5 | ||
| | ||
5 | Bar(String), | ||
| ^^^ | ||
|
||
error: #[derive(Export)] only supports fieldless enums | ||
--> tests/ui/export_fail_01.rs:6:5 | ||
| | ||
6 | Baz { a: i32, b: u32 }, | ||
| ^^^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use gdnative::prelude::*; | ||
|
||
#[derive(Export, ToVariant)] | ||
pub struct Foo { | ||
bar: i32, | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
error: #[derive(Export)] only supports fieldless enums | ||
--> tests/ui/export_fail_02.rs:4:5 | ||
| | ||
4 | pub struct Foo { | ||
| ^^^^^^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use gdnative::prelude::*; | ||
|
||
#[derive(Export, ToVariant)] | ||
pub union Foo { | ||
bar: i32, | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: #[derive(Export)] only supports fieldless enums | ||
--> tests/ui/export_fail_03.rs:4:5 | ||
| | ||
4 | pub union Foo { | ||
| ^^^^^ | ||
|
||
error: Variant conversion derive macro does not work on unions. | ||
--> tests/ui/export_fail_03.rs:4:1 | ||
| | ||
4 | pub union Foo { | ||
| ^^^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use gdnative::prelude::*; | ||
|
||
#[derive(Export, ToVariant, Clone, Copy)] | ||
#[variant(enum = "repr")] | ||
#[repr(i32)] | ||
pub enum Foo { | ||
Bar, | ||
Baz, | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use gdnative::prelude::*; | ||
|
||
#[derive(Debug, PartialEq, Clone, Copy, Export, ToVariant, FromVariant)] | ||
#[variant(enum = "repr")] | ||
#[repr(i32)] | ||
enum Dir { | ||
Up = 1, | ||
Down = -1, | ||
} | ||
|
||
pub(crate) fn run_tests() -> bool { | ||
let mut ok = true; | ||
|
||
ok &= test_from_variant(); | ||
ok &= test_to_variant(); | ||
|
||
ok | ||
} | ||
|
||
crate::godot_itest!(test_from_variant { | ||
assert_eq!(Dir::from_variant(&1_i32.to_variant()), Ok(Dir::Up)); | ||
assert_eq!(Dir::from_variant(&(-1_i32).to_variant()), Ok(Dir::Down)); | ||
// 42 isn't mapped to any variant of `Dir` | ||
assert!(Dir::from_variant(&42_i32.to_variant()).is_err()); | ||
}); | ||
|
||
crate::godot_itest!(test_to_variant { | ||
assert_eq!(Dir::Up.to_variant(), 1_i32.to_variant()); | ||
assert_eq!(Dir::Down.to_variant(), (-1_i32).to_variant()); | ||
}); |