Feat(treewide): Add a way for Commands to return more than just strings

This commit is contained in:
Benedikt Peetz 2023-07-26 21:04:04 +02:00
parent a4c09c7b42
commit 49c9e90ba6
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
11 changed files with 324 additions and 112 deletions

12
Cargo.lock generated
View File

@ -811,6 +811,15 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "erased-serde"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da96524cc884f6558f1769b6c46686af2fe8e8b4cd253bd5a3cdba8181b8e070"
dependencies = [
"serde",
]
[[package]]
name = "errno"
version = "0.3.1"
@ -1646,6 +1655,7 @@ checksum = "07366ed2cd22a3b000aed076e2b68896fb46f06f1f5786c5962da73c0af01577"
dependencies = [
"bstr",
"cc",
"erased-serde",
"futures-core",
"futures-task",
"futures-util",
@ -1653,6 +1663,7 @@ dependencies = [
"once_cell",
"pkg-config",
"rustc-hash",
"serde",
]
[[package]]
@ -2693,7 +2704,6 @@ dependencies = [
"libc",
"mio",
"num_cpus",
"parking_lot 0.12.1",
"pin-project-lite",
"socket2",
"tokio-macros",

View File

@ -19,7 +19,7 @@ tokio = { version = "1.29", features = ["macros", "rt-multi-thread"] }
# lua stuff
language_macros = { path = "./language_macros" }
mlua = { version = "0.8.9", features = ["lua54", "async", "send"] }
mlua = { version = "0.8.9", features = ["lua54", "async", "send", "serialize"] }
once_cell = "1.18.0"
# tui feature specific parts

View File

@ -126,12 +126,71 @@ fn get_function_body(field: &Field, has_input: bool, output_type: &Option<Type>)
let function_return = if let Some(_) = output_type {
quote! {
let output: mlua::Value = lua.to_value(output).expect("This conversion should (indirectely) be checked at compile time");
return Ok(output);
let converted_output = lua
.to_value(&output)
.expect("This conversion should (indirectely) be checked at compile time");
if let mlua::Value::Table(table) = converted_output {
let real_output: mlua::Value = match output {
CommandTransferValue::Nil => table
.get("Nil")
.expect("This should exist"),
CommandTransferValue::Boolean(_) => table
.get("Boolean")
.expect("This should exist"),
CommandTransferValue::Integer(_) => table
.get("Integer")
.expect("This should exist"),
CommandTransferValue::Number(_) => table
.get("Number")
.expect("This should exist"),
CommandTransferValue::String(_) => table
.get("String")
.expect("This should exist"),
CommandTransferValue::Table(_) => {
todo!()
// FIXME(@Soispha): This returns a table with the values wrapped the
// same way the values above are wrapped. That is (from the greet_multiple
// function):
// ```json
// {
// "Table": {
// "UserName1": {
// "Integer": 2
// }
// }
// }
// ```
// whilst the output should be:
// ```json
// {
// "UserName1": 2
// }
// ```
// That table would need to be unpacked, but this requires some recursive
// function, which seems not very performance oriented.
//
// My first (quick) attempt:
//let mut output_table = lua.create_table().expect("This should work?");
//let initial_table: mlua::Value = table
// .get("Table")
// .expect("This should exist");
//while let mlua::Value::Table(table) = initial_table {
// for pair in table.pairs() {
// let (key, value) = pair.expect("This should also work?");
// output_table.set(key, value);
// }
//}
},
};
return Ok(real_output);
} else {
unreachable!("Lua serializes these things always in a table");
}
}
} else {
quote! {
return Ok(());
return Ok(mlua::Value::Nil);
}
};
let does_function_expect_output = if output_type.is_some() {
@ -145,7 +204,7 @@ fn get_function_body(field: &Field, has_input: bool, output_type: &Option<Type>)
} else {
quote! {
// We didn't receive output and didn't expect output. Everything went well!
return Ok(());
return Ok(mlua::Value::Nil);
}
};

View File

@ -0,0 +1,189 @@
use std::{collections::HashMap, fmt::Display, thread};
use anyhow::{Context, Result};
use cli_log::{error, info, debug};
use mlua::{Function, Value};
use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
use tokio::{
runtime::Builder,
sync::{mpsc, Mutex},
task::{self, LocalSet},
};
use crate::app::{
command_interface::{add_lua_functions_to_globals, Command},
events::event_types::Event,
};
static LUA: OnceCell<Mutex<mlua::Lua>> = OnceCell::new();
pub type Table = HashMap<String, CommandTransferValue>;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum CommandTransferValue {
/// `nil` or `null` or `undefined`; anything which goes in that group of types.
Nil,
/// `true` or `false`.
Boolean(bool),
// A "light userdata" object, equivalent to a raw pointer.
// /*TODO*/ LightUserData(LightUserData),
/// An integer number.
Integer(i64),
/// A floating point number.
Number(f64),
/// A string
String(String),
/// A table, directory or HashMap
Table(HashMap<String, CommandTransferValue>),
// Reference to a Lua function (or closure).
// /* TODO */ Function(Function),
// Reference to a Lua thread (or coroutine).
// /* TODO */ Thread(Thread<'lua>),
// Reference to a userdata object that holds a custom type which implements `UserData`.
// Special builtin userdata types will be represented as other `Value` variants.
// /* TODO */ UserData(AnyUserData),
// `Error` is a special builtin userdata type. When received from Lua it is implicitly cloned.
// /* TODO */ Error(Error),
}
impl Display for CommandTransferValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CommandTransferValue::Nil => f.write_str("Nil"),
CommandTransferValue::Boolean(bool) => f.write_str(&format!("{}", bool)),
CommandTransferValue::Integer(int) => f.write_str(&format!("{}", int)),
CommandTransferValue::Number(num) => f.write_str(&format!("{}", num)),
CommandTransferValue::String(str) => f.write_str(&format!("{}", str)),
// TODO(@Soispha): The following line should be a real display call, but how do you
// format a HashMap?
CommandTransferValue::Table(table) => f.write_str(&format!("{:#?}", table)),
}
}
}
pub struct LuaCommandManager {
lua_command_tx: mpsc::Sender<String>,
}
impl From<String> for CommandTransferValue {
fn from(s: String) -> Self {
Self::String(s.to_owned())
}
}
impl From<f64> for CommandTransferValue {
fn from(s: f64) -> Self {
Self::Number(s.to_owned())
}
}
impl From<i64> for CommandTransferValue {
fn from(s: i64) -> Self {
Self::Integer(s.to_owned())
}
}
impl From<HashMap<String, CommandTransferValue>> for CommandTransferValue {
fn from(s: HashMap<String, CommandTransferValue>) -> Self {
Self::Table(s.to_owned())
}
}
impl From<bool> for CommandTransferValue {
fn from(s: bool) -> Self {
Self::Boolean(s.to_owned())
}
}
impl From<()> for CommandTransferValue {
fn from(_: ()) -> Self {
Self::Nil
}
}
impl LuaCommandManager {
pub async fn execute_code(&self, code: String) {
self.lua_command_tx
.send(code)
.await
.expect("The receiver should not be dropped at this time");
}
pub fn new(event_call_tx: mpsc::Sender<Event>) -> Self {
info!("Spawning lua code execution thread...");
let (lua_command_tx, mut lua_command_rx) = mpsc::channel::<String>(256);
thread::spawn(move || {
let rt = Builder::new_current_thread().enable_all().build().expect(
"Should always be able to build \
tokio runtime for lua command handling",
);
let local = LocalSet::new();
local.spawn_local(async move {
info!(
"Lua command handling initialized, \
waiting for commands.."
);
while let Some(command) = lua_command_rx.recv().await {
debug!("Recieved lua code: {}", &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 \
by other messages, \
this should never error",
);
});
}
});
rt.block_on(local);
});
LuaCommandManager { lua_command_tx }
}
}
async fn exec_lua_command(command: &str, event_call_tx: mpsc::Sender<Event>) -> Result<()> {
let second_event_call_tx = event_call_tx.clone();
let lua = LUA
.get_or_init(|| {
Mutex::new(add_lua_functions_to_globals(
mlua::Lua::new(),
second_event_call_tx,
))
})
.lock()
.await;
info!("Recieved code to execute: `{}`, executing...", &command);
let output = lua.load(command).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);
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);
event_call_tx
.send(Event::CommandEvent(
Command::RaiseError(err.to_string()),
None,
))
.await?;
}
};
Ok(())
}

