From 9f244b34759756ce8ddd590266c79f7ae23fb2eb Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Wed, 15 Oct 2025 03:34:56 +0200 Subject: [PATCH] feat: Added/expanded Typed impls --- Cargo.lock | 4 +-- automation_lib/src/action_callback.rs | 6 ++++ automation_lib/src/config.rs | 2 +- automation_lib/src/device_manager.rs | 7 +++++ automation_lib/src/lua/utils/timeout.rs | 42 +++++++++++++++++++++++++ automation_lib/src/mqtt.rs | 31 ++++++++++++++++++ 6 files changed, 89 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f025384..0dbaf70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1101,7 +1101,7 @@ dependencies = [ [[package]] name = "lua_typed" version = "0.1.0" -source = "git+https://git.huizinga.dev/Dreaded_X/lua_typed#d5d6fc1638bd108514899a792ee64335af50fc8b" +source = "git+https://git.huizinga.dev/Dreaded_X/lua_typed#08f5c4533a93131e8eda6702c062fb841d14d4e1" dependencies = [ "eui48", "lua_typed_macro", @@ -1110,7 +1110,7 @@ dependencies = [ [[package]] name = "lua_typed_macro" version = "0.1.0" -source = "git+https://git.huizinga.dev/Dreaded_X/lua_typed#d5d6fc1638bd108514899a792ee64335af50fc8b" +source = "git+https://git.huizinga.dev/Dreaded_X/lua_typed#08f5c4533a93131e8eda6702c062fb841d14d4e1" dependencies = [ "convert_case", "itertools", diff --git a/automation_lib/src/action_callback.rs b/automation_lib/src/action_callback.rs index d1eb384..b78bc0a 100644 --- a/automation_lib/src/action_callback.rs +++ b/automation_lib/src/action_callback.rs @@ -10,6 +10,12 @@ pub struct ActionCallback

{ _parameters: PhantomData

, } +impl Typed for ActionCallback<()> { + fn type_name() -> String { + "fun() | fun()[]".into() + } +} + impl Typed for ActionCallback { fn type_name() -> String { let type_name = A::type_name(); diff --git a/automation_lib/src/config.rs b/automation_lib/src/config.rs index 4b3b334..e0c9e0e 100644 --- a/automation_lib/src/config.rs +++ b/automation_lib/src/config.rs @@ -5,7 +5,7 @@ use lua_typed::Typed; use rumqttc::{MqttOptions, Transport}; use serde::Deserialize; -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Typed)] pub struct MqttConfig { pub host: String, pub port: u16, diff --git a/automation_lib/src/device_manager.rs b/automation_lib/src/device_manager.rs index 405d382..29b7b54 100644 --- a/automation_lib/src/device_manager.rs +++ b/automation_lib/src/device_manager.rs @@ -4,6 +4,7 @@ use std::sync::Arc; use futures::Future; use futures::future::join_all; +use lua_typed::Typed; use tokio::sync::{RwLock, RwLockReadGuard}; use tokio_cron_scheduler::{Job, JobScheduler}; use tracing::{debug, instrument, trace}; @@ -142,3 +143,9 @@ impl mlua::UserData for DeviceManager { methods.add_method("event_channel", |_lua, this, ()| Ok(this.event_channel())) } } + +impl Typed for DeviceManager { + fn type_name() -> String { + "DeviceManager".into() + } +} diff --git a/automation_lib/src/lua/utils/timeout.rs b/automation_lib/src/lua/utils/timeout.rs index c770d36..2066287 100644 --- a/automation_lib/src/lua/utils/timeout.rs +++ b/automation_lib/src/lua/utils/timeout.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use std::time::Duration; +use lua_typed::Typed; use tokio::sync::RwLock; use tokio::task::JoinHandle; use tracing::debug; @@ -74,3 +75,44 @@ impl mlua::UserData for Timeout { }); } } + +impl Typed for Timeout { + fn type_name() -> String { + "Timeout".into() + } + + fn generate_header() -> Option { + let type_name = Self::type_name(); + Some(format!("---@class {type_name}\nlocal {type_name}\n")) + } + + fn generate_members() -> Option { + let mut output = String::new(); + + let type_name = Self::type_name(); + + output += &format!( + "---@async\n---@param timeout number\n---@param callback {}\nfunction {type_name}:start(timeout, callback) end\n", + ActionCallback::<()>::type_name() + ); + + output += &format!("---@async\nfunction {type_name}:cancel() end\n",); + + output += + &format!("---@async\n---@return boolean\nfunction {type_name}:is_waiting() end\n",); + + Some(output) + } + + fn generate_footer() -> Option { + let mut output = String::new(); + + let type_name = Self::type_name(); + + output += &format!("utils.{type_name} = {{}}\n"); + output += &format!("---@return {type_name}\n"); + output += &format!("function utils.{type_name}.new() end\n"); + + Some(output) + } +} diff --git a/automation_lib/src/mqtt.rs b/automation_lib/src/mqtt.rs index 3fb7d4c..b9fbbe9 100644 --- a/automation_lib/src/mqtt.rs +++ b/automation_lib/src/mqtt.rs @@ -5,6 +5,7 @@ use mlua::FromLua; use rumqttc::{AsyncClient, Event, EventLoop, Incoming}; use tracing::{debug, warn}; +use crate::config::MqttConfig; use crate::event::{self, EventChannel}; #[derive(Debug, Clone, FromLua)] @@ -14,6 +15,36 @@ impl Typed for WrappedAsyncClient { fn type_name() -> String { "AsyncClient".into() } + + fn generate_header() -> Option { + let type_name = Self::type_name(); + Some(format!("---@class {type_name}\nlocal {type_name}\n")) + } + + fn generate_members() -> Option { + let mut output = String::new(); + + let type_name = Self::type_name(); + + output += &format!( + "---@async\n---@param topic string\n---@param message table?\nfunction {type_name}:send_message(topic, message) end\n" + ); + + Some(output) + } + + fn generate_footer() -> Option { + let mut output = String::new(); + + let type_name = Self::type_name(); + + output += &format!("mqtt.{type_name} = {{}}\n"); + output += &format!("---@param config {}\n", MqttConfig::type_name()); + output += &format!("---@return {type_name}\n"); + output += "function mqtt.new(config) end\n"; + + Some(output) + } } impl Deref for WrappedAsyncClient {