forked from trinitrix/core
1
0
Fork 0

Refactor(Events): Changed the `Event` type from a struct to an enum

This commit is contained in:
antifallobst 2023-07-14 00:06:10 +02:00
parent dd3c765ea2
commit dfeac4662d
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
6 changed files with 24 additions and 91 deletions

View File

@ -1,7 +1,7 @@
use anyhow::Result; use anyhow::Result;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use super::events::event_types::{Event, EventBuilder}; use super::events::event_types::Event;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Command { pub enum Command {
@ -19,7 +19,7 @@ pub enum Command {
} }
pub async fn execute(channel: &mpsc::Sender<Event>, command: Command) -> Result<()> { pub async fn execute(channel: &mpsc::Sender<Event>, command: Command) -> Result<()> {
let event = EventBuilder::default().command_event(command).build(); let event = Event::CommandEvent(command);
channel.send(event).await?; channel.send(event).await?;
Ok(()) Ok(())
} }

View File

@ -10,39 +10,32 @@ use self::handlers::{command, main, matrix, setup};
use super::EventStatus; use super::EventStatus;
#[derive(Debug)] #[derive(Debug)]
pub struct Event { pub enum Event {
pub(super) input_event: Option<CrosstermEvent>, InputEvent(CrosstermEvent),
pub(super) matrix_event: Option<matrix_sdk::deserialized_responses::SyncResponse>, MatrixEvent(matrix_sdk::deserialized_responses::SyncResponse),
pub(super) command_event: Option<Command>, CommandEvent(Command),
} }
impl Event { impl Event {
pub async fn handle(&self, app: &mut App<'_>) -> Result<EventStatus> { pub async fn handle(&self, app: &mut App<'_>) -> Result<EventStatus> {
if let Some(matrix_event) = &self.matrix_event { match &self {
return matrix::handle(app, matrix_event) Event::MatrixEvent(event) => matrix::handle(app, event)
.await .await
.with_context(|| format!("Failed to handle matrix event: `{:#?}`", matrix_event)); .with_context(|| format!("Failed to handle matrix event: `{:#?}`", event)),
}
if let Some(command_event) = &self.command_event { Event::CommandEvent(event) => command::handle(app, event)
return command::handle(app, command_event).await.with_context(|| { .await
format!("Failed to handle command event: `{:#?}`", command_event) .with_context(|| format!("Failed to handle command event: `{:#?}`", event)),
});
}
if let Some(input_event) = &self.input_event { Event::InputEvent(event) => match app.status.state() {
let status = match app.status.state() { State::None => Ok(EventStatus::Ok),
State::None => EventStatus::Ok, State::Main => main::handle(app, event)
State::Main => main::handle(app, input_event).await.with_context(|| { .await
format!("Failed to handle input event: `{:#?}`", input_event) .with_context(|| format!("Failed to handle input event: `{:#?}`", event)),
})?, State::Setup => setup::handle(app, event)
State::Setup => setup::handle(app, input_event).await.with_context(|| { .await
format!("Failed to handle input event: `{:#?}`", input_event) .with_context(|| format!("Failed to handle input event: `{:#?}`", event)),
})?, },
};
return Ok(status);
} }
Ok(EventStatus::Ok)
} }
} }

View File

@ -1,51 +0,0 @@
use super::Event;
pub struct EventBuilder {
event: Event,
}
impl Default for Event {
fn default() -> Self {
Self {
input_event: None,
matrix_event: None,
command_event: None,
}
}
}
impl Default for EventBuilder {
fn default() -> Self {
Self {
event: Event::default(),
}
}
}
impl EventBuilder {
pub fn input_event(&mut self, input_event: crossterm::event::Event) -> &Self {
self.event.input_event = Some(input_event);
self
}
pub fn matrix_event(
&mut self,
matrix_event: matrix_sdk::deserialized_responses::SyncResponse,
) -> &Self {
self.event.matrix_event = Some(matrix_event);
self
}
pub fn command_event(&mut self, command_event: crate::app::command::Command) -> &Self {
self.event.command_event = Some(command_event);
self
}
pub fn build(&self) -> Event {
Event {
input_event: self.event.input_event.to_owned(),
matrix_event: self.event.matrix_event.to_owned(),
command_event: self.event.command_event.to_owned(),
}
}
}

View File

@ -1,8 +1,5 @@
pub mod event_builder;
pub mod event; pub mod event;
pub mod event_status; pub mod event_status;
pub use self::event_builder::*;
pub use self::event::*; pub use self::event::*;
pub use self::event_status::*; pub use self::event_status::*;

View File

@ -5,8 +5,6 @@ use matrix_sdk::{config::SyncSettings, Client, LoopCtrl};
use tokio::{sync::mpsc, time::Duration}; use tokio::{sync::mpsc, time::Duration};
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use crate::app::events::event_types::EventBuilder;
use self::event_types::Event; use self::event_types::Event;
pub async fn poll_input_events( pub async fn poll_input_events(
@ -16,10 +14,7 @@ pub async fn poll_input_events(
async fn poll_input_events_stage_2(channel: mpsc::Sender<Event>) -> Result<()> { async fn poll_input_events_stage_2(channel: mpsc::Sender<Event>) -> Result<()> {
loop { loop {
if crossterm::event::poll(Duration::from_millis(100))? { if crossterm::event::poll(Duration::from_millis(100))? {
let event = EventBuilder::default() let event = Event::InputEvent(crossterm::event::read()?);
.input_event(crossterm::event::read()?)
.build();
channel.send(event).await?; channel.send(event).await?;
} else { } else {
tokio::task::yield_now().await; tokio::task::yield_now().await;
@ -49,7 +44,7 @@ pub async fn poll_matrix_events(
client client
.sync_with_callback(sync_settings, |response| async move { .sync_with_callback(sync_settings, |response| async move {
let event = EventBuilder::default().matrix_event(response).build(); let event = Event::MatrixEvent(response);
match tx.send(event).await { match tx.send(event).await {
Ok(_) => LoopCtrl::Continue, Ok(_) => LoopCtrl::Continue,

View File

@ -14,10 +14,9 @@ use status::{State, Status};
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use crate::app::command::Command;
use crate::{accounts, app::command_interface::generate_ci_functions, ui}; use crate::{accounts, app::command_interface::generate_ci_functions, ui};
use self::events::event_types::{self, Event, EventBuilder}; use self::events::event_types::{self, Event};
pub struct App<'ui> { pub struct App<'ui> {
ui: ui::UI<'ui>, ui: ui::UI<'ui>,