Compare commits
No commits in common. "a6d176b6e9420f5fe89fb47cc6dc7593032874c4" and "2a2c173683b4992942cb7dfdf40ca159fd1445c0" have entirely different histories.
a6d176b6e9
...
2a2c173683
|
@ -18,4 +18,4 @@ tokio-util = "0.7"
|
|||
serde = "1.0"
|
||||
cli-log = "2.0"
|
||||
indexmap = "2.0.0"
|
||||
mlua = { version = "0.8.9", features = ["lua54", "async", "send"] }
|
||||
mlua = { version = "0.8.9", features = ["lua54", "async"] }
|
||||
|
|
11
flake.nix
11
flake.nix
|
@ -45,13 +45,10 @@
|
|||
overlays = [(import rust-overlay)];
|
||||
};
|
||||
|
||||
nightly = true;
|
||||
rust =
|
||||
if nightly
|
||||
then pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default)
|
||||
else pkgs.rust-bin.stable.latest.default;
|
||||
#rust-nightly = pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default);
|
||||
rust-stable = pkgs.rust-bin.stable.latest.default;
|
||||
|
||||
craneLib = (crane.mkLib pkgs).overrideToolchain rust;
|
||||
craneLib = (crane.mkLib pkgs).overrideToolchain rust-stable;
|
||||
|
||||
nativeBuildInputs = with pkgs; [
|
||||
pkg-config
|
||||
|
@ -82,7 +79,7 @@
|
|||
statix
|
||||
ltex-ls
|
||||
|
||||
rust
|
||||
rust-stable
|
||||
rust-analyzer
|
||||
cargo-edit
|
||||
cargo-expand
|
||||
|
|
|
@ -3,67 +3,61 @@ mod struct_to_ci_enum;
|
|||
|
||||
use mark_as_ci_command::generate_final_function;
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::{Span, TokenStream as TokenStream2};
|
||||
use quote::{format_ident, quote};
|
||||
use struct_to_ci_enum::{generate_command_enum, generate_generate_ci_function, generate_help_function};
|
||||
use syn::{self, parse_quote, parse_str, DeriveInput, FieldMutability, ItemFn, Token, Visibility};
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::quote;
|
||||
use struct_to_ci_enum::{generate_command_enum, generate_generate_ci_function};
|
||||
use syn::{self, ItemFn, Field, parse::Parser};
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn turn_struct_to_ci_command_enum(_attrs: TokenStream, input: TokenStream) -> TokenStream {
|
||||
pub fn turn_struct_to_ci_commands(_attrs: TokenStream, input: TokenStream) -> TokenStream {
|
||||
// Construct a representation of Rust code as a syntax tree
|
||||
// that we can manipulate
|
||||
let mut input: DeriveInput = syn::parse(input).expect("This should always be valid rust code, as it's extracted from direct code");
|
||||
|
||||
let mut named_fields = match &input.data {
|
||||
syn::Data::Struct(input) => match &input.fields {
|
||||
syn::Fields::Named(named_fields) => named_fields,
|
||||
_ => unimplemented!("The macro only works for named fields (e.g.: `Name: Type`)"),
|
||||
},
|
||||
_ => unimplemented!("The macro only works for structs"),
|
||||
}
|
||||
.to_owned();
|
||||
|
||||
let attr_parsed = parse_quote! {
|
||||
/// This is a help function
|
||||
};
|
||||
|
||||
named_fields.named.push(syn::Field {
|
||||
attrs: vec![attr_parsed],
|
||||
// attrs: attr_parser
|
||||
// .parse("#[doc = r\"This is a help function\"]".to_token_stream().into())
|
||||
// .expect("See reason for other one"),
|
||||
vis: Visibility::Inherited,
|
||||
mutability: FieldMutability::None,
|
||||
ident: Some(format_ident!("help")),
|
||||
colon_token: Some(Token![:](Span::call_site())),
|
||||
ty: parse_str("fn(Option<String>) -> String").expect("This is static and valid rust code"),
|
||||
});
|
||||
|
||||
match &mut input.data {
|
||||
syn::Data::Struct(input) => input.fields = syn::Fields::Named(named_fields.clone()),
|
||||
_ => unreachable!("This was a DataStruct before"),
|
||||
};
|
||||
let input = syn::parse(input)
|
||||
.expect("This should always be valid rust code, as it's extracted from direct code");
|
||||
|
||||
// Build the trait implementation
|
||||
let generate_ci_function: TokenStream2 = generate_generate_ci_function(&input);
|
||||
|
||||
let command_enum = generate_command_enum(&named_fields);
|
||||
|
||||
let help_function = generate_help_function(&named_fields);
|
||||
let command_enum = generate_command_enum(&input);
|
||||
|
||||
quote! {
|
||||
#command_enum
|
||||
|
||||
#generate_ci_function
|
||||
|
||||
//#help_function
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Generate a default lua function implementation.
|
||||
// TODO: Is this needed?
|
||||
#[proc_macro_attribute]
|
||||
pub fn gen_lua_function(_attrs: TokenStream, input: TokenStream) -> TokenStream {
|
||||
// Construct a representation of Rust code as a syntax tree
|
||||
// that we can manipulate
|
||||
let parser = Field::parse_named;
|
||||
let input = parser.parse(input)
|
||||
.expect("This is only defined for named fileds.");
|
||||
|
||||
|
||||
quote! {
|
||||
#input
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Turn a function into a valid ci command function
|
||||
#[proc_macro_attribute]
|
||||
pub fn ci_command(_attrs: TokenStream, input: TokenStream) -> TokenStream {
|
||||
let mut input: ItemFn = syn::parse(input).expect("This should always be valid rust code, as it's extracted from direct code");
|
||||
let output_function = generate_final_function(&mut input);
|
||||
output_function.into()
|
||||
// Construct a representation of Rust code as a syntax tree
|
||||
// that we can manipulate
|
||||
let mut input: ItemFn = syn::parse(input)
|
||||
.expect("This should always be valid rust code, as it's extracted from direct code");
|
||||
|
||||
// Build the trait implementation
|
||||
let output_function: TokenStream2 = generate_final_function(&mut input);
|
||||
|
||||
//panic!("{:#?}", output_function);
|
||||
quote! {
|
||||
#output_function
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
|
|
@ -58,13 +58,18 @@ fn append_tx_send_code(input: &mut syn::ItemFn) -> &mut syn::ItemFn {
|
|||
|
||||
// There should only be two segments (the type is <String, rlua::Error>)
|
||||
if filtered_paths.len() > 2 {
|
||||
unreachable!("There should be no more than two filtered_output, but got: {:#?}", filtered_paths)
|
||||
unreachable!(
|
||||
"There should be no more than two filtered_output, but got: {:#?}",
|
||||
filtered_paths
|
||||
)
|
||||
} else if filtered_paths.len() <= 0 {
|
||||
unreachable!("There should be more than zero filtered_output, but got: {:#?}", filtered_paths)
|
||||
unreachable!(
|
||||
"There should be more than zero filtered_output, but got: {:#?}",
|
||||
filtered_paths
|
||||
)
|
||||
}
|
||||
|
||||
if filtered_paths.len() == 2 {
|
||||
// There is something else than mlua::Error
|
||||
// There is something else than rlua
|
||||
let gen_type = if let GenericArgument::Type(ret_type) =
|
||||
filtered_paths
|
||||
.first()
|
||||
|
@ -92,64 +97,36 @@ fn append_tx_send_code(input: &mut syn::ItemFn) -> &mut syn::ItemFn {
|
|||
}
|
||||
_ => unimplemented!("Only for path types"),
|
||||
};
|
||||
|
||||
let send_data = match return_type {
|
||||
ReturnType::Default => {
|
||||
quote! {
|
||||
{
|
||||
Event::CommandEvent(Command::#function_name_pascal, None)
|
||||
Event::CommandEvent(Command::#function_name_pascal)
|
||||
}
|
||||
}
|
||||
}
|
||||
ReturnType::Type(_, _) => {
|
||||
quote! {
|
||||
{
|
||||
Event::CommandEvent(Command::#function_name_pascal(input.clone()), Some(callback_tx))
|
||||
Event::CommandEvent(Command::#function_name_pascal(input_str.clone()))
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let output_return = match return_type {
|
||||
ReturnType::Default => {
|
||||
quote! {
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
ReturnType::Type(_, _) => {
|
||||
quote! {
|
||||
{
|
||||
if let Some(output) = callback_rx.recv().await {
|
||||
callback_rx.close();
|
||||
return Ok(output);
|
||||
} else {
|
||||
return Err(mlua::Error::ExternalError(Arc::new(Error::new(
|
||||
ErrorKind::Other,
|
||||
"Callback reciever dropped",
|
||||
))));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
quote! {
|
||||
{
|
||||
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>> =
|
||||
core::cell::Ref<
|
||||
tokio::sync::mpsc::Sender<crate::app::events::event_types::Event>
|
||||
> =
|
||||
lua
|
||||
.app_data_ref()
|
||||
.expect("This exists, it was set before");
|
||||
|
||||
(*tx)
|
||||
.try_send(#send_data)
|
||||
.send(#send_data)
|
||||
.await
|
||||
.expect("This should work, as the reciever is not dropped");
|
||||
|
||||
#output_return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -157,7 +134,6 @@ fn append_tx_send_code(input: &mut syn::ItemFn) -> &mut syn::ItemFn {
|
|||
|
||||
let tx_send_block: Block =
|
||||
syn::parse(tx_send.into()).expect("This is a static string, it will always parse");
|
||||
|
||||
let tx_send_expr_block = ExprBlock {
|
||||
attrs: vec![],
|
||||
label: None,
|
||||
|
|
|
@ -1,13 +1,63 @@
|
|||
use convert_case::{Case, Casing};
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::{format_ident, quote};
|
||||
use syn::{parse_quote, ReturnType, Type};
|
||||
use syn::{self, ReturnType};
|
||||
|
||||
fn generate_ci_function_exposure(field: &syn::Field) -> TokenStream2 {
|
||||
pub fn generate_generate_ci_function(input: &syn::DeriveInput) -> TokenStream2 {
|
||||
let mut functions_to_generate: Vec<TokenStream2> = vec![];
|
||||
let input_tokens: TokenStream2 = match &input.data {
|
||||
syn::Data::Struct(input) => match &input.fields {
|
||||
syn::Fields::Named(named_fields) => named_fields
|
||||
.named
|
||||
.iter()
|
||||
.map(|field| -> TokenStream2 {
|
||||
if field.attrs.iter().any(|attribute| {
|
||||
attribute.path()
|
||||
== &syn::parse_str::<syn::Path>("gen_default_lua_function")
|
||||
.expect("This is valid rust code")
|
||||
}) {
|
||||
let function_name = field
|
||||
.ident
|
||||
.as_ref()
|
||||
.expect("These are only the named field, thus they all should have a name.");
|
||||
functions_to_generate.push(quote! {
|
||||
#[ci_command]
|
||||
async fn #function_name(lua: &mlua::Lua, input_str: String) -> Result<(), mlua::Error> {
|
||||
Ok(())
|
||||
}
|
||||
});
|
||||
generate_ci_part(field)
|
||||
} else {
|
||||
generate_ci_part(field)
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
|
||||
_ => unimplemented!("Only implemented for named fileds"),
|
||||
},
|
||||
_ => unimplemented!("Only implemented for structs"),
|
||||
};
|
||||
|
||||
let functions_to_generate: TokenStream2 = functions_to_generate.into_iter().collect();
|
||||
let gen = quote! {
|
||||
pub fn generate_ci_functions(
|
||||
lua: &mut mlua::Lua,
|
||||
tx: tokio::sync::mpsc::Sender<crate::app::events::event_types::Event>)
|
||||
{
|
||||
lua.set_app_data(tx);
|
||||
let globals = lua.globals();
|
||||
#input_tokens
|
||||
}
|
||||
#functions_to_generate
|
||||
};
|
||||
gen.into()
|
||||
}
|
||||
|
||||
fn generate_ci_part(field: &syn::Field) -> TokenStream2 {
|
||||
let field_ident = field
|
||||
.ident
|
||||
.as_ref()
|
||||
.expect("These are only the named field, thus they all should have a name.");
|
||||
|
||||
let function_name_ident = format_ident!("fun_{}", field_ident);
|
||||
let function_name = format!("{}", field_ident);
|
||||
quote! {
|
||||
|
@ -29,77 +79,62 @@ fn generate_ci_function_exposure(field: &syn::Field) -> TokenStream2 {
|
|||
.into()
|
||||
}
|
||||
|
||||
pub fn generate_generate_ci_function(input: &syn::DeriveInput) -> TokenStream2 {
|
||||
let mut functions_to_generate: Vec<TokenStream2> = vec![];
|
||||
|
||||
let functions_to_export_in_lua: TokenStream2 = match &input.data {
|
||||
pub fn generate_command_enum(input: &syn::DeriveInput) -> TokenStream2 {
|
||||
let input_tokens: TokenStream2 = match &input.data {
|
||||
syn::Data::Struct(input) => match &input.fields {
|
||||
syn::Fields::Named(named_fields) => named_fields
|
||||
.named
|
||||
.iter()
|
||||
.map(|field| -> TokenStream2 {
|
||||
let input_type = match &field.ty {
|
||||
syn::Type::BareFn(bare_fn) => {
|
||||
if bare_fn.inputs.len() == 1 {
|
||||
bare_fn.inputs.last().expect("The last element exists").ty.clone()
|
||||
} else if bare_fn.inputs.len() == 0 {
|
||||
let input_type: Type = parse_quote! {()};
|
||||
input_type
|
||||
} else {
|
||||
panic!("The Function can only take on argument, or none");
|
||||
}
|
||||
}
|
||||
_ => unimplemented!("This is only implemented for bare function types"),
|
||||
};
|
||||
let return_type = match &field.ty {
|
||||
syn::Type::BareFn(function) => {
|
||||
let return_path: &ReturnType = &function.output;
|
||||
match return_path {
|
||||
ReturnType::Default => None,
|
||||
ReturnType::Type(_, return_type) => Some(return_type.to_owned()) }
|
||||
}
|
||||
_ => unimplemented!("This is only implemented for bare function types"),
|
||||
};
|
||||
|
||||
let function_name = field
|
||||
let field_ident = field
|
||||
.ident
|
||||
.as_ref()
|
||||
.expect("These are only the named field, thus they all should have a name.");
|
||||
|
||||
if let Some(ret_type) = return_type {
|
||||
functions_to_generate.push(quote! {
|
||||
#[ci_command]
|
||||
async fn #function_name(lua: &mlua::Lua, input: #input_type) -> Result<#ret_type, mlua::Error> {
|
||||
let enum_variant_type = match &field.ty {
|
||||
syn::Type::BareFn(function) => {
|
||||
let return_path: &ReturnType = &function.output;
|
||||
match return_path {
|
||||
ReturnType::Default => None,
|
||||
ReturnType::Type(_, return_type) => Some(match *(return_type.to_owned()) {
|
||||
syn::Type::Path(path_type) => path_type
|
||||
.path
|
||||
.get_ident()
|
||||
.expect("A path should either be complete, or only conain one segment")
|
||||
.to_owned(),
|
||||
_ => unimplemented!("This is only implemented for path types"),
|
||||
}),
|
||||
}
|
||||
}
|
||||
});
|
||||
_ => unimplemented!("This is only implemented for bare function types"),
|
||||
};
|
||||
|
||||
let enum_variant_name = format_ident!(
|
||||
"{}",
|
||||
field_ident.to_string().from_case(Case::Snake).to_case(Case::Pascal)
|
||||
);
|
||||
if enum_variant_type.is_some() {
|
||||
quote! {
|
||||
#enum_variant_name (#enum_variant_type),
|
||||
}
|
||||
.into()
|
||||
} else {
|
||||
functions_to_generate.push(quote! {
|
||||
#[ci_command]
|
||||
async fn #function_name(lua: &mlua::Lua, input: #input_type) -> Result<(), mlua::Error> {
|
||||
quote! {
|
||||
#enum_variant_name,
|
||||
}
|
||||
});
|
||||
}
|
||||
generate_ci_function_exposure(field)
|
||||
})
|
||||
.collect(),
|
||||
|
||||
_ => unimplemented!("Only implemented for named fileds"),
|
||||
},
|
||||
_ => unimplemented!("Only implemented for structs"),
|
||||
};
|
||||
|
||||
let functions_to_generate: TokenStream2 = functions_to_generate.into_iter().collect();
|
||||
let gen = quote! {
|
||||
pub fn generate_ci_functions(
|
||||
lua: &mut mlua::Lua,
|
||||
tx: tokio::sync::mpsc::Sender<crate::app::events::event_types::Event>
|
||||
)
|
||||
{
|
||||
lua.set_app_data(tx);
|
||||
let globals = lua.globals();
|
||||
#functions_to_export_in_lua
|
||||
#[derive(Debug)]
|
||||
pub enum Command {
|
||||
#input_tokens
|
||||
}
|
||||
#functions_to_generate
|
||||
};
|
||||
gen.into()
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
use convert_case::{Case, Casing};
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::{format_ident, quote};
|
||||
use syn::Type;
|
||||
|
||||
pub fn generate_command_enum(input: &syn::FieldsNamed) -> TokenStream2 {
|
||||
let input_tokens: TokenStream2 = input
|
||||
.named
|
||||
.iter()
|
||||
.map(|field| -> TokenStream2 {
|
||||
let field_ident = field
|
||||
.ident
|
||||
.as_ref()
|
||||
.expect("These are only the named fields, thus they should all have a ident.");
|
||||
|
||||
let enum_variant_type = match &field.ty {
|
||||
syn::Type::BareFn(function) => {
|
||||
let return_path = &function.inputs;
|
||||
|
||||
let input_type: Option<Type> = if return_path.len() == 1 {
|
||||
Some(
|
||||
return_path
|
||||
.last()
|
||||
.expect("The last element exists")
|
||||
.ty
|
||||
.clone(),
|
||||
)
|
||||
} else if return_path.len() == 0 {
|
||||
None
|
||||
} else {
|
||||
panic!("The Function can only take on argument, or none");
|
||||
};
|
||||
input_type
|
||||
}
|
||||
_ => unimplemented!("This is only implemented for bare function types"),
|
||||
};
|
||||
|
||||
let enum_variant_name = format_ident!(
|
||||
"{}",
|
||||
field_ident
|
||||
.to_string()
|
||||
.from_case(Case::Snake)
|
||||
.to_case(Case::Pascal)
|
||||
);
|
||||
if enum_variant_type.is_some() {
|
||||
quote! {
|
||||
#enum_variant_name (#enum_variant_type),
|
||||
}
|
||||
.into()
|
||||
} else {
|
||||
quote! {
|
||||
#enum_variant_name,
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let gen = quote! {
|
||||
#[derive(Debug)]
|
||||
pub enum Command {
|
||||
#input_tokens
|
||||
}
|
||||
};
|
||||
gen.into()
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::{quote, ToTokens};
|
||||
|
||||
pub fn generate_help_function(input: &syn::FieldsNamed) -> TokenStream2 {
|
||||
let input: Vec<_> = input.named.iter().collect();
|
||||
|
||||
let combined_help_text: TokenStream2 = input
|
||||
.iter()
|
||||
.map(|field| {
|
||||
let attrs_with_doc: Vec<TokenStream2> = field
|
||||
.attrs
|
||||
.iter()
|
||||
.filter_map(|attr| {
|
||||
if attr.path().is_ident("doc") {
|
||||
let help_text = attr
|
||||
.meta
|
||||
.require_name_value()
|
||||
.expect("This is a named value type, because all doc comments work this way")
|
||||
.value
|
||||
.clone();
|
||||
Some(help_text.into_token_stream().into())
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
if attrs_with_doc.len() == 0 {
|
||||
// TODO there should be a better panic function, than the generic one
|
||||
panic!(
|
||||
"The command named: `{}`, does not provide a help message",
|
||||
field.ident.as_ref().expect("These are all named")
|
||||
);
|
||||
} else {
|
||||
let help_text_for_one_command_combined: TokenStream2 = attrs_with_doc.into_iter().collect();
|
||||
return help_text_for_one_command_combined;
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
quote! {
|
||||
#[ci_command]
|
||||
async fn help(
|
||||
lua: &mlua::Lua,
|
||||
input_str: Option<String>
|
||||
) -> Result<String, mlua::Error> {
|
||||
// TODO add a way to filter the help based on the input
|
||||
|
||||
let output = "These functions exist:\n";
|
||||
output.push_str(#combined_help_text);
|
||||
Ok(output)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
pub mod generate_command_enum;
|
||||
pub mod generate_generate_ci_function;
|
||||
pub mod generate_help_function;
|
||||
|
||||
pub use generate_command_enum::*;
|
||||
pub use generate_generate_ci_function::*;
|
||||
pub use generate_help_function::*;
|
|
@ -1,11 +1,9 @@
|
|||
// 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 lua_macros::{ci_command, turn_struct_to_ci_commands};
|
||||
|
||||
use std::{io::{Error, ErrorKind}, sync::Arc};
|
||||
use super::events::event_types::Event;
|
||||
|
||||
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.
|
||||
///
|
||||
|
@ -22,28 +20,41 @@ use crate::app::event_types::Event;
|
|||
/// ```
|
||||
/// 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]
|
||||
#[turn_struct_to_ci_commands]
|
||||
struct Commands {
|
||||
/// Greets the user
|
||||
greet: fn(String) -> String,
|
||||
greet: fn(usize) -> String,
|
||||
|
||||
/// Closes the application
|
||||
//#[expose(lua)]
|
||||
#[gen_default_lua_function]
|
||||
exit: fn(),
|
||||
|
||||
/// Shows the command line
|
||||
#[gen_default_lua_function]
|
||||
command_line_show: fn(),
|
||||
|
||||
/// Hides the command line
|
||||
#[gen_default_lua_function]
|
||||
command_line_hide: fn(),
|
||||
|
||||
/// Go to the next plane
|
||||
#[gen_default_lua_function]
|
||||
cycle_planes: fn(),
|
||||
/// Go to the previous plane
|
||||
#[gen_default_lua_function]
|
||||
cycle_planes_rev: fn(),
|
||||
|
||||
/// Send a message to the current room
|
||||
/// The send message is interpreted literally.
|
||||
room_message_send: fn(String) -> String,
|
||||
}
|
||||
|
||||
#[ci_command]
|
||||
async fn greet(lua: &mlua::Lua, input_str: String) -> Result<String, mlua::Error> {
|
||||
Ok(format!("Name is {}", input_str))
|
||||
}
|
||||
|
||||
#[ci_command]
|
||||
async fn room_message_send(lua: &mlua::Lua, input_str: String) -> Result<String, mlua::Error> {
|
||||
Ok(format!("Sent message: {}", input_str))
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
use anyhow::Result;
|
||||
use cli_log::info;
|
||||
|
||||
use crate::app::{events::event_types::EventStatus, App};
|
||||
|
||||
pub async fn handle(app: &mut App<'_>, output: &String) -> Result<EventStatus> {
|
||||
info!("Recieved command output: `{}`", output);
|
||||
app.ui.set_command_output(output);
|
||||
|
||||
Ok(EventStatus::Ok)
|
||||
}
|
|
@ -2,65 +2,39 @@ use crate::app::{command_interface::Command, events::event_types::EventStatus, A
|
|||
use anyhow::Result;
|
||||
use cli_log::info;
|
||||
|
||||
pub async fn handle(
|
||||
app: &mut App<'_>,
|
||||
command: &Command,
|
||||
send_output: bool,
|
||||
) -> Result<(EventStatus, String)> {
|
||||
macro_rules! set_status_output {
|
||||
($str:expr) => {
|
||||
if send_output {
|
||||
app.ui.set_command_output($str);
|
||||
}
|
||||
};
|
||||
($str:expr, $($args:ident),+) => {
|
||||
if send_output {
|
||||
app.ui.set_command_output(&format!($str, $($args),+));
|
||||
}
|
||||
};
|
||||
}
|
||||
pub async fn handle(app: &mut App<'_>, command: &Command) -> Result<EventStatus> {
|
||||
info!("Handling command: {:#?}", command);
|
||||
|
||||
Ok(match command {
|
||||
Command::Exit => (
|
||||
EventStatus::Terminate,
|
||||
"Terminated the application".to_owned(),
|
||||
),
|
||||
Command::Exit => EventStatus::Terminate,
|
||||
|
||||
Command::CommandLineShow => {
|
||||
app.ui.cli_enable();
|
||||
set_status_output!("CLI online");
|
||||
(EventStatus::Ok, "".to_owned())
|
||||
EventStatus::Ok
|
||||
}
|
||||
Command::CommandLineHide => {
|
||||
app.ui.cli_disable();
|
||||
set_status_output!("CLI offline");
|
||||
(EventStatus::Ok, "".to_owned())
|
||||
EventStatus::Ok
|
||||
}
|
||||
|
||||
Command::CyclePlanes => {
|
||||
app.ui.cycle_main_input_position();
|
||||
set_status_output!("Switched main input position");
|
||||
(EventStatus::Ok, "".to_owned())
|
||||
EventStatus::Ok
|
||||
}
|
||||
Command::CyclePlanesRev => {
|
||||
app.ui.cycle_main_input_position_rev();
|
||||
set_status_output!("Switched main input position; reversed");
|
||||
(EventStatus::Ok, "".to_owned())
|
||||
EventStatus::Ok
|
||||
}
|
||||
|
||||
Command::RoomMessageSend(msg) => {
|
||||
if let Some(room) = app.status.room_mut() {
|
||||
room.send(msg.clone()).await?;
|
||||
}
|
||||
set_status_output!("Send message: `{}`", msg);
|
||||
(EventStatus::Ok, "".to_owned())
|
||||
EventStatus::Ok
|
||||
}
|
||||
Command::Greet(name) => {
|
||||
info!("Greated {}", name);
|
||||
set_status_output!("Hi, {}!", name);
|
||||
(EventStatus::Ok, "".to_owned())
|
||||
EventStatus::Ok
|
||||
}
|
||||
Command::Help(_) => todo!(),
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,38 +1,24 @@
|
|||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use cli_log::{debug, info};
|
||||
use tokio::{task, time::timeout};
|
||||
use cli_log::info;
|
||||
|
||||
use crate::app::{events::event_types::EventStatus, App};
|
||||
use crate::app::{
|
||||
events::event_types::{Event, EventStatus},
|
||||
App,
|
||||
};
|
||||
|
||||
pub async fn handle(app: &mut App<'_>, command: String) -> Result<EventStatus> {
|
||||
pub async fn handle(app: &mut App<'_>, command: &str) -> Result<EventStatus> {
|
||||
info!("Recieved ci command: `{command}`; executing..");
|
||||
|
||||
let local = task::LocalSet::new();
|
||||
|
||||
// Run the local task set.
|
||||
let output = local
|
||||
.run_until(async move {
|
||||
let lua = Arc::clone(&app.lua);
|
||||
debug!("before_handle");
|
||||
let c_handle = task::spawn_local(async move {
|
||||
lua.load(&command)
|
||||
// FIXME this assumes string output only
|
||||
.eval_async::<String>()
|
||||
.await
|
||||
.with_context(|| format!("Failed to execute: `{command}`"))
|
||||
});
|
||||
debug!("after_handle");
|
||||
c_handle
|
||||
})
|
||||
.await;
|
||||
debug!("after_thread");
|
||||
|
||||
let output = timeout(Duration::from_secs(10), output)
|
||||
// TODO: Should the ci support more than strings?
|
||||
let output = app
|
||||
.lua
|
||||
.load(command)
|
||||
.eval_async::<String>()
|
||||
.await
|
||||
.context("Failed to join lua command executor")???;
|
||||
info!("Command returned: `{}`", output);
|
||||
.with_context(|| format!("Failed to execute: `{command}`"))?;
|
||||
info!("Function evaluated to: `{output}`");
|
||||
|
||||
app.tx.send(Event::CiOutput(output)).await.context("Failed to send ci output to internal event stream")?;
|
||||
|
||||
Ok(EventStatus::Ok)
|
||||
}
|
||||
|
|
|
@ -16,14 +16,14 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result<E
|
|||
code: KeyCode::Esc, ..
|
||||
}) => {
|
||||
app.tx
|
||||
.send(Event::CommandEvent(Command::Exit, None))
|
||||
.send(Event::CommandEvent(Command::Exit))
|
||||
.await?;
|
||||
}
|
||||
CrosstermEvent::Key(KeyEvent {
|
||||
code: KeyCode::Tab, ..
|
||||
}) => {
|
||||
app.tx
|
||||
.send(Event::CommandEvent(Command::CyclePlanes, None))
|
||||
.send(Event::CommandEvent(Command::CyclePlanes))
|
||||
.await?;
|
||||
}
|
||||
CrosstermEvent::Key(KeyEvent {
|
||||
|
@ -31,7 +31,7 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result<E
|
|||
..
|
||||
}) => {
|
||||
app.tx
|
||||
.send(Event::CommandEvent(Command::CyclePlanesRev, None))
|
||||
.send(Event::CommandEvent(Command::CyclePlanesRev))
|
||||
.await?;
|
||||
}
|
||||
CrosstermEvent::Key(KeyEvent {
|
||||
|
@ -40,7 +40,7 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result<E
|
|||
..
|
||||
}) => {
|
||||
app.tx
|
||||
.send(Event::CommandEvent(Command::CommandLineShow, None))
|
||||
.send(Event::CommandEvent(Command::CommandLineShow))
|
||||
.await?;
|
||||
}
|
||||
input => match app.ui.input_position() {
|
||||
|
@ -52,10 +52,9 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result<E
|
|||
..
|
||||
}) => {
|
||||
app.tx
|
||||
.send(Event::CommandEvent(
|
||||
Command::RoomMessageSend(app.ui.message_compose.lines().join("\n")),
|
||||
None,
|
||||
))
|
||||
.send(Event::CommandEvent(Command::RoomMessageSend(
|
||||
app.ui.message_compose.lines().join("\n"),
|
||||
)))
|
||||
.await?;
|
||||
app.ui.message_compose_clear();
|
||||
}
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
mod handlers;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use cli_log::{info, trace};
|
||||
use crossterm::event::Event as CrosstermEvent;
|
||||
use tokio::sync::mpsc::Sender;
|
||||
|
||||
use crate::app::{command_interface::Command, status::State, App};
|
||||
|
||||
use self::handlers::{command, lua_command, main, matrix, setup};
|
||||
use self::handlers::{ci_output, command, lua_command, main, matrix, setup};
|
||||
|
||||
use super::EventStatus;
|
||||
|
||||
|
@ -15,39 +13,32 @@ use super::EventStatus;
|
|||
pub enum Event {
|
||||
InputEvent(CrosstermEvent),
|
||||
MatrixEvent(matrix_sdk::deserialized_responses::SyncResponse),
|
||||
CommandEvent(Command, Option<Sender<String>>),
|
||||
CommandEvent(Command),
|
||||
CiOutput(String),
|
||||
LuaCommand(String),
|
||||
}
|
||||
|
||||
impl Event {
|
||||
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)
|
||||
.await
|
||||
.with_context(|| format!("Failed to handle matrix event: `{:#?}`", event)),
|
||||
|
||||
Event::CommandEvent(event, callback_tx) => {
|
||||
let (result, output) = command::handle(app, event, callback_tx.is_some())
|
||||
.await
|
||||
.with_context(|| format!("Failed to handle command event: `{:#?}`", event))?;
|
||||
|
||||
if let Some(callback_tx) = callback_tx {
|
||||
callback_tx
|
||||
.send(output.clone())
|
||||
.await
|
||||
.with_context(|| format!("Failed to send command output: {}", output))?;
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
Event::LuaCommand(lua_code) => lua_command::handle(app, lua_code.to_owned())
|
||||
Event::CommandEvent(event) => command::handle(app, event)
|
||||
.await
|
||||
.with_context(|| format!("Failed to handle lua code: `{:#?}`", lua_code)),
|
||||
.with_context(|| format!("Failed to handle command event: `{:#?}`", event)),
|
||||
Event::CiOutput(output) => ci_output::handle(app, output).await.with_context(|| {
|
||||
format!("Failed to handle command interface output: `{:#?}`", output)
|
||||
}),
|
||||
Event::LuaCommand(lua_code) => {
|
||||
lua_command::handle(app, lua_code).await.with_context(|| {
|
||||
format!("Failed to handle lua code: `{:#?}`", lua_code)
|
||||
})
|
||||
}
|
||||
|
||||
Event::InputEvent(event) => match app.status.state() {
|
||||
State::None => unreachable!(
|
||||
"This state should not be available, when we are in the input handling"
|
||||
),
|
||||
State::None => Ok(EventStatus::Ok),
|
||||
State::Main => main::handle(app, event)
|
||||
.await
|
||||
.with_context(|| format!("Failed to handle input event: `{:#?}`", event)),
|
||||
|
|
|
@ -2,9 +2,9 @@ pub mod command_interface;
|
|||
pub mod events;
|
||||
pub mod status;
|
||||
|
||||
use std::{path::Path, sync::Arc};
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::{Context, Error, Result};
|
||||
use anyhow::{Error, Result, Context};
|
||||
use cli_log::info;
|
||||
use matrix_sdk::Client;
|
||||
use mlua::Lua;
|
||||
|
@ -31,16 +31,16 @@ pub struct App<'ui> {
|
|||
input_listener_killer: CancellationToken,
|
||||
matrix_listener_killer: CancellationToken,
|
||||
|
||||
lua: Arc<Lua>,
|
||||
lua: Lua,
|
||||
}
|
||||
|
||||
impl App<'_> {
|
||||
pub fn new() -> Result<Self> {
|
||||
fn set_up_lua(tx: mpsc::Sender<Event>) -> Arc<Lua> {
|
||||
fn set_up_lua(tx: mpsc::Sender<Event>) -> Lua {
|
||||
let mut lua = Lua::new();
|
||||
|
||||
generate_ci_functions(&mut lua, tx);
|
||||
Arc::new(lua)
|
||||
lua
|
||||
}
|
||||
|
||||
let path: &std::path::Path = Path::new("userdata/accounts.json");
|
||||
|
|
Reference in New Issue