feat!: Improve lua module registration
Instead of having to call all the module registration functions in one place it is possible for each module to register itself in a global registry. During startup all the all the modules will be registered automatically. This does currently have one weakness, to need to ensure that the crate is linked.
This commit is contained in:
@@ -11,6 +11,7 @@ dyn-clone = { workspace = true }
|
||||
futures = { workspace = true }
|
||||
google_home = { workspace = true }
|
||||
indexmap = { workspace = true }
|
||||
inventory = { workspace = true }
|
||||
mlua = { workspace = true }
|
||||
rumqttc = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
@@ -12,3 +12,26 @@ pub mod lua;
|
||||
pub mod messages;
|
||||
pub mod mqtt;
|
||||
pub mod schedule;
|
||||
|
||||
type RegisterFn = fn(lua: &mlua::Lua) -> mlua::Result<mlua::Table>;
|
||||
|
||||
pub struct Module {
|
||||
name: &'static str,
|
||||
register_fn: RegisterFn,
|
||||
}
|
||||
|
||||
impl Module {
|
||||
pub const fn new(name: &'static str, register_fn: RegisterFn) -> Self {
|
||||
Self { name, register_fn }
|
||||
}
|
||||
|
||||
pub const fn get_name(&self) -> &'static str {
|
||||
self.name
|
||||
}
|
||||
|
||||
pub fn register(&self, lua: &mlua::Lua) -> mlua::Result<mlua::Table> {
|
||||
(self.register_fn)(lua)
|
||||
}
|
||||
}
|
||||
|
||||
inventory::collect!(Module);
|
||||
|
||||
Reference in New Issue
Block a user