automation_rs/automation_macro/src/lua_device.rs
Dreaded_X 2b62aca78a
All checks were successful
Build and deploy automation_rs / Build automation_rs (push) Successful in 4m53s
Build and deploy automation_rs / Build Docker image (push) Successful in 59s
Build and deploy automation_rs / Deploy Docker container (push) Has been skipped
LuaDevice macro now uses LuaDeviceCreate trait to create devices from configs
2024-04-29 02:53:21 +02:00

29 lines
1.0 KiB
Rust

use proc_macro2::TokenStream;
use quote::quote;
use syn::DeriveInput;
pub fn impl_lua_device_macro(ast: &DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
impl #name {
pub fn register_with_lua(lua: &mlua::Lua) -> mlua::Result<()> {
lua.globals().set(stringify!(#name), lua.create_proxy::<#name>()?)
}
}
impl mlua::UserData for #name {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_async_function("new", |lua, config: mlua::Value| async {
let config = mlua::FromLua::from_lua(config, lua)?;
// TODO: Using crate:: could cause issues
let device: #name = crate::devices::LuaDeviceCreate::create(config).await.map_err(mlua::ExternalError::into_lua_err)?;
Ok(crate::device_manager::WrappedDevice::new(Box::new(device)))
});
}
}
};
gen
}