View File

@ -1,9 +1,14 @@
// Use `cargo expand app::command_interface` for an overview of the file contents
pub mod lua_command_manger;
pub mod lua_command_manager;
use language_macros::ci_command_enum;
// TODO(@Soispha): Should these paths be moved to the proc macro?
// As they are not static, it could be easier for other people,
// if they stay here
use lua_command_manager::CommandTransferValue;
use mlua::LuaSerdeExt;
use crate::app::Event;
#[ci_command_enum]

View File

@ -1,14 +1,24 @@
use crate::app::{command_interface::Command, events::event_types::EventStatus, App};
use anyhow::{Context, Result};
use cli_log::{info, warn, trace};
use tokio::sync::mpsc;
use std::collections::HashMap;
use anyhow::{Error, Result};
use cli_log::{trace, warn};
use tokio::sync::oneshot;
use crate::app::{
command_interface::{
lua_command_manager::{CommandTransferValue, Table},
Command,
},
events::event_types::EventStatus,
App,
};
pub async fn handle(
app: &mut App<'_>,
command: &Command,
output_callback: &Option<mpsc::Sender<String>>,
output_callback: Option<oneshot::Sender<CommandTransferValue>>,
) -> Result<EventStatus> {
// A command returns both _status output_ (what you would normally print to stderr)
// A command can both return _status output_ (what you would normally print to stderr)
// and _main output_ (the output which is normally printed to stdout).
// We simulate these by returning the main output to the lua function, and printing the
// status output to a status ui field.
@ -36,17 +46,15 @@ pub async fn handle(
($str:expr) => {
if let Some(sender) = output_callback {
sender
.send($str.to_owned())
.await
.context("Failed to send command main output")?;
.send(CommandTransferValue::from($str))
.map_err(|e| Error::msg(format!("Failed to send command main output: `{}`", e)))?;
}
};
($str:expr, $($args:ident),+) => {
if let Some(sender) = output_callback {
sender
.send(format!($str, $($args),+))
.await
.context("Failed to send command main output")?;
.send(CommandTransferValue::from(format!($str, $($args),+)))
.map_err(|e| Error::msg(format!("Failed to send command main output: `{}`", e)))?;
}
};
}

View File

@ -5,10 +5,13 @@ use crate::app::{events::event_types::EventStatus, App};
// This function is here mainly to reserve this spot for further processing of the lua command.
// TODO(@Soispha): Move the lua executor thread code from app to this module
pub async fn handle(app: &mut App<'_>, command: String) -> Result<EventStatus> {
pub async fn handle(
app: &mut App<'_>,
command: String,
) -> Result<EventStatus> {
trace!("Recieved ci command: `{command}`; executing..");
app.lua_command_tx.send(command).await?;
app.lua.execute_code(command).await;
Ok(EventStatus::Ok)
}

View File

@ -1,9 +1,12 @@
use matrix_sdk::deserialized_responses::SyncResponse;
use anyhow::Result;
use matrix_sdk::deserialized_responses::SyncResponse;
use crate::app::{events::event_types::EventStatus, App};
pub async fn handle<'a>(app: &mut App<'a>, sync: &SyncResponse) -> Result<EventStatus> {
pub async fn handle(
app: &mut App<'_>,
sync: &SyncResponse,
) -> Result<EventStatus> {
for (m_room_id, m_room) in sync.rooms.join.iter() {
let room = match app.status.get_room_mut(m_room_id) {
Some(r) => r,

View File

@ -1,9 +1,15 @@
use anyhow::{bail, Context, Result};
use crossterm::event::{Event as CrosstermEvent, KeyCode, KeyEvent};
use crate::{app::{events::event_types::EventStatus, App}, ui::setup};
use crate::{
app::{events::event_types::EventStatus, App},
ui::setup,
};
pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result<EventStatus> {
pub async fn handle(
app: &mut App<'_>,
input_event: &CrosstermEvent,
) -> Result<EventStatus> {
let ui = match &mut app.ui.setup_ui {
Some(ui) => ui,
None => bail!("SetupUI instance not found"),

View File

@ -3,9 +3,9 @@ mod handlers;
use anyhow::{Context, Result};
use cli_log::trace;
use crossterm::event::Event as CrosstermEvent;
use tokio::sync::mpsc::Sender;
use tokio::sync::oneshot;
use crate::app::{command_interface::Command, status::State, App};
use crate::app::{command_interface::{Command, lua_command_manager::CommandTransferValue}, status::State, App};
use self::handlers::{command, lua_command, main, matrix, setup};
@ -15,19 +15,19 @@ use super::EventStatus;
pub enum Event {
InputEvent(CrosstermEvent),
MatrixEvent(matrix_sdk::deserialized_responses::SyncResponse),
CommandEvent(Command, Option<Sender<String>>),
CommandEvent(Command, Option<oneshot::Sender<CommandTransferValue>>),
LuaCommand(String),
}
impl Event {
pub async fn handle(&self, app: &mut App<'_>) -> Result<EventStatus> {
pub async fn handle(self, app: &mut App<'_>) -> Result<EventStatus> {
trace!("Recieved event to handle: `{:#?}`", &self);
match &self {
Event::MatrixEvent(event) => matrix::handle(app, event)
match self {
Event::MatrixEvent(event) => matrix::handle(app, &event)
.await
.with_context(|| format!("Failed to handle matrix event: `{:#?}`", event)),
Event::CommandEvent(event, callback_tx) => command::handle(app, event, callback_tx)
Event::CommandEvent(event, callback_tx) => command::handle(app, &event, callback_tx)
.await
.with_context(|| format!("Failed to handle command event: `{:#?}`", event)),
Event::LuaCommand(lua_code) => lua_command::handle(app, lua_code.to_owned())
@ -38,10 +38,10 @@ impl Event {
State::None => unreachable!(
"This state should not be available, when we are in the input handling"
),
State::Main => main::handle(app, event)
State::Main => main::handle(app, &event)
.await
.with_context(|| format!("Failed to handle input event: `{:#?}`", event)),
State::Setup => setup::handle(app, event)
State::Setup => setup::handle(app, &event)
.await
.with_context(|| format!("Failed to handle input event: `{:#?}`", event)),
},

View File

@ -2,30 +2,24 @@ pub mod command_interface;
pub mod events;
pub mod status;
use std::{path::Path, thread};
use std::path::Path;
use anyhow::{Context, Error, Result};
use cli_log::{error, info};
use cli_log::info;
use matrix_sdk::Client;
use once_cell::sync::OnceCell;
use status::{State, Status};
use tokio::{
runtime::Builder,
sync::{mpsc, Mutex},
task::{self, LocalSet},
};
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use crate::{
accounts::{Account, AccountsManager},
app::{
command_interface::{generate_ci_functions, Command},
events::event_types::Event,
status::{State, Status},
},
ui::{central, setup},
};
use self::events::event_types;
use self::{command_interface::lua_command_manager::LuaCommandManager, events::event_types};
pub struct App<'ui> {
ui: central::UI<'ui>,
@ -38,76 +32,11 @@ pub struct App<'ui> {
input_listener_killer: CancellationToken,
matrix_listener_killer: CancellationToken,
lua_command_tx: mpsc::Sender<String>,
lua: LuaCommandManager,
}
impl App<'_> {
pub fn new() -> Result<Self> {
fn set_up_lua(event_call_tx: mpsc::Sender<Event>) -> mpsc::Sender<String> {
async fn exec_lua_command(
command: &str,
event_call_tx: mpsc::Sender<Event>,
) -> Result<()> {
let second_event_call_tx = event_call_tx.clone();
let lua = LUA
.get_or_init(|| {
Mutex::new(generate_ci_functions(
mlua::Lua::new(),
second_event_call_tx,
))
})
.lock()
.await;
info!("Recieved command to execute: `{}`", &command);
let output = lua
.load(command)
// FIXME this assumes string output only
.eval_async::<String>()
.await;
match output {
Ok(out) => {
info!("Function `{}` returned: `{}`", command, out);
}
Err(err) => {
error!("Function `{}` returned error: `{}`", command, err);
event_call_tx
.send(Event::CommandEvent(
Command::RaiseError(err.to_string()),
None,
))
.await?;
}
};
Ok(())
}
info!("Setting up Lua context..");
static LUA: OnceCell<Mutex<mlua::Lua>> = OnceCell::new();
let (lua_command_tx, mut rx) = mpsc::channel::<String>(256);
thread::spawn(move || {
let rt = Builder::new_current_thread().enable_all().build().expect(
"Should always be able to build tokio runtime for lua command handling",
);
let local = LocalSet::new();
local.spawn_local(async move {
info!("Lua command handling initialized, waiting for commands..");
while let Some(command) = rx.recv().await {
info!("Recieved lua command: {}", &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 by other messages, this should never error");
});
}
});
rt.block_on(local);
});
lua_command_tx
}
let path: &std::path::Path = Path::new("userdata/accounts.json");
let config = if path.exists() {
info!("Reading account config (userdata/accounts.json)");
@ -127,7 +56,7 @@ impl App<'_> {
input_listener_killer: CancellationToken::new(),
matrix_listener_killer: CancellationToken::new(),
lua_command_tx: set_up_lua(tx),
lua: LuaCommandManager::new(tx),
})
}