From b327d3217738c15d95441730ce582b9ae368efa7 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Wed, 15 Oct 2025 03:48:03 +0200 Subject: [PATCH] feat: Added optional definition function to module --- Cargo.lock | 1 + Cargo.toml | 1 + automation_devices/src/lib.rs | 56 ++++++++++++++++------------- automation_lib/src/lib.rs | 18 ++++++++-- automation_lib/src/lua/utils/mod.rs | 19 +++++++++- automation_lib/src/mqtt.rs | 18 +++++++++- 6 files changed, 84 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0dbaf70..142c38c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,6 +98,7 @@ dependencies = [ "config", "git-version", "google_home", + "inventory", "mlua", "reqwest", "rumqttc", diff --git a/Cargo.toml b/Cargo.toml index ea5e5f0..7e3c0e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -76,6 +76,7 @@ config = { version = "0.15.15", default-features = false, features = [ ] } git-version = "0.3.9" google_home = { workspace = true } +inventory = { workspace = true } mlua = { workspace = true } reqwest = { workspace = true } rumqttc = { workspace = true } diff --git a/automation_devices/src/lib.rs b/automation_devices/src/lib.rs index 0e5838c..a560398 100644 --- a/automation_devices/src/lib.rs +++ b/automation_devices/src/lib.rs @@ -15,20 +15,7 @@ mod zigbee; use automation_lib::Module; use automation_lib::device::{Device, LuaDeviceCreate}; -use tracing::debug; - -macro_rules! register_device { - ($device:ty) => { - ::inventory::submit!(crate::RegisteredDevice::new( - <$device as ::lua_typed::Typed>::type_name, - ::mlua::Lua::create_proxy::<$device> - )); - - crate::register_type!($device); - }; -} - -pub(crate) use register_device; +use tracing::{debug, warn}; type DeviceNameFn = fn() -> String; type RegisterDeviceFn = fn(lua: &mlua::Lua) -> mlua::Result; @@ -55,6 +42,18 @@ impl RegisteredDevice { } } +macro_rules! register_device { + ($device:ty) => { + ::inventory::submit!(crate::RegisteredDevice::new( + <$device as ::lua_typed::Typed>::type_name, + ::mlua::Lua::create_proxy::<$device> + )); + + crate::register_type!($device); + }; +} +pub(crate) use register_device; + inventory::collect!(RegisteredDevice); pub fn create_module(lua: &mlua::Lua) -> mlua::Result { @@ -71,7 +70,9 @@ pub fn create_module(lua: &mlua::Lua) -> mlua::Result { Ok(devices) } -inventory::submit! {Module::new("automation:devices", create_module)} +type RegisterTypeFn = fn() -> Option; + +pub struct RegisteredType(RegisterTypeFn); macro_rules! register_type { ($ty:ty) => { @@ -80,20 +81,25 @@ macro_rules! register_type { )); }; } - pub(crate) use register_type; -type RegisterTypeFn = fn() -> Option; - -pub struct RegisteredType(RegisterTypeFn); - inventory::collect!(RegisteredType); -pub fn generate_definitions() { - println!("---@meta\n\nlocal devices\n"); +fn generate_definitions() -> String { + let mut output = String::new(); + + output += "---@meta\n\nlocal devices\n\n"; for ty in inventory::iter:: { - let def = ty.0().unwrap(); - println!("{def}"); + if let Some(def) = ty.0() { + output += &(def + "\n"); + } else { + // NOTE: Due to how this works the typed is erased, so we don't know the cause + warn!("Registered type is missing generate_full function"); + } } - println!("return devices") + output += "return devices"; + + output } + +inventory::submit! {Module::new("automation:devices", create_module, Some(generate_definitions))} diff --git a/automation_lib/src/lib.rs b/automation_lib/src/lib.rs index 3fa87e8..ec9a05c 100644 --- a/automation_lib/src/lib.rs +++ b/automation_lib/src/lib.rs @@ -17,15 +17,25 @@ pub mod mqtt; pub mod schedule; type RegisterFn = fn(lua: &mlua::Lua) -> mlua::Result; +type DefinitionsFn = fn() -> String; pub struct Module { name: &'static str, register_fn: RegisterFn, + definitions_fn: Option, } impl Module { - pub const fn new(name: &'static str, register_fn: RegisterFn) -> Self { - Self { name, register_fn } + pub const fn new( + name: &'static str, + register_fn: RegisterFn, + definitions_fn: Option, + ) -> Self { + Self { + name, + register_fn, + definitions_fn, + } } pub const fn get_name(&self) -> &'static str { @@ -35,6 +45,10 @@ impl Module { pub fn register(&self, lua: &mlua::Lua) -> mlua::Result { (self.register_fn)(lua) } + + pub fn definitions(&self) -> Option { + self.definitions_fn.map(|f| f()) + } } pub fn load_modules(lua: &mlua::Lua) -> mlua::Result<()> { diff --git a/automation_lib/src/lua/utils/mod.rs b/automation_lib/src/lua/utils/mod.rs index 2fc67d2..ba00332 100644 --- a/automation_lib/src/lua/utils/mod.rs +++ b/automation_lib/src/lua/utils/mod.rs @@ -2,6 +2,7 @@ mod timeout; use std::time::{SystemTime, UNIX_EPOCH}; +use lua_typed::Typed; pub use timeout::Timeout; use crate::Module; @@ -28,4 +29,20 @@ fn create_module(lua: &mlua::Lua) -> mlua::Result { Ok(utils) } -inventory::submit! {Module::new("automation:utils", create_module)} +fn generate_definitions() -> String { + let mut output = String::new(); + + output += "---@meta\n\nlocal utils\n\n"; + + output += &Timeout::generate_full().expect("Timeout should have generate_full"); + output += "\n"; + + output += "---@return string\nfunction utils.get_hostname() end\n\n"; + output += "---@return integer\nfunction utils.get_epoch() end\n\n"; + + output += "return utils"; + + output +} + +inventory::submit! {Module::new("automation:utils", create_module, Some(generate_definitions))} diff --git a/automation_lib/src/mqtt.rs b/automation_lib/src/mqtt.rs index a464a45..038251c 100644 --- a/automation_lib/src/mqtt.rs +++ b/automation_lib/src/mqtt.rs @@ -132,4 +132,20 @@ fn create_module(lua: &mlua::Lua) -> mlua::Result { Ok(mqtt) } -inventory::submit! {Module::new("automation:mqtt", create_module)} +fn generate_definitions() -> String { + let mut output = String::new(); + + output += "---@meta\n\nlocal mqtt\n\n"; + + output += &MqttConfig::generate_full().expect("WrappedAsyncClient should have generate_full"); + output += "\n"; + output += + &WrappedAsyncClient::generate_full().expect("WrappedAsyncClient should have generate_full"); + output += "\n"; + + output += "return mqtt"; + + output +} + +inventory::submit! {Module::new("automation:mqtt", create_module, Some(generate_definitions))}