Fix(app::command_interface): Provide pre-generated file, to check syntax

This commit is contained in:
Benedikt Peetz 2023-07-23 16:35:18 +02:00
parent e792334d21
commit d88cf810a4
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
2 changed files with 110 additions and 47 deletions

View File

@ -0,0 +1,49 @@
// 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(),
/// Send a message to the current room
/// The send message is interpreted literally.
room_message_send: fn(String) -> String,
}

View File

@ -1,49 +1,63 @@
// FIXME: This file needs documentation with examples of how the proc macros work. use cli_log::debug;
// for now use `cargo expand app::command_interface` for an overview
use std::{io::{Error, ErrorKind}, sync::Arc}; #[derive(Debug)]
pub enum Command {
use lua_macros::{ci_command, turn_struct_to_ci_command_enum}; Greet(String),
Exit,
use crate::app::event_types::Event; CommandLineShow,
/// This struct is here to guarantee, that all functions actually end up in the lua context. CommandLineHide,
/// I.e. Rust should throw a compile error, when one field is added, but not a matching function. CyclePlanes,
/// CyclePlanesRev,
/// What it does: RoomMessageSend(String),
/// - Generates a `generate_ci_functions` function, which wraps the specified rust in functions Help(Option<String>),
/// 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.
/// pub fn generate_ci_functions(
/// Every command specified here should have a function named $command_name, where $command_name is the snake cased name of the field. lua: mlua::Lua,
/// tx: tokio::sync::mpsc::Sender<crate::app::events::event_types::Event>,
/// This function is exported to the lua context, thus it's signature must be: ) -> mlua::Lua {
/// ```rust lua.set_app_data(tx);
/// fn $command_name(context: Context, input_string: String) -> Result<$return_type, rlua::Error> {} let globals = lua.globals();
/// ``` let fun_greet = lua.create_async_function(greet).expect(&{
/// where $return_type is the type returned by the function (the only supported ones are right now let res = format!("The function: `{}` should be defined", "greet");
/// `String` and `()`). res
});
#[turn_struct_to_ci_command_enum] globals.set("greet", fun_greet).expect(&{
struct Commands { let res = format!(
/// Greets the user "Setting a static global value ({}, fun_{}) should work",
greet: fn(String) -> String, "greet", "greet",
);
/// Closes the application res
//#[expose(lua)] });
exit: fn(),
drop(globals);
/// Shows the command line lua
command_line_show: fn(), }
/// Hides the command line async fn greet(lua: &mlua::Lua, input: String) -> Result<String, mlua::Error> {
command_line_hide: fn(), let (callback_tx, mut callback_rx) = tokio::sync::mpsc::channel::<String>(256);
let tx: core::cell::Ref<tokio::sync::mpsc::Sender<crate::app::events::event_types::Event>> =
/// Go to the next plane lua.app_data_ref().expect("This exists, it was set before");
cycle_planes: fn(),
/// Go to the previous plane debug!("Got tx");
cycle_planes_rev: fn(), (*tx)
.try_send(crate::app::events::event_types::Event::CommandEvent(
/// Send a message to the current room Command::Greet(input.clone()),
/// The send message is interpreted literally. Some(callback_tx),
room_message_send: fn(String) -> String, ))
.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"),
)));
}
} }