From 8a3143a3ea5a4da6e28293cdc16ffc6b3bf9f6a0 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Wed, 22 Oct 2025 03:59:40 +0200 Subject: [PATCH] feat: Added type alias for setup and schedule types --- definitions/config.lua | 8 +++++-- src/bin/generate_definitions.rs | 8 ++++++- src/config.rs | 40 ++++++++++++++++++++++++++++----- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/definitions/config.lua b/definitions/config.lua index 1cef4fd..9bd274c 100644 --- a/definitions/config.lua +++ b/definitions/config.lua @@ -13,10 +13,14 @@ local FulfillmentConfig ---@field mqtt MqttConfig local Config +---@alias SetupFunction fun(mqtt_client: AsyncClient): Module | DeviceInterface[] | nil + +---@alias Schedule table + ---@class Module ----@field setup (fun(mqtt_client: AsyncClient): Module | DeviceInterface[] | nil)? +---@field setup (SetupFunction)? ---@field devices (DeviceInterface)[]? ----@field schedule table? +---@field schedule Schedule? ---@field [number] (Module)[]? local Module diff --git a/src/bin/generate_definitions.rs b/src/bin/generate_definitions.rs index fbff60a..495b5b0 100644 --- a/src/bin/generate_definitions.rs +++ b/src/bin/generate_definitions.rs @@ -1,7 +1,9 @@ use std::fs::{self, File}; use std::io::Write; -use automation::config::{Config, FulfillmentConfig, Module as ConfigModule}; +use automation::config::{ + Config, FulfillmentConfig, Module as ConfigModule, Schedule, SetupFunction, +}; use automation_lib::Module; use automation_lib::mqtt::{MqttConfig, WrappedAsyncClient}; use lua_typed::Typed; @@ -35,6 +37,10 @@ fn config_definitions() -> String { output += "\n"; output += &Config::generate_full().expect("Config should have a definition"); output += "\n"; + output += &SetupFunction::generate_full().expect("SetupFunction should have a definition"); + output += "\n"; + output += &Schedule::generate_full().expect("Schedule should have a definition"); + output += "\n"; output += &ConfigModule::generate_full().expect("Module should have a definition"); output += "\n"; output += &MqttConfig::generate_full().expect("MqttConfig should have a definition"); diff --git a/src/config.rs b/src/config.rs index fd71452..eb8793e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -41,11 +41,16 @@ pub struct SetupFunction(mlua::Function); impl Typed for SetupFunction { fn type_name() -> String { - format!( - "fun(mqtt_client: {}): {} | DeviceInterface[] | nil", + "SetupFunction".into() + } + + fn generate_header() -> Option { + Some(format!( + "---@alias {} fun(mqtt_client: {}): {} | DeviceInterface[] | nil\n", + Self::type_name(), WrappedAsyncClient::type_name(), Module::type_name() - ) + )) } } @@ -55,11 +60,34 @@ impl FromLua for SetupFunction { } } +#[derive(Debug, Default)] +pub struct Schedule(HashMap>); + +impl Typed for Schedule { + fn type_name() -> String { + "Schedule".into() + } + + fn generate_header() -> Option { + Some(format!( + "---@alias {} {}\n", + Self::type_name(), + HashMap::>::type_name(), + )) + } +} + +impl FromLua for Schedule { + fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result { + Ok(Self(FromLua::from_lua(value, lua)?)) + } +} + #[derive(Debug, Default)] pub struct Module { pub setup: Option, pub devices: Vec>, - pub schedule: HashMap>, + pub schedule: Schedule, pub modules: Vec, } @@ -82,7 +110,7 @@ impl Typed for Module { "#, Option::::type_name(), Vec::>::type_name(), - HashMap::>::type_name(), + Schedule::type_name(), Vec::::type_name(), )) } @@ -181,7 +209,7 @@ impl Modules { } devices.extend(module.devices); - for (cron, f) in module.schedule { + for (cron, f) in module.schedule.0 { scheduler.add_job(cron, f); } }