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

View File

@@ -17,15 +17,25 @@ pub mod mqtt;
pub mod schedule;
type RegisterFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::Table>;
type DefinitionsFn = fn() -> String;
pub struct Module {
name: &'static str,
register_fn: RegisterFn,
definitions_fn: Option<DefinitionsFn>,
}
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<DefinitionsFn>,
) -> 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<mlua::Table> {
(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<()> {