From bc1fc0cc02678691d3d1107e5d9b00f11f641dd8 Mon Sep 17 00:00:00 2001 From: Soispha Date: Sat, 9 Sep 2023 19:55:16 +0200 Subject: [PATCH] refactor(LuaCommandManager): Express the semantics of Lua code explicitly --- .../lua_command_manager/mod.rs | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/app/command_interface/lua_command_manager/mod.rs b/src/app/command_interface/lua_command_manager/mod.rs index 9b5bb24..de84bbc 100644 --- a/src/app/command_interface/lua_command_manager/mod.rs +++ b/src/app/command_interface/lua_command_manager/mod.rs @@ -42,21 +42,19 @@ impl LuaCommandManager { let local = LocalSet::new(); local.spawn_local(async move { info!( - "Lua command handling initialized, \ + "Lua command handling initialized, \ waiting for commands.." ); while let Some(command) = lua_command_rx.recv().await { - debug!("Recieved lua code: {}", &command); + debug!("Recieved lua code (in LuaCommandHandler): {}", &command); let local_event_call_tx = event_call_tx.clone(); task::spawn_local(async move { - exec_lua_command(&command, local_event_call_tx) - .await - .expect( - "This should return all relevent errors \ + exec_lua(&command, local_event_call_tx).await.expect( + "This should return all relevent errors \ by other messages, \ this should never error", - ); + ); }); } }); @@ -67,7 +65,7 @@ impl LuaCommandManager { } } -async fn exec_lua_command(command: &str, event_call_tx: mpsc::Sender) -> Result<()> { +async fn exec_lua(lua_code: &str, event_call_tx: mpsc::Sender) -> Result<()> { let second_event_call_tx = event_call_tx.clone(); let lua = LUA .get_or_init(|| { @@ -79,21 +77,23 @@ async fn exec_lua_command(command: &str, event_call_tx: mpsc::Sender) -> .lock() .await; - info!("Recieved code to execute: `{}`, executing...", &command); - let output = lua.load(command).eval_async::().await; + info!("Recieved code to execute: `{}`, executing...", &lua_code); + let output = lua.load(lua_code).eval_async::().await; match output { Ok(out) => { let to_string_fn: Function = lua.globals().get("tostring").expect("This always exists"); let output: String = to_string_fn.call(out).expect("tostring should not error"); - info!("Function `{}` returned: `{}`", command, &output); + info!("Lua code `{}` evaluated to: `{}`", lua_code, &output); - event_call_tx - .send(Event::CommandEvent(Command::DisplayOutput(output), None)) - .await - .context("Failed to send lua output command")? + if output != "nil" { + event_call_tx + .send(Event::CommandEvent(Command::DisplayOutput(output), None)) + .await + .context("Failed to send lua output command")? + } } Err(err) => { - error!("Function `{}` returned error: `{}`", command, err); + error!("Lua code `{}` returned error: `{}`", lua_code, err); event_call_tx .send(Event::CommandEvent( Command::RaiseError(err.to_string()),