diff --git a/crates/bevy_mod_scripting_derive/src/derive/into_script.rs b/crates/bevy_mod_scripting_derive/src/derive/into_script.rs new file mode 100644 index 0000000000..d67d3ab288 --- /dev/null +++ b/crates/bevy_mod_scripting_derive/src/derive/into_script.rs @@ -0,0 +1,25 @@ +use proc_macro2::TokenStream; +use quote::quote; +use syn::DeriveInput; + +pub fn into_script(input: TokenStream) -> TokenStream { + let (ident, generics) = match syn::parse2(input) { + Ok(DeriveInput { + ident, generics, .. + }) => (ident, generics), + Err(err) => return err.to_compile_error(), + }; + + let (impl_generics, type_generics, where_clause) = generics.split_for_impl(); + + quote! { + impl #impl_generics ::bevy_mod_scripting::bindings::function::into::IntoScript for #ident #type_generics #where_clause { + fn into_script(self, world: ::bevy_mod_scripting::core::bindings::WorldGuard) -> Result<::bevy_mod_scripting::core::bindings::script_value::ScriptValue, ::bevy_mod_scripting::core::error::InteropError> { + ::bevy_mod_scripting::core::bindings::function::into::IntoScript::into_script( + ::bevy_mod_scripting::core::bindings::function::from::Val(self), + world, + ) + } + } + } +} diff --git a/crates/bevy_mod_scripting_derive/src/derive/mod.rs b/crates/bevy_mod_scripting_derive/src/derive/mod.rs new file mode 100644 index 0000000000..3ae391a536 --- /dev/null +++ b/crates/bevy_mod_scripting_derive/src/derive/mod.rs @@ -0,0 +1,4 @@ +mod into_script; +mod typed_through; + +pub use self::{into_script::into_script, typed_through::typed_through}; diff --git a/crates/bevy_mod_scripting_derive/src/derive/typed_through.rs b/crates/bevy_mod_scripting_derive/src/derive/typed_through.rs new file mode 100644 index 0000000000..d8213aeef4 --- /dev/null +++ b/crates/bevy_mod_scripting_derive/src/derive/typed_through.rs @@ -0,0 +1,23 @@ +use proc_macro2::TokenStream; +use quote::quote; +use syn::DeriveInput; + +pub fn typed_through(input: TokenStream) -> TokenStream { + let (ident, generics) = match syn::parse2(input) { + Ok(DeriveInput { + ident, generics, .. + }) => (ident, generics), + Err(err) => return err.to_compile_error(), + }; + + let (impl_generics, type_generics, where_clause) = generics.split_for_impl(); + + let turbofish = type_generics.as_turbofish(); + quote! { + impl #impl_generics ::bevy_mod_scripting::core::docgen::typed_through::TypedThrough for #ident #type_generics #where_clause { + fn through_type_info() -> ::bevy_mod_scripting::core::docgen::typed_through::ThroughTypeInfo { + ::bevy_mod_scripting::core::docgen::typed_through::ThroughTypeInfo::TypeInfo(#ident #turbofish ::type_info()) + } + } + } +} diff --git a/crates/bevy_mod_scripting_derive/src/lib.rs b/crates/bevy_mod_scripting_derive/src/lib.rs index 9a6d133389..7e1ea2e391 100644 --- a/crates/bevy_mod_scripting_derive/src/lib.rs +++ b/crates/bevy_mod_scripting_derive/src/lib.rs @@ -4,6 +4,20 @@ use proc_macro2::{Span, TokenStream}; use quote::{format_ident, quote_spanned, ToTokens}; use syn::{spanned::Spanned, ImplItemFn, ItemImpl}; +mod derive; + +#[proc_macro_derive(TypedThrough)] +/// Default implementation for the `TypedThrough` trait +pub fn typed_through(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + derive::typed_through(input.into()).into() +} + +#[proc_macro_derive(IntoScript)] +/// Default implementation for the `IntoScript` trait +pub fn into_script(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + derive::into_script(input.into()).into() +} + /// Derive macro for generating script bindings from an impl block. /// /// Does not support generics.