feat: Added optional definition function to module
This commit is contained in:
@@ -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<()> {
|
||||
|
||||
@@ -29,4 +29,20 @@ fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||
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))}
|
||||
|
||||
@@ -132,4 +132,20 @@ fn create_module(lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||
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))}
|
||||
|
||||
Reference in New Issue
Block a user