From 4763d8ce3b833df1e7321a407b08666f69657fdb Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Thu, 8 Aug 2024 21:46:54 +0200 Subject: refactore runnable --- src/config/keymap/keymap_store.rs | 37 ++++------------------------------ src/config/keymap/mod.rs | 9 +++------ src/config/keymap/template.rs | 10 ++++------ src/lua/mod.rs | 1 + src/lua/ownedfunction.rs | 12 ++++++++++- src/lua/runnable.rs | 42 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 65 insertions(+), 46 deletions(-) create mode 100644 src/lua/runnable.rs diff --git a/src/config/keymap/keymap_store.rs b/src/config/keymap/keymap_store.rs index 233ac52..e5e32e5 100644 --- a/src/config/keymap/keymap_store.rs +++ b/src/config/keymap/keymap_store.rs @@ -4,14 +4,13 @@ use std::{ sync::{Arc, Mutex}, }; -use mlua::{Function, RegistryKey, Result}; use ratatui::crossterm::event::KeyEvent; -use crate::lua; +use crate::lua::runnable::Runnable; #[derive(Default, Clone)] pub struct KeyMapStore { - store: Option>>>, + store: Option>>>>, } impl KeyMapStore { @@ -19,7 +18,7 @@ impl KeyMapStore { Self { store: None } } - pub fn get(&mut self, event: KeyEvent) -> Option>> { + pub fn get(&mut self, event: KeyEvent) -> Option>>> { match &self.store { Some(store) => match store.get(&event) { Some(func) => Some(Arc::clone(func)), @@ -32,7 +31,7 @@ impl KeyMapStore { } } - pub fn map(&mut self, event: KeyEvent, func: impl Runnable + 'static) { + pub fn map(&mut self, event: KeyEvent, func: impl Runnable<(), bool> + 'static) { match &self.store { Some(_) => {} None => self.store = Some(HashMap::new()), @@ -44,34 +43,6 @@ impl KeyMapStore { } } -pub trait Runnable -where - Self: Send, -{ - fn run(&self) -> Result; -} - -impl Runnable for T -where - T: Fn() -> bool, - Self: Send, -{ - fn run(&self) -> Result { - Ok(self()) - } -} - -impl Runnable for RegistryKey -where - Self: Send, -{ - fn run(&self) -> Result { - let lua = lua::get(); - let func: Function = lua.registry_value(self)?; - Ok(func.call::<(), bool>(()).unwrap_or(false)) - } -} - impl fmt::Debug for KeyMapStore { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "KeyMapStore") diff --git a/src/config/keymap/mod.rs b/src/config/keymap/mod.rs index dc97ccc..3ff9f04 100644 --- a/src/config/keymap/mod.rs +++ b/src/config/keymap/mod.rs @@ -1,12 +1,10 @@ -use mlua::{Function, UserData}; +use mlua::UserData; use ratatui::crossterm::event::KeyEvent; -use self::{ - event_from_string::event_from_string, - keymap_store::{KeyMapStore, Runnable}, -}; +use self::{event_from_string::event_from_string, keymap_store::KeyMapStore}; use super::{GlobalConfig, DUMMY_CONFIG}; +use crate::lua::{self, ownedfunction::OwnedFunction, runnable::Runnable}; use template::KeyMapSections; pub mod event_from_string; @@ -46,4 +44,3 @@ KeyMap!( GlobalKeyMap => global, EditorKeyMap => editor, ); - diff --git a/src/config/keymap/template.rs b/src/config/keymap/template.rs index 5425eb8..cf68f80 100644 --- a/src/config/keymap/template.rs +++ b/src/config/keymap/template.rs @@ -23,7 +23,7 @@ macro_rules! KeyMapSections { if let Some(func) = func { let func = func.lock().unwrap(); - match func.run() { + match func.run((), &lua::get()) { Ok(value) => value, _ => def } @@ -32,7 +32,7 @@ macro_rules! KeyMapSections { } } - pub fn map(event: KeyEvent, func: impl Runnable + 'static) { + pub fn map(event: KeyEvent, func: impl Runnable<(), bool> + 'static) { GlobalConfig::instance_mut() .keymap .$key @@ -56,11 +56,9 @@ macro_rules! KeyMapSections { fn add_methods<'lua, M: mlua::prelude::LuaUserDataMethods<'lua, Self>>( methods: &mut M, ) { - methods.add_function("map", |lua, (event, func): (String, Function<'_>)| { - let key = lua.create_registry_value(func)?; - + methods.add_function("map", |_, (event, func): (String, OwnedFunction)| { if let Ok(event) = event_from_string(event) { - $name::map(event, key); + $name::map(event, func); } Ok(()) }); diff --git a/src/lua/mod.rs b/src/lua/mod.rs index e10d83b..afa6171 100644 --- a/src/lua/mod.rs +++ b/src/lua/mod.rs @@ -19,6 +19,7 @@ pub mod evalsto; pub mod iobuffer; pub mod ownedfunction; pub mod runtime; +pub mod runnable; lazy_static! { static ref LUA: Mutex = { diff --git a/src/lua/ownedfunction.rs b/src/lua/ownedfunction.rs index a8d81cf..54e39df 100644 --- a/src/lua/ownedfunction.rs +++ b/src/lua/ownedfunction.rs @@ -1,4 +1,4 @@ -use mlua::{Function, Lua, RegistryKey, Value}; +use mlua::{Error, FromLua, Function, Lua, RegistryKey, Value}; #[derive(Debug)] pub struct OwnedFunction { @@ -15,3 +15,13 @@ impl OwnedFunction { lua.registry_value(&self.key).unwrap() } } + +impl<'lua> FromLua<'lua> for OwnedFunction { + fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> mlua::prelude::LuaResult { + if value.is_function() { + Ok(OwnedFunction::new(value, lua)) + } else { + Err(Error::runtime("value needs to be function")) + } + } +} diff --git a/src/lua/runnable.rs b/src/lua/runnable.rs new file mode 100644 index 0000000..adda512 --- /dev/null +++ b/src/lua/runnable.rs @@ -0,0 +1,42 @@ +use mlua::{FromLuaMulti, Function, IntoLuaMulti, Lua, Result}; + +use super::ownedfunction::OwnedFunction; + +pub trait Runnable +where + Self: Send, +{ + fn run<'lua>(&self, arg: A, lua: &'lua Lua) -> Result + where + A: IntoLuaMulti<'lua>, + R: FromLuaMulti<'lua>; +} + +impl Runnable for T +where + T: Fn(A) -> R, + Self: Send, +{ + fn run<'lua>(&self, arg: A, _: &'lua Lua) -> Result + where + A: IntoLuaMulti<'lua>, + R: FromLuaMulti<'lua>, + { + Ok(self(arg)) + } +} + +impl Runnable for OwnedFunction +where + Self: Send, +{ + fn run<'lua>(&self, arg: A, lua: &'lua Lua) -> Result + where + A: IntoLuaMulti<'lua>, + R: FromLuaMulti<'lua>, + { + let func: Function = self.get(lua); + func.call::(arg) + } +} + -- cgit v1.2.3-70-g09d2