From 98ab265fed74614c7f5f768442ee2c1d5b7ac5b0 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Thu, 25 Jul 2024 00:49:10 +0200 Subject: [PATCH] Improved Lua macro situation --- automation_macro/src/lib.rs | 9 ------ automation_macro/src/lua_device.rs | 28 ----------------- src/devices/air_filter.rs | 4 +-- src/devices/audio_setup.rs | 4 +-- src/devices/contact_sensor.rs | 4 +-- src/devices/debug_bridge.rs | 4 +-- src/devices/hue_bridge.rs | 4 +-- src/devices/hue_group.rs | 4 +-- src/devices/ikea_outlet.rs | 4 +-- src/devices/kasa_outlet.rs | 4 +-- src/devices/light_sensor.rs | 4 +-- src/devices/mod.rs | 48 ++++++++++++++++++++++-------- src/devices/ntfy.rs | 4 +-- src/devices/presence.rs | 4 +-- src/devices/wake_on_lan.rs | 4 +-- src/devices/washer.rs | 4 +-- 16 files changed, 61 insertions(+), 76 deletions(-) delete mode 100644 automation_macro/src/lua_device.rs diff --git a/automation_macro/src/lib.rs b/automation_macro/src/lib.rs index 7cb1b9c..8ade07f 100644 --- a/automation_macro/src/lib.rs +++ b/automation_macro/src/lib.rs @@ -1,19 +1,10 @@ #![feature(let_chains)] #![feature(iter_intersperse)] -mod lua_device; mod lua_device_config; -use lua_device::impl_lua_device_macro; use lua_device_config::impl_lua_device_config_macro; use syn::{parse_macro_input, DeriveInput}; -#[proc_macro_derive(LuaDevice, attributes(config))] -pub fn lua_device_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream { - let ast = parse_macro_input!(input as DeriveInput); - - impl_lua_device_macro(&ast).into() -} - #[proc_macro_derive(LuaDeviceConfig, attributes(device_config))] pub fn lua_device_config_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let ast = parse_macro_input!(input as DeriveInput); diff --git a/automation_macro/src/lua_device.rs b/automation_macro/src/lua_device.rs deleted file mode 100644 index fcccc1b..0000000 --- a/automation_macro/src/lua_device.rs +++ /dev/null @@ -1,28 +0,0 @@ -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 -} diff --git a/src/devices/air_filter.rs b/src/devices/air_filter.rs index 6ebf02c..49747c2 100644 --- a/src/devices/air_filter.rs +++ b/src/devices/air_filter.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use automation_macro::{LuaDevice, LuaDeviceConfig}; +use automation_macro::LuaDeviceConfig; use google_home::device::Name; use google_home::errors::ErrorCode; use google_home::traits::{ @@ -27,7 +27,7 @@ pub struct AirFilterConfig { pub client: WrappedAsyncClient, } -#[derive(Debug, LuaDevice)] +#[derive(Debug)] pub struct AirFilter { config: AirFilterConfig, diff --git a/src/devices/audio_setup.rs b/src/devices/audio_setup.rs index ff74a67..5ce5de1 100644 --- a/src/devices/audio_setup.rs +++ b/src/devices/audio_setup.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use automation_macro::{LuaDevice, LuaDeviceConfig}; +use automation_macro::LuaDeviceConfig; use google_home::traits::OnOff; use tracing::{debug, error, trace, warn}; @@ -24,7 +24,7 @@ pub struct AudioSetupConfig { pub client: WrappedAsyncClient, } -#[derive(Debug, LuaDevice)] +#[derive(Debug)] pub struct AudioSetup { config: AudioSetupConfig, } diff --git a/src/devices/contact_sensor.rs b/src/devices/contact_sensor.rs index c3231b3..6cf28b4 100644 --- a/src/devices/contact_sensor.rs +++ b/src/devices/contact_sensor.rs @@ -1,7 +1,7 @@ use std::time::Duration; use async_trait::async_trait; -use automation_macro::{LuaDevice, LuaDeviceConfig}; +use automation_macro::LuaDeviceConfig; use google_home::traits::OnOff; use mlua::FromLua; use tokio::task::JoinHandle; @@ -62,7 +62,7 @@ pub struct ContactSensorConfig { pub client: WrappedAsyncClient, } -#[derive(Debug, LuaDevice)] +#[derive(Debug)] pub struct ContactSensor { config: ContactSensorConfig, diff --git a/src/devices/debug_bridge.rs b/src/devices/debug_bridge.rs index a4de62c..88b4677 100644 --- a/src/devices/debug_bridge.rs +++ b/src/devices/debug_bridge.rs @@ -1,7 +1,7 @@ use std::convert::Infallible; use async_trait::async_trait; -use automation_macro::{LuaDevice, LuaDeviceConfig}; +use automation_macro::LuaDeviceConfig; use tracing::{trace, warn}; use super::LuaDeviceCreate; @@ -20,7 +20,7 @@ pub struct DebugBridgeConfig { pub client: WrappedAsyncClient, } -#[derive(Debug, LuaDevice)] +#[derive(Debug)] pub struct DebugBridge { config: DebugBridgeConfig, } diff --git a/src/devices/hue_bridge.rs b/src/devices/hue_bridge.rs index 2fc0fe1..7e8fba8 100644 --- a/src/devices/hue_bridge.rs +++ b/src/devices/hue_bridge.rs @@ -2,7 +2,7 @@ use std::convert::Infallible; use std::net::SocketAddr; use async_trait::async_trait; -use automation_macro::{LuaDevice, LuaDeviceConfig}; +use automation_macro::LuaDeviceConfig; use serde::{Deserialize, Serialize}; use tracing::{error, trace, warn}; @@ -31,7 +31,7 @@ pub struct HueBridgeConfig { pub flags: FlagIDs, } -#[derive(Debug, LuaDevice)] +#[derive(Debug)] pub struct HueBridge { config: HueBridgeConfig, } diff --git a/src/devices/hue_group.rs b/src/devices/hue_group.rs index ca96952..ba23da6 100644 --- a/src/devices/hue_group.rs +++ b/src/devices/hue_group.rs @@ -3,7 +3,7 @@ use std::time::Duration; use anyhow::{anyhow, Context, Result}; use async_trait::async_trait; -use automation_macro::{LuaDevice, LuaDeviceConfig}; +use automation_macro::LuaDeviceConfig; use google_home::errors::ErrorCode; use google_home::traits::OnOff; use rumqttc::{Publish, SubscribeFilter}; @@ -31,7 +31,7 @@ pub struct HueGroupConfig { pub client: WrappedAsyncClient, } -#[derive(Debug, LuaDevice)] +#[derive(Debug)] pub struct HueGroup { config: HueGroupConfig, } diff --git a/src/devices/ikea_outlet.rs b/src/devices/ikea_outlet.rs index 15e8945..eb95b2b 100644 --- a/src/devices/ikea_outlet.rs +++ b/src/devices/ikea_outlet.rs @@ -2,7 +2,7 @@ use std::time::Duration; use anyhow::Result; use async_trait::async_trait; -use automation_macro::{LuaDevice, LuaDeviceConfig}; +use automation_macro::LuaDeviceConfig; use google_home::device; use google_home::errors::ErrorCode; use google_home::traits::{self, OnOff}; @@ -45,7 +45,7 @@ pub struct IkeaOutletConfig { pub client: WrappedAsyncClient, } -#[derive(Debug, LuaDevice)] +#[derive(Debug)] pub struct IkeaOutlet { config: IkeaOutletConfig, diff --git a/src/devices/kasa_outlet.rs b/src/devices/kasa_outlet.rs index cbd7cab..222af32 100644 --- a/src/devices/kasa_outlet.rs +++ b/src/devices/kasa_outlet.rs @@ -3,7 +3,7 @@ use std::net::SocketAddr; use std::str::Utf8Error; use async_trait::async_trait; -use automation_macro::{LuaDevice, LuaDeviceConfig}; +use automation_macro::LuaDeviceConfig; use bytes::{Buf, BufMut}; use google_home::errors::{self, DeviceError}; use google_home::traits; @@ -22,7 +22,7 @@ pub struct KasaOutletConfig { pub addr: SocketAddr, } -#[derive(Debug, LuaDevice)] +#[derive(Debug)] pub struct KasaOutlet { config: KasaOutletConfig, } diff --git a/src/devices/light_sensor.rs b/src/devices/light_sensor.rs index 041aa57..99bfe3d 100644 --- a/src/devices/light_sensor.rs +++ b/src/devices/light_sensor.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use automation_macro::{LuaDevice, LuaDeviceConfig}; +use automation_macro::LuaDeviceConfig; use rumqttc::Publish; use tracing::{debug, trace, warn}; @@ -25,7 +25,7 @@ pub struct LightSensorConfig { const DEFAULT: bool = false; -#[derive(Debug, LuaDevice)] +#[derive(Debug)] pub struct LightSensor { config: LightSensorConfig, diff --git a/src/devices/mod.rs b/src/devices/mod.rs index 927af26..81f2243 100644 --- a/src/devices/mod.rs +++ b/src/devices/mod.rs @@ -44,20 +44,42 @@ pub trait LuaDeviceCreate { Self: Sized; } +macro_rules! register_device { + ($lua:expr, $device:ty) => { + impl mlua::UserData for $device { + 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: $device = crate::devices::LuaDeviceCreate::create(config) + .await + .map_err(mlua::ExternalError::into_lua_err)?; + + Ok(crate::device_manager::WrappedDevice::new(Box::new(device))) + }); + } + } + + $lua.globals() + .set(stringify!($device), $lua.create_proxy::<$device>()?)?; + }; +} + pub fn register_with_lua(lua: &mlua::Lua) -> mlua::Result<()> { - AirFilter::register_with_lua(lua)?; - AudioSetup::register_with_lua(lua)?; - ContactSensor::register_with_lua(lua)?; - DebugBridge::register_with_lua(lua)?; - HueBridge::register_with_lua(lua)?; - HueGroup::register_with_lua(lua)?; - IkeaOutlet::register_with_lua(lua)?; - KasaOutlet::register_with_lua(lua)?; - LightSensor::register_with_lua(lua)?; - Ntfy::register_with_lua(lua)?; - Presence::register_with_lua(lua)?; - WakeOnLAN::register_with_lua(lua)?; - Washer::register_with_lua(lua)?; + register_device!(lua, AirFilter); + register_device!(lua, AudioSetup); + register_device!(lua, ContactSensor); + register_device!(lua, DebugBridge); + register_device!(lua, HueBridge); + register_device!(lua, HueGroup); + register_device!(lua, IkeaOutlet); + register_device!(lua, KasaOutlet); + register_device!(lua, LightSensor); + register_device!(lua, Ntfy); + register_device!(lua, Presence); + register_device!(lua, WakeOnLAN); + register_device!(lua, Washer); Ok(()) } diff --git a/src/devices/ntfy.rs b/src/devices/ntfy.rs index 6d5d5af..ef96168 100644 --- a/src/devices/ntfy.rs +++ b/src/devices/ntfy.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::convert::Infallible; use async_trait::async_trait; -use automation_macro::{LuaDevice, LuaDeviceConfig}; +use automation_macro::LuaDeviceConfig; use serde::Serialize; use serde_repr::*; use tracing::{error, trace, warn}; @@ -120,7 +120,7 @@ pub struct NtfyConfig { pub tx: event::Sender, } -#[derive(Debug, LuaDevice)] +#[derive(Debug)] pub struct Ntfy { config: NtfyConfig, } diff --git a/src/devices/presence.rs b/src/devices/presence.rs index 18880b3..672b909 100644 --- a/src/devices/presence.rs +++ b/src/devices/presence.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use async_trait::async_trait; -use automation_macro::{LuaDevice, LuaDeviceConfig}; +use automation_macro::LuaDeviceConfig; use rumqttc::Publish; use tracing::{debug, trace, warn}; @@ -24,7 +24,7 @@ pub struct PresenceConfig { pub const DEFAULT_PRESENCE: bool = false; -#[derive(Debug, LuaDevice)] +#[derive(Debug)] pub struct Presence { config: PresenceConfig, devices: HashMap, diff --git a/src/devices/wake_on_lan.rs b/src/devices/wake_on_lan.rs index 1a0a35f..ea2eba0 100644 --- a/src/devices/wake_on_lan.rs +++ b/src/devices/wake_on_lan.rs @@ -1,7 +1,7 @@ use std::net::Ipv4Addr; use async_trait::async_trait; -use automation_macro::{LuaDevice, LuaDeviceConfig}; +use automation_macro::LuaDeviceConfig; use eui48::MacAddress; use google_home::device; use google_home::errors::ErrorCode; @@ -29,7 +29,7 @@ pub struct WakeOnLANConfig { pub client: WrappedAsyncClient, } -#[derive(Debug, LuaDevice)] +#[derive(Debug)] pub struct WakeOnLAN { config: WakeOnLANConfig, } diff --git a/src/devices/washer.rs b/src/devices/washer.rs index c27d8a0..c9ee627 100644 --- a/src/devices/washer.rs +++ b/src/devices/washer.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use automation_macro::{LuaDevice, LuaDeviceConfig}; +use automation_macro::LuaDeviceConfig; use rumqttc::Publish; use tracing::{debug, error, trace, warn}; @@ -24,7 +24,7 @@ pub struct WasherConfig { } // TODO: Add google home integration -#[derive(Debug, LuaDevice)] +#[derive(Debug)] pub struct Washer { config: WasherConfig,