From ba225e29df778ac8085a33004a2f5d234e88cee1 Mon Sep 17 00:00:00 2001 From: Soispha Date: Sun, 16 Jul 2023 14:13:58 +0200 Subject: [PATCH] Fix(treewide): Use the new lua_macros macros --- src/app/command.rs | 25 --------- src/app/command_interface.rs | 53 ++++++++++++++++--- .../event_types/event/handlers/command.rs | 2 +- src/app/events/event_types/event/mod.rs | 2 +- src/app/mod.rs | 9 ++-- 5 files changed, 52 insertions(+), 39 deletions(-) delete mode 100644 src/app/command.rs diff --git a/src/app/command.rs b/src/app/command.rs deleted file mode 100644 index 6342bf9..0000000 --- a/src/app/command.rs +++ /dev/null @@ -1,25 +0,0 @@ -use anyhow::Result; -use tokio::sync::mpsc; - -use super::events::event_types::Event; - -#[derive(Debug, Clone)] -pub enum Command { - // Closes the application - Exit, - - CommandLineShow, - CommandLineHide, - - CyclePlanes, - CyclePlanesRev, - - // sends a message to the current room - RoomMessageSend(String), -} - -pub async fn execute(channel: &mpsc::Sender, command: Command) -> Result<()> { - let event = Event::CommandEvent(command); - channel.send(event).await?; - Ok(()) -} diff --git a/src/app/command_interface.rs b/src/app/command_interface.rs index e38e91e..87f184d 100644 --- a/src/app/command_interface.rs +++ b/src/app/command_interface.rs @@ -1,13 +1,52 @@ -use lua_macros::generate_ci_functions; +// FIXME: This file needs documentation with examples of how the proc macros work. +use lua_macros::{ci_command, turn_struct_to_ci_commands}; use rlua::Context; -// This struct is here to gurantee, 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. -#[generate_ci_functions()] +use super::events::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_commands] struct Commands<'lua> { - greet: Function<'lua>, + greet: fn(usize) -> String, + + // Closes the application + #[gen_default_lua_function] + exit: fn(), + #[gen_default_lua_function] + command_line_show: fn(), + #[gen_default_lua_function] + command_line_hide: fn(), + + #[gen_default_lua_function] + cycle_planes: fn(), + #[gen_default_lua_function] + cycle_planes_rev: fn(), + + //// sends a message to the current room + room_message_send: fn(String) -> String, } -fn greet(context: Context, name: String) -> Result { - Ok(format!("Name is {}", name)) +#[ci_command] +fn greet(context: Context, input_str: String) -> Result { + Ok(format!("Name is {}", input_str)) +} + +#[ci_command] +fn room_message_send(context: Context, input_str: String) -> Result { + Ok(format!("Sent message: {}", input_str)) } diff --git a/src/app/events/event_types/event/handlers/command.rs b/src/app/events/event_types/event/handlers/command.rs index 0c67ed2..364ad23 100644 --- a/src/app/events/event_types/event/handlers/command.rs +++ b/src/app/events/event_types/event/handlers/command.rs @@ -1,4 +1,4 @@ -use crate::app::{command::Command, events::event_types::EventStatus, App}; +use crate::app::{events::event_types::EventStatus, App, command_interface::Command}; use anyhow::Result; use cli_log::info; diff --git a/src/app/events/event_types/event/mod.rs b/src/app/events/event_types/event/mod.rs index ffdad06..b37d452 100644 --- a/src/app/events/event_types/event/mod.rs +++ b/src/app/events/event_types/event/mod.rs @@ -3,7 +3,7 @@ mod handlers; use anyhow::{Context, Result}; use crossterm::event::Event as CrosstermEvent; -use crate::app::{command::Command, status::State, App}; +use crate::app::{status::State, App, command_interface::Command}; use self::handlers::{command, main, matrix, setup}; diff --git a/src/app/mod.rs b/src/app/mod.rs index 785fa8a..3b21e9d 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1,9 +1,8 @@ -pub mod command; pub mod command_interface; pub mod events; pub mod status; -use std::path::Path; +use std::{path::Path, sync::mpsc::Sender as StdSender}; use accounts::{Account, AccountsManager}; use anyhow::{Context, Error, Result}; @@ -33,11 +32,11 @@ pub struct App<'ui> { impl App<'_> { pub fn new() -> Result { - fn set_up_lua() -> Lua { + fn set_up_lua(tx: StdSender) -> Lua { let lua = Lua::new(); lua.context(|mut lua_context| { - generate_ci_functions(&mut lua_context); + generate_ci_functions(&mut lua_context, tx); }); lua } @@ -62,7 +61,7 @@ impl App<'_> { input_listener_killer: CancellationToken::new(), matrix_listener_killer: CancellationToken::new(), - lua: set_up_lua(), + lua: set_up_lua(channel_tx), }) }