From 7489f06a7ce9649a9b852a0701b74f080022b5c9 Mon Sep 17 00:00:00 2001 From: Soispha Date: Wed, 26 Jul 2023 17:19:57 +0200 Subject: [PATCH] Fix(app::command_interface): Use new language_macro api --- Cargo.toml | 4 +- .../app/command_interface.lua_macros.rs | 54 ---------------- src/tui_app/app/command_interface.rs | 64 ------------------- src/tui_app/app/command_interface/mod.rs | 48 ++++++++++++++ 4 files changed, 50 insertions(+), 120 deletions(-) delete mode 100644 src/tui_app/app/command_interface.lua_macros.rs delete mode 100644 src/tui_app/app/command_interface.rs create mode 100644 src/tui_app/app/command_interface/mod.rs diff --git a/Cargo.toml b/Cargo.toml index a60ace5..465b983 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,10 +15,10 @@ clap = { version = "4.3.19", features = ["derive"] } cli-log = "2.0" anyhow = "1.0" matrix-sdk = "0.6" -tokio = { version = "1.29", features = ["macros", "rt-multi-thread", "io-std"] } +tokio = { version = "1.29", features = ["macros", "rt-multi-thread"] } # lua stuff -lua_macros = { path = "./lua_macros" } +language_macros = { path = "./language_macros" } mlua = { version = "0.8.9", features = ["lua54", "async", "send"] } once_cell = "1.18.0" diff --git a/src/tui_app/app/command_interface.lua_macros.rs b/src/tui_app/app/command_interface.lua_macros.rs deleted file mode 100644 index ea1cad7..0000000 --- a/src/tui_app/app/command_interface.lua_macros.rs +++ /dev/null @@ -1,54 +0,0 @@ -// FIXME: This file needs documentation with examples of how the proc macros work. -// for now use `cargo expand app::command_interface` for an overview - -use std::{io::{Error, ErrorKind}, sync::Arc}; - -use lua_macros::{ci_command, turn_struct_to_ci_command_enum}; - -use crate::app::event_types::Event; -/// This struct is here to guarantee, that all functions actually end up in the lua context. -/// I.e. Rust should throw a compile error, when one field is added, but not a matching function. -/// -/// What it does: -/// - Generates a `generate_ci_functions` function, which wraps the specified rust in functions -/// in lua and exports them to the globals in the context provided as argument. -/// - Generates a Commands enum, which contains every Camel cased version of the fields. -/// -/// Every command specified here should have a function named $command_name, where $command_name is the snake cased name of the field. -/// -/// This function is exported to the lua context, thus it's signature must be: -/// ```rust -/// fn $command_name(context: Context, input_string: String) -> Result<$return_type, rlua::Error> {} -/// ``` -/// where $return_type is the type returned by the function (the only supported ones are right now -/// `String` and `()`). - -#[turn_struct_to_ci_command_enum] -struct Commands { - /// Greets the user - greet: fn(String) -> String, - - /// Closes the application - //#[expose(lua)] - exit: fn(), - - /// Shows the command line - command_line_show: fn(), - - /// Hides the command line - command_line_hide: fn(), - - /// Go to the next plane - cycle_planes: fn(), - /// Go to the previous plane - cycle_planes_rev: fn(), - - /// Sets the current app mode to Normal / navigation mode - set_mode_normal: fn(), - /// Sets the current app mode to Insert / editing mode - set_mode_insert: fn(), - - /// Send a message to the current room - /// The send message is interpreted literally. - room_message_send: fn(String) -> String, -} diff --git a/src/tui_app/app/command_interface.rs b/src/tui_app/app/command_interface.rs deleted file mode 100644 index 610c28c..0000000 --- a/src/tui_app/app/command_interface.rs +++ /dev/null @@ -1,64 +0,0 @@ -use cli_log::debug; - -#[derive(Debug)] -pub enum Command { - RaiseError(String), - Greet(String), - Exit, - CommandLineShow, - CommandLineHide, - CyclePlanes, - CyclePlanesRev, - RoomMessageSend(String), - Help(Option), -} - -pub fn generate_ci_functions( - lua: mlua::Lua, - tx: tokio::sync::mpsc::Sender, -) -> mlua::Lua { - lua.set_app_data(tx); - let globals = lua.globals(); - let fun_greet = lua.create_async_function(greet).expect(&{ - let res = format!("The function: `{}` should be defined", "greet"); - res - }); - globals.set("greet", fun_greet).expect(&{ - let res = format!( - "Setting a static global value ({}, fun_{}) should work", - "greet", "greet", - ); - res - }); - - drop(globals); - lua -} - -async fn greet(lua: &mlua::Lua, input: String) -> Result { - let (callback_tx, mut callback_rx) = tokio::sync::mpsc::channel::(256); - let tx: core::cell::Ref> = - lua.app_data_ref().expect("This exists, it was set before"); - - debug!("Got tx"); - (*tx) - .try_send(crate::app::events::event_types::Event::CommandEvent( - Command::Greet(input.clone()), - Some(callback_tx), - )) - .expect("This should work, as the reciever is not dropped"); - - debug!("Sent CommandEvent"); - - debug!("Returning output..."); - if let Some(output) = callback_rx.recv().await { - callback_rx.close(); - debug!("returned output"); - return Ok(output); - } else { - debug!("Not returning output..."); - return Err(mlua::Error::ExternalError(std::sync::Arc::new( - std::io::Error::new(std::io::ErrorKind::Other, "Callback reciever dropped"), - ))); - } -} diff --git a/src/tui_app/app/command_interface/mod.rs b/src/tui_app/app/command_interface/mod.rs new file mode 100644 index 0000000..b3ebb88 --- /dev/null +++ b/src/tui_app/app/command_interface/mod.rs @@ -0,0 +1,48 @@ +// Use `cargo expand app::command_interface` for an overview of the file contents + +pub mod lua_command_manger; + +use language_macros::ci_command_enum; + +use crate::app::Event; + +#[ci_command_enum] +struct Commands { + /// Greets the user + greet: fn(String) -> String, + + /// Closes the application + exit: fn(), + + /// Shows the command line + command_line_show: fn(), + + /// Hides the command line + command_line_hide: fn(), + + /// Go to the next plane + cycle_planes: fn(), + /// Go to the previous plane + cycle_planes_rev: fn(), + + /// Sets the current app mode to Normal / navigation mode + set_mode_normal: fn(), + /// Sets the current app mode to Insert / editing mode + set_mode_insert: fn(), + + /// Send a message to the current room + /// The sent message is interpreted literally. + room_message_send: fn(String), + + /// Open the help pages at the first occurrence of + /// the input string if it is Some, otherwise open + /// the help pages at the start + help: fn(Option), + + /// Send an error to the default error output + raise_error: fn(String), + /// Send output to the default output + /// This is mainly used to display the final + /// output of evaluated lua commands. + display_output: fn(String), +}