Skip to content

feat: Add TypedThrough and IntoScript derive macros #294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions crates/bevy_mod_scripting_derive/src/derive/into_script.rs
Original file line number Diff line number Diff line change
@@ -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,
)
}
}
}
}
4 changes: 4 additions & 0 deletions crates/bevy_mod_scripting_derive/src/derive/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod into_script;
mod typed_through;

pub use self::{into_script::into_script, typed_through::typed_through};
23 changes: 23 additions & 0 deletions crates/bevy_mod_scripting_derive/src/derive/typed_through.rs
Original file line number Diff line number Diff line change
@@ -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())
}
}
}
}
14 changes: 14 additions & 0 deletions crates/bevy_mod_scripting_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading