Compare commits

...

4 Commits

14 changed files with 542 additions and 121 deletions

View File

@ -4,9 +4,10 @@ version = "0.1.0"
edition = "2021"
[lib]
crate_type = ["proc-macro"]
proc-macro = true
[dependencies]
convert_case = "0.6.0"
proc-macro2 = "1.0.64"
quote = "1.0.29"
syn = "2.0.25"
syn = { version = "2.0.25", features = ["extra-traits", "full", "parsing"] }

View File

@ -0,0 +1,11 @@
use proc_macro2::TokenStream as TokenStream2;
use quote::ToTokens;
// TODO: Do we need this noop?
pub fn generate_default_lua_function(input: &syn::Field) -> TokenStream2 {
let output: TokenStream2 = syn::parse(input.into_token_stream().into())
.expect("This is generated from valid rust code, it should stay that way.");
output
}

View File

@ -1,59 +1,68 @@
mod generate_noop_lua_function;
mod mark_as_ci_command;
mod struct_to_ci_enum;
use generate_noop_lua_function::generate_default_lua_function;
use mark_as_ci_command::generate_final_function;
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use syn;
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 generate_ci_functions(_: 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 input = syn::parse(input)
.expect("This should always be valid rust code, as it's extracted from direct code");
// Build the trait implementation
generate_generate_ci_functions(&input)
let generate_ci_function: TokenStream2 = generate_generate_ci_function(&input);
let command_enum = generate_command_enum(&input);
quote! {
#command_enum
#generate_ci_function
}
.into()
}
fn generate_generate_ci_functions(input: &syn::DeriveInput) -> TokenStream {
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 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! {
let #function_name_ident = context.create_function(#field_ident).expect(
&format!(
"The function: `{}` should be defined",
#function_name
)
);
globals.set(#function_name, #function_name_ident).expect(
&format!(
"Setting a static global value ({}, fun_{}) should work",
#function_name,
#function_name
)
);
}
.into()
})
.collect(),
_ => unimplemented!("Only implemented for named fileds"),
},
_ => unimplemented!("Only for implemented for structs"),
};
/// Generate a default lua function implementation.
#[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.");
let gen = quote! {
pub fn generate_ci_functions(context: &mut Context) {
let globals = context.globals();
#input_tokens
}
};
gen.into()
// Build the trait implementation
let default_lua_function: TokenStream2 = generate_default_lua_function(&input);
quote! {
#default_lua_function
}
.into()
}
/// Turn a function into a valid ci command function
#[proc_macro_attribute]
pub fn ci_command(_attrs: TokenStream, input: TokenStream) -> TokenStream {
// 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()
}

View File

@ -0,0 +1,161 @@
use convert_case::{Case, Casing};
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote, ToTokens};
use syn::{Block, Expr, ExprBlock, GenericArgument, ReturnType, Stmt, Type};
pub fn generate_final_function(input: &mut syn::ItemFn) -> TokenStream2 {
append_tx_send_code(input);
let output: TokenStream2 = syn::parse(input.into_token_stream().into())
.expect("This is generated from valid rust code, it should stay that way.");
output
}
fn append_tx_send_code(input: &mut syn::ItemFn) -> &mut syn::ItemFn {
let function_name_pascal = format_ident!(
"{}",
input
.sig
.ident
.clone()
.to_string()
.from_case(Case::Snake)
.to_case(Case::Pascal)
);
let tx_send = match &input.sig.output {
syn::ReturnType::Default => {
todo!(
"Does this case even trigger? All functions should have a output of (Result<$type, rlua::Error>)"
);
quote! {
{
let tx: std::sync::mpsc::Sender<crate::app::events::event_types::Event> =
context
.named_registry_value("sender_for_ci_commands")
.expect("This exists, it was set before");
tx
.send(Event::CommandEvent(Command::#function_name_pascal))
.expect("This should work, as the reciever is not dropped");
}
}
}
syn::ReturnType::Type(_, ret_type) => {
let return_type = match *(ret_type.clone()) {
syn::Type::Path(path) => {
match path
.path
.segments
.first()
.expect("This is expected to be only one path segment")
.arguments
.to_owned()
{
syn::PathArguments::AngleBracketed(angled_path) => {
let angled_path = angled_path.args.to_owned();
let filtered_paths: Vec<_> = angled_path
.into_iter()
.filter(|generic_arg| {
if let GenericArgument::Type(generic_type) = generic_arg {
if let Type::Path(_) = generic_type {
true
} else {
false
}
} else {
false
}
})
.collect();
// 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
)
} else if filtered_paths.len() <= 0 {
unreachable!(
"There should be more than zero filtered_output, but got: {:#?}",
filtered_paths
)
}
if filtered_paths.len() == 2 {
// There is something else than rlua
let gen_type = if let GenericArgument::Type(ret_type) =
filtered_paths
.first()
.expect("One path segment should exists")
.to_owned()
{
ret_type
} else {
unreachable!("These were filtered above.");
};
let return_type_as_type_prepared = quote! {-> #gen_type};
let return_type_as_return_type: ReturnType = syn::parse(
return_type_as_type_prepared.to_token_stream().into(),
)
.expect("This is valid.");
return_type_as_return_type
} else {
// There is only rlua
ReturnType::Default
}
}
_ => unimplemented!("Only for angled paths"),
}
}
_ => unimplemented!("Only for path types"),
};
match return_type {
ReturnType::Default => {
quote! {
{
let tx: std::sync::mpsc::Sender<crate::app::events::event_types::Event> =
context
.named_registry_value("sender_for_ci_commands")
.expect("This exists, it was set before");
tx
.send(Event::CommandEvent(Command::#function_name_pascal))
.expect("This should work, as the reciever is not dropped");
}
}
}
ReturnType::Type(_, _) => {
quote! {
{
let tx: std::sync::mpsc::Sender<crate::app::events::event_types::Event> =
context
.named_registry_value("sender_for_ci_commands")
.expect("This exists, it was set before");
tx
.send(Event::CommandEvent(Command::#function_name_pascal(input_str)))
.expect("This should work, as the reciever is not dropped");
}
}
}
}
}
};
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,
block: tx_send_block,
};
let mut tx_send_stmt = vec![Stmt::Expr(Expr::Block(tx_send_expr_block), None)];
let mut new_stmts: Vec<Stmt> = Vec::with_capacity(input.block.stmts.len() + 1);
new_stmts.append(&mut tx_send_stmt);
new_stmts.append(&mut input.block.stmts);
input.block.stmts = new_stmts;
input
}

View File

@ -0,0 +1,140 @@
use convert_case::{Case, Casing};
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use syn::{self, ReturnType};
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]
fn #function_name(context: Context, input_str: String) -> Result<(), rlua::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(
context: &mut rlua::Context,
tx: std::sync::mpsc::Sender<crate::app::events::event_types::Event>)
{
context.set_named_registry_value("sender_for_ci_commands", tx).expect("This should always work, as the value is added before all else");
let globals = context.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! {
let #function_name_ident = context.create_function(#field_ident).expect(
&format!(
"The function: `{}` should be defined",
#function_name
)
);
globals.set(#function_name, #function_name_ident).expect(
&format!(
"Setting a static global value ({}, fun_{}) should work",
#function_name,
#function_name
)
);
}
.into()
}
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 field_ident = field
.ident
.as_ref()
.expect("These are only the named field, thus they all should have a name.");
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 {
quote! {
#enum_variant_name,
}
}
})
.collect(),
_ => unimplemented!("Only implemented for named fileds"),
},
_ => unimplemented!("Only implemented for structs"),
};
let gen = quote! {
#[derive(Debug)]
pub enum Command {
#input_tokens
}
};
gen.into()
}

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;
// 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<String, rlua::Error> {
Ok(format!("Name is {}", name))
#[ci_command]
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

