feat: Added type alias for setup and schedule types

This commit is contained in:
2025-10-22 03:59:40 +02:00
parent 9546585440
commit 8a3143a3ea
3 changed files with 47 additions and 9 deletions

View File

@@ -13,10 +13,14 @@ local FulfillmentConfig
---@field mqtt MqttConfig
local Config
---@alias SetupFunction fun(mqtt_client: AsyncClient): Module | DeviceInterface[] | nil
---@alias Schedule table<string, fun() | fun()[]>
---@class Module
---@field setup (fun(mqtt_client: AsyncClient): Module | DeviceInterface[] | nil)?
---@field setup (SetupFunction)?
---@field devices (DeviceInterface)[]?
---@field schedule table<string, fun() | fun()[]>?
---@field schedule Schedule?
---@field [number] (Module)[]?
local Module

View File

@@ -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");

View File

@@ -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<String> {
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<String, ActionCallback<()>>);
impl Typed for Schedule {
fn type_name() -> String {
"Schedule".into()
}
fn generate_header() -> Option<String> {
Some(format!(
"---@alias {} {}\n",
Self::type_name(),
HashMap::<String, ActionCallback<()>>::type_name(),
))
}
}
impl FromLua for Schedule {
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
Ok(Self(FromLua::from_lua(value, lua)?))
}
}
#[derive(Debug, Default)]
pub struct Module {
pub setup: Option<SetupFunction>,
pub devices: Vec<Box<dyn Device>>,
pub schedule: HashMap<String, ActionCallback<()>>,
pub schedule: Schedule,
pub modules: Vec<Module>,
}
@@ -82,7 +110,7 @@ impl Typed for Module {
"#,
Option::<SetupFunction>::type_name(),
Vec::<Box<dyn Device>>::type_name(),
HashMap::<String, ActionCallback<()>>::type_name(),
Schedule::type_name(),
Vec::<Module>::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);
}
}