feat: Added derive macro to implement IntoLua on structs that implement Serialize
This can be very useful if you want to convert a data struct to a lua table without having to write the boilerplane (however small it may be). It also adds the macro on several state structs so they can be converted to lua in the upcoming ActionCallback refactor.
This commit is contained in:
@@ -3,6 +3,7 @@ mod impl_device;
|
||||
mod lua_device_config;
|
||||
|
||||
use lua_device_config::impl_lua_device_config_macro;
|
||||
use quote::quote;
|
||||
use syn::{DeriveInput, parse_macro_input};
|
||||
|
||||
use crate::impl_device::impl_device_macro;
|
||||
@@ -20,3 +21,19 @@ pub fn impl_device(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
|
||||
impl_device_macro(&ast).into()
|
||||
}
|
||||
|
||||
#[proc_macro_derive(LuaSerialize, attributes(traits))]
|
||||
pub fn lua_serialize(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let ast = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
let name = &ast.ident;
|
||||
|
||||
quote! {
|
||||
impl ::mlua::IntoLua for #name {
|
||||
fn into_lua(self, lua: &::mlua::Lua) -> ::mlua::Result<::mlua::Value> {
|
||||
::mlua::LuaSerdeExt::to_value(lua, &self)
|
||||
}
|
||||
}
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user