diff --git a/src/action_callback.rs b/src/action_callback.rs new file mode 100644 index 0000000..750cab8 --- /dev/null +++ b/src/action_callback.rs @@ -0,0 +1,39 @@ +use std::marker::PhantomData; + +use mlua::{FromLua, IntoLua}; + +#[derive(Debug, Clone)] +pub struct ActionCallback { + uuid: uuid::Uuid, + lua: mlua::Lua, + phantom: PhantomData, +} + +impl FromLua for ActionCallback { + fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result { + let uuid = uuid::Uuid::new_v4(); + lua.set_named_registry_value(&uuid.to_string(), value)?; + + Ok(ActionCallback { + uuid, + lua: lua.clone(), + phantom: PhantomData::, + }) + } +} + +// TODO: Return proper error here +impl ActionCallback +where + T: IntoLua + Sync + Send + Clone + Copy + 'static, +{ + pub async fn call(&self, state: T) { + let uuid = self.uuid; + + let callback: mlua::Value = self.lua.named_registry_value(&uuid.to_string()).unwrap(); + match callback { + mlua::Value::Function(f) => f.call_async::<()>(state).await.unwrap(), + _ => todo!("Only functions are currently supported"), + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 3cbd2de..2de680a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ #![feature(specialization)] #![feature(let_chains)] +pub mod action_callback; pub mod auth; pub mod config; pub mod device_manager;