forked from trinitrix/core
refactor(LuaCommandManager): Express the semantics of Lua code explicitly
This commit is contained in:
parent
ed37d1239f
commit
bc1fc0cc02
|
@ -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<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 lua = LUA
|
||||
.get_or_init(|| {
|
||||
|
@ -79,21 +77,23 @@ async fn exec_lua_command(command: &str, event_call_tx: mpsc::Sender<Event>) ->
|
|||
.lock()
|
||||
.await;
|
||||
|
||||
info!("Recieved code to execute: `{}`, executing...", &command);
|
||||
let output = lua.load(command).eval_async::<Value>().await;
|
||||
info!("Recieved code to execute: `{}`, executing...", &lua_code);
|
||||
let output = lua.load(lua_code).eval_async::<Value>().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()),
|
||||
|
|
Loading…
Reference in New Issue