@ -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)
}

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 cli_log::info;

View File

@ -0,0 +1,22 @@
use anyhow::{Context, Result};
use cli_log::info;
use crate::app::{App, events::event_types::{EventStatus, Event}};
pub async fn handle(app: &mut App<'_>, command: &str) -> Result<EventStatus> {
info!("Recieved ci command: `{command}`; executing..");
// TODO: Should the ci support more than strings?
let output = app.lua.context(|context| -> Result<String> {
let output = context
.load(&command)
.eval::<String>()
.with_context(|| format!("Failed to execute: `{command}`"))?;
info!("Function evaluated to: `{output}`");
Ok(output)
})?;
app.transmitter.send(Event::CiOutput(output));
Ok(EventStatus::Ok)
}

View File

@ -2,7 +2,11 @@ use anyhow::Result;
use crossterm::event::{Event as CrosstermEvent, KeyCode, KeyEvent, KeyModifiers};
use crate::{
app::{command, command::Command, events::event_types::EventStatus, App},
app::{
command_interface::Command,
events::event_types::{Event, EventStatus},
App,
},
ui::central,
};
@ -11,25 +15,33 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result<E
CrosstermEvent::Key(KeyEvent {
code: KeyCode::Esc, ..
}) => {
command::execute(app.channel_tx(), Command::Exit).await?;
app.transmitter
.send(Event::CommandEvent(Command::Exit))
.await?;
}
CrosstermEvent::Key(KeyEvent {
code: KeyCode::Tab, ..
}) => {
command::execute(app.channel_tx(), Command::CyclePlanes).await?;
app.transmitter
.send(Event::CommandEvent(Command::CyclePlanes))
.await?;
}
CrosstermEvent::Key(KeyEvent {
code: KeyCode::BackTab,
..
}) => {
command::execute(app.channel_tx(), Command::CyclePlanesRev).await?;
app.transmitter
.send(Event::CommandEvent(Command::CyclePlanesRev))
.await?;
}
CrosstermEvent::Key(KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::CONTROL,
..
}) => {
command::execute(app.channel_tx(), Command::CommandLineShow).await?;
app.transmitter
.send(Event::CommandEvent(Command::CommandLineShow))
.await?;
}
input => match app.ui.input_position() {
central::InputPosition::MessageCompose => {
@ -39,11 +51,11 @@ pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result<E
modifiers: KeyModifiers::ALT,
..
}) => {
command::execute(
app.channel_tx(),
Command::RoomMessageSend(app.ui.message_compose.lines().join("\n")),
)
.await?;
app.transmitter
.send(Event::CommandEvent(Command::RoomMessageSend(
app.ui.message_compose.lines().join("\n"),
)))
.await?;
app.ui.message_compose_clear();
}
_ => {

View File

@ -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};

View File

@ -1,30 +1,31 @@
pub mod command;
pub mod command_interface;
pub mod events;
pub mod status;
pub mod transmitter;
use std::path::Path;
use std::{path::Path, sync::mpsc::Sender as StdSender};
use accounts::{Account, AccountsManager};
use anyhow::{Context, Error, Result};
use cli_log::info;
use matrix_sdk::Client;
use rlua::Lua;
use status::{State, Status};
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use crate::{accounts, app::command_interface::generate_ci_functions, ui::{central, setup}};
use crate::{
accounts::{Account, AccountsManager},
app::{command_interface::generate_ci_functions, events::event_types::Event},
ui::{central, setup},
};
use self::events::event_types::{self, Event};
use self::{events::event_types, transmitter::Transmitter};
pub struct App<'ui> {
ui: central::UI<'ui>,
accounts_manager: accounts::AccountsManager,
accounts_manager: AccountsManager,
status: Status,
channel_tx: mpsc::Sender<event_types::Event>,
channel_rx: mpsc::Receiver<event_types::Event>,
transmitter: Transmitter,
input_listener_killer: CancellationToken,
matrix_listener_killer: CancellationToken,
@ -33,11 +34,11 @@ pub struct App<'ui> {
impl App<'_> {
pub fn new() -> Result<Self> {
fn set_up_lua() -> Lua {
fn set_up_lua(tx: StdSender<Event>) -> Lua {
let lua = Lua::new();
lua.context(|mut lua_context| {
generate_ci_functions(&mut lua_context);
generate_ci_functions(&mut lua_context, tx);
});
lua
}
@ -50,19 +51,17 @@ impl App<'_> {
None
};
let (channel_tx, channel_rx) = mpsc::channel(256);
let transmitter = Transmitter::new();
Ok(Self {
ui: central::UI::new()?,
accounts_manager: AccountsManager::new(config)?,
status: Status::new(None),
channel_tx,
channel_rx,
transmitter,
input_listener_killer: CancellationToken::new(),
matrix_listener_killer: CancellationToken::new(),
lua: set_up_lua(),
lua: set_up_lua(transmitter.std_tx()),
})
}
@ -84,7 +83,7 @@ impl App<'_> {
pub async fn run(&mut self) -> Result<()> {
// Spawn input event listener
tokio::task::spawn(events::poll_input_events(
self.channel_tx.clone(),
self.transmitter.tx(),
self.input_listener_killer.clone(),
));
@ -100,10 +99,11 @@ impl App<'_> {
self.status.set_state(State::Main);
self.ui.update(&self.status).await?;
let event: event_types::Event = match self.channel_rx.recv().await {
Some(e) => e,
None => return Err(Error::msg("Event channel has no senders")),
};
let event = self
.transmitter
.recv()
.await
.context("Failed to get next event")?;
match event.handle(self).await? {
event_types::EventStatus::Ok => (),
@ -123,10 +123,11 @@ impl App<'_> {
self.status.set_state(State::Setup);
self.ui.update_setup().await?;
let event: event_types::Event = match self.channel_rx.recv().await {
Some(e) => e,
None => return Err(Error::msg("Event channel has no senders")),
};
let event = self
.transmitter
.recv()
.await
.context("Failed to get next event")?;
match event.handle(self).await? {
event_types::EventStatus::Ok => (),
@ -150,7 +151,7 @@ impl App<'_> {
// Spawn Matrix Event Listener
tokio::task::spawn(events::poll_matrix_events(
self.channel_tx.clone(),
self.transmitter.tx(),
self.matrix_listener_killer.clone(),
client.clone(),
));
@ -204,8 +205,4 @@ impl App<'_> {
pub fn client(&self) -> Option<&Client> {
self.accounts_manager.client()
}
pub fn channel_tx(&self) -> &mpsc::Sender<Event> {
&self.channel_tx
}
}

43
src/app/transmitter.rs Normal file
View File

@ -0,0 +1,43 @@
use std::sync::mpsc as StdMpsc;
use anyhow::{bail, Context, Result};
use tokio::sync::mpsc;
use super::events::event_types::Event;
pub struct Transmitter {
tx: mpsc::Sender<Event>,
rx: mpsc::Receiver<Event>,
std_tx: StdMpsc::Sender<Event>,
std_rx: StdMpsc::Receiver<Event>,
}
impl Transmitter {
pub fn new() -> Transmitter {
let (std_tx, std_rx) = StdMpsc::channel();
let (tx, rx) = mpsc::channel(256);
Transmitter {
tx,
rx,
std_tx,
std_rx,
}
}
pub fn tx(&self) -> mpsc::Sender<Event> {
self.tx.to_owned()
}
pub fn std_tx(&self) -> StdMpsc::Sender<Event> {
self.std_tx.to_owned()
}
pub async fn recv(&mut self) -> Result<Event> {
match self.rx.recv().await {
Some(event) => Ok(event),
None => bail!("Event channel has no senders"),
}
}
pub async fn send(&mut self, event: Event) -> Result<()> {
self.tx.send(event).await.context("Failed to send event")
}
}