refactor(LuaCommandManager): Express the semantics of Lua code explicitly
This commit is contained in:
parent
ed37d1239f
commit
bc1fc0cc02
|
@ -46,13 +46,11 @@ impl LuaCommandManager {
|
||||||
waiting for commands.."
|
waiting for commands.."
|
||||||
);
|
);
|
||||||
while let Some(command) = lua_command_rx.recv().await {
|
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();
|
let local_event_call_tx = event_call_tx.clone();
|
||||||
|
|
||||||
task::spawn_local(async move {
|
task::spawn_local(async move {
|
||||||
exec_lua_command(&command, local_event_call_tx)
|
exec_lua(&command, local_event_call_tx).await.expect(
|
||||||
.await
|
|
||||||
.expect(
|
|
||||||
"This should return all relevent errors \
|
"This should return all relevent errors \
|
||||||
by other messages, \
|
by other messages, \
|
||||||
this should never error",
|
this should never error",
|
||||||
|
@ -67,7 +65,7 @@ impl LuaCommandManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn exec_lua_command(command: &str, event_call_tx: mpsc::Sender<Event>) -> Result<()> {
|
async fn exec_lua(lua_code: &str, event_call_tx: mpsc::Sender<Event>) -> Result<()> {
|
||||||
let second_event_call_tx = event_call_tx.clone();
|
let second_event_call_tx = event_call_tx.clone();
|
||||||
let lua = LUA
|
let lua = LUA
|
||||||
.get_or_init(|| {
|
.get_or_init(|| {
|
||||||
|
@ -79,21 +77,23 @@ async fn exec_lua_command(command: &str, event_call_tx: mpsc::Sender<Event>) ->
|
||||||
.lock()
|
.lock()
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
info!("Recieved code to execute: `{}`, executing...", &command);
|
info!("Recieved code to execute: `{}`, executing...", &lua_code);
|
||||||
let output = lua.load(command).eval_async::<Value>().await;
|
let output = lua.load(lua_code).eval_async::<Value>().await;
|
||||||
match output {
|
match output {
|
||||||
Ok(out) => {
|
Ok(out) => {
|
||||||
let to_string_fn: Function = lua.globals().get("tostring").expect("This always exists");
|
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");
|
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);
|
||||||
|
|
||||||
|
if output != "nil" {
|
||||||
event_call_tx
|
event_call_tx
|
||||||
.send(Event::CommandEvent(Command::DisplayOutput(output), None))
|
.send(Event::CommandEvent(Command::DisplayOutput(output), None))
|
||||||
.await
|
.await
|
||||||
.context("Failed to send lua output command")?
|
.context("Failed to send lua output command")?
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Function `{}` returned error: `{}`", command, err);
|
error!("Lua code `{}` returned error: `{}`", lua_code, err);
|
||||||
event_call_tx
|
event_call_tx
|
||||||
.send(Event::CommandEvent(
|
.send(Event::CommandEvent(
|
||||||
Command::RaiseError(err.to_string()),
|
Command::RaiseError(err.to_string()),
|
||||||
|
|
Reference in New Issue