feat: Implement useful traits to simplify code
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::net::{Ipv4Addr, SocketAddr};
|
||||
use std::ops::Deref;
|
||||
|
||||
use automation_lib::action_callback::ActionCallback;
|
||||
use automation_lib::device::Device;
|
||||
@@ -60,6 +61,14 @@ impl FromLua for SetupFunction {
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for SetupFunction {
|
||||
type Target = mlua::Function;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Schedule(HashMap<String, ActionCallback<()>>);
|
||||
|
||||
@@ -83,6 +92,16 @@ impl FromLua for Schedule {
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoIterator for Schedule {
|
||||
type Item = <HashMap<String, ActionCallback<()>> as IntoIterator>::Item;
|
||||
|
||||
type IntoIter = <HashMap<String, ActionCallback<()>> as IntoIterator>::IntoIter;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Module {
|
||||
pub setup: Option<SetupFunction>,
|
||||
@@ -189,7 +208,7 @@ impl Modules {
|
||||
modules.extend(module.modules);
|
||||
|
||||
if let Some(setup) = module.setup {
|
||||
let result: mlua::Value = setup.0.call_async(client.clone()).await?;
|
||||
let result: mlua::Value = setup.call_async(client.clone()).await?;
|
||||
|
||||
if result.is_nil() {
|
||||
// We ignore nil results
|
||||
@@ -209,7 +228,7 @@ impl Modules {
|
||||
}
|
||||
|
||||
devices.extend(module.devices);
|
||||
for (cron, f) in module.schedule.0 {
|
||||
for (cron, f) in module.schedule {
|
||||
scheduler.add_job(cron, f);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user