feat: Added optional definition function to module

This commit is contained in:
2025-10-15 03:48:03 +02:00
parent be1602d0e2
commit 17a68e8991
6 changed files with 83 additions and 29 deletions

1
Cargo.lock generated
View File

@@ -98,6 +98,7 @@ dependencies = [
"config", "config",
"git-version", "git-version",
"google_home", "google_home",
"inventory",
"mlua", "mlua",
"reqwest", "reqwest",
"rumqttc", "rumqttc",

View File

@@ -76,6 +76,7 @@ config = { version = "0.15.15", default-features = false, features = [
] } ] }
git-version = "0.3.9" git-version = "0.3.9"
google_home = { workspace = true } google_home = { workspace = true }
inventory = { workspace = true }
mlua = { workspace = true } mlua = { workspace = true }
reqwest = { workspace = true } reqwest = { workspace = true }
rumqttc = { workspace = true } rumqttc = { workspace = true }

View File

@@ -15,20 +15,7 @@ mod zigbee;
use automation_lib::Module; use automation_lib::Module;
use automation_lib::device::{Device, LuaDeviceCreate}; use automation_lib::device::{Device, LuaDeviceCreate};
use tracing::debug; use tracing::{debug, warn};
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;
type DeviceNameFn = fn() -> String; type DeviceNameFn = fn() -> String;
type RegisterDeviceFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::AnyUserData>; type RegisterDeviceFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::AnyUserData>;
@@ -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); inventory::collect!(RegisteredDevice);
pub fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> { pub fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
@@ -71,7 +70,9 @@ pub fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
Ok(devices) Ok(devices)
} }
inventory::submit! {Module::new("automation:devices", create_module)} type RegisterTypeFn = fn() -> Option<String>;
pub struct RegisteredType(RegisterTypeFn);
macro_rules! register_type { macro_rules! register_type {
($ty:ty) => { ($ty:ty) => {
@@ -80,20 +81,25 @@ macro_rules! register_type {
)); ));
}; };
} }
pub(crate) use register_type; pub(crate) use register_type;
type RegisterTypeFn = fn() -> Option<String>;
pub struct RegisteredType(RegisterTypeFn);
inventory::collect!(RegisteredType); inventory::collect!(RegisteredType);
pub fn generate_definitions() { fn generate_definitions() -> String {
println!("---@meta\n\nlocal devices\n"); let mut output = String::new();
output += "---@meta\n\nlocal devices\n\n";
for ty in inventory::iter::<RegisteredType> { for ty in inventory::iter::<RegisteredType> {
let def = ty.0().unwrap(); if let Some(def) = ty.0() {
println!("{def}"); 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))}

View File

@@ -17,15 +17,25 @@ pub mod mqtt;
pub mod schedule; pub mod schedule;
type RegisterFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::Table>; type RegisterFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::Table>;
type DefinitionsFn = fn() -> String;
pub struct Module { pub struct Module {
name: &'static str, name: &'static str,
register_fn: RegisterFn, register_fn: RegisterFn,
definitions_fn: Option<DefinitionsFn>,
} }
impl Module { impl Module {
pub const fn new(name: &'static str, register_fn: RegisterFn) -> Self { pub const fn new(
Self { name, register_fn } name: &'static str,
register_fn: RegisterFn,
definitions_fn: Option<DefinitionsFn>,
) -> Self {
Self {
name,
register_fn,
definitions_fn,
}
} }
pub const fn get_name(&self) -> &'static str { pub const fn get_name(&self) -> &'static str {
@@ -35,6 +45,10 @@ impl Module {
pub fn register(&self, lua: &mlua::Lua) -> mlua::Result<mlua::Table> { pub fn register(&self, lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
(self.register_fn)(lua) (self.register_fn)(lua)
} }
pub fn definitions(&self) -> Option<String> {
self.definitions_fn.map(|f| f())
}
} }
pub fn load_modules(lua: &mlua::Lua) -> mlua::Result<()> { pub fn load_modules(lua: &mlua::Lua) -> mlua::Result<()> {

View File

@@ -29,4 +29,20 @@ fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
Ok(utils) 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))}

View File

@@ -132,4 +132,20 @@ fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
Ok(mqtt) 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))}