ActionCallback can now handle tuples
This commit is contained in:
parent
6b8d0b7d56
commit
d39432fa22
|
@ -1,6 +1,6 @@
|
|||
use std::marker::PhantomData;
|
||||
|
||||
use mlua::{FromLua, IntoLua};
|
||||
use mlua::{FromLua, IntoLuaMulti};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Internal {
|
||||
|
@ -8,12 +8,21 @@ struct Internal {
|
|||
lua: mlua::Lua,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ActionCallback<T> {
|
||||
internal: Option<Internal>,
|
||||
phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> Default for ActionCallback<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
internal: None,
|
||||
phantom: PhantomData::<T>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> FromLua for ActionCallback<T> {
|
||||
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
|
||||
let uuid = uuid::Uuid::new_v4();
|
||||
|
@ -32,7 +41,7 @@ impl<T> FromLua for ActionCallback<T> {
|
|||
// TODO: Return proper error here
|
||||
impl<T> ActionCallback<T>
|
||||
where
|
||||
T: IntoLua + Sync + Send + Clone + Copy + 'static,
|
||||
T: IntoLuaMulti + Sync + Send + Clone + 'static,
|
||||
{
|
||||
pub async fn call(&self, state: T) {
|
||||
let Some(internal) = self.internal.as_ref() else {
|
||||
|
|
|
@ -22,12 +22,11 @@ pub struct Config {
|
|||
#[device_config(from_lua)]
|
||||
pub client: WrappedAsyncClient,
|
||||
|
||||
// TODO: IntoLua is not implemented for unit type ()
|
||||
#[device_config(from_lua, default)]
|
||||
pub left_callback: ActionCallback<bool>,
|
||||
pub left_callback: ActionCallback<()>,
|
||||
|
||||
#[device_config(from_lua, default)]
|
||||
pub right_callback: ActionCallback<bool>,
|
||||
pub right_callback: ActionCallback<()>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -75,10 +74,10 @@ impl OnMqtt for HueSwitch {
|
|||
|
||||
match action {
|
||||
zigbee2mqtt_types::vendors::philips::Zigbee929003017102Action::Leftpress => {
|
||||
self.config.left_callback.call(true).await
|
||||
self.config.left_callback.call(()).await
|
||||
}
|
||||
zigbee2mqtt_types::vendors::philips::Zigbee929003017102Action::Rightpress => {
|
||||
self.config.right_callback.call(true).await
|
||||
self.config.right_callback.call(()).await
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user