1
0
Fork 0

Fix(treewide): Use the new lua_macros macros

This commit is contained in:
Benedikt Peetz 2023-07-16 14:13:58 +02:00
parent 3e8722433d
commit ba225e29df
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
5 changed files with 52 additions and 39 deletions

View File

@ -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<Event>, command: Command) -> Result<()> {
let event = Event::CommandEvent(command);
channel.send(event).await?;
Ok(())
}

View File

@ -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; use rlua::Context;
// This struct is here to gurantee, that all functions actually end up in the lua context. use super::events::event_types::Event;
// I. e. rust should throw a compile error, when one field is added, but not a matching function.
#[generate_ci_functions()] /// 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> { 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<String, rlua::Error> { #[ci_command]
Ok(format!("Name is {}", name)) fn greet(context: Context, input_str: String) -> Result<String, rlua::Error> {
Ok(format!("Name is {}", input_str))
}
#[ci_command]
fn room_message_send(context: Context, input_str: String) -> Result<String, rlua::Error> {
Ok(format!("Sent message: {}", input_str))
} }

View File

@ -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 anyhow::Result;
use cli_log::info; use cli_log::info;

View File

@ -3,7 +3,7 @@ mod handlers;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use crossterm::event::Event as CrosstermEvent; 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}; use self::handlers::{command, main, matrix, setup};

View File

@ -1,9 +1,8 @@
pub mod command;
pub mod command_interface; pub mod command_interface;
pub mod events; pub mod events;
pub mod status; pub mod status;
use std::path::Path; use std::{path::Path, sync::mpsc::Sender as StdSender};
use accounts::{Account, AccountsManager}; use accounts::{Account, AccountsManager};
use anyhow::{Context, Error, Result}; use anyhow::{Context, Error, Result};
@ -33,11 +32,11 @@ pub struct App<'ui> {
impl App<'_> { impl App<'_> {
pub fn new() -> Result<Self> { pub fn new() -> Result<Self> {
fn set_up_lua() -> Lua { fn set_up_lua(tx: StdSender<Event>) -> Lua {
let lua = Lua::new(); let lua = Lua::new();
lua.context(|mut lua_context| { lua.context(|mut lua_context| {
generate_ci_functions(&mut lua_context); generate_ci_functions(&mut lua_context, tx);
}); });
lua lua
} }
@ -62,7 +61,7 @@ impl App<'_> {
input_listener_killer: CancellationToken::new(), input_listener_killer: CancellationToken::new(),
matrix_listener_killer: CancellationToken::new(), matrix_listener_killer: CancellationToken::new(),
lua: set_up_lua(), lua: set_up_lua(channel_tx),
}) })
} }