From dfeac4662dd22f7e57fccd0bbd15fd6c15e1a3bb Mon Sep 17 00:00:00 2001 From: antifallobst Date: Fri, 14 Jul 2023 00:06:10 +0200 Subject: [PATCH] Refactor(Events): Changed the `Event` type from a struct to an enum --- src/app/command.rs | 4 +- src/app/events/event_types/event/mod.rs | 45 ++++++++---------- src/app/events/event_types/event_builder.rs | 51 --------------------- src/app/events/event_types/mod.rs | 3 -- src/app/events/mod.rs | 9 +--- src/app/mod.rs | 3 +- 6 files changed, 24 insertions(+), 91 deletions(-) delete mode 100644 src/app/events/event_types/event_builder.rs diff --git a/src/app/command.rs b/src/app/command.rs index 47b372a..6342bf9 100644 --- a/src/app/command.rs +++ b/src/app/command.rs @@ -1,7 +1,7 @@ use anyhow::Result; use tokio::sync::mpsc; -use super::events::event_types::{Event, EventBuilder}; +use super::events::event_types::Event; #[derive(Debug, Clone)] pub enum Command { @@ -19,7 +19,7 @@ pub enum Command { } pub async fn execute(channel: &mpsc::Sender, command: Command) -> Result<()> { - let event = EventBuilder::default().command_event(command).build(); + let event = Event::CommandEvent(command); channel.send(event).await?; Ok(()) } diff --git a/src/app/events/event_types/event/mod.rs b/src/app/events/event_types/event/mod.rs index 748d1cd..ffdad06 100644 --- a/src/app/events/event_types/event/mod.rs +++ b/src/app/events/event_types/event/mod.rs @@ -10,39 +10,32 @@ use self::handlers::{command, main, matrix, setup}; use super::EventStatus; #[derive(Debug)] -pub struct Event { - pub(super) input_event: Option, - pub(super) matrix_event: Option, - pub(super) command_event: Option, +pub enum Event { + InputEvent(CrosstermEvent), + MatrixEvent(matrix_sdk::deserialized_responses::SyncResponse), + CommandEvent(Command), } impl Event { pub async fn handle(&self, app: &mut App<'_>) -> Result { - if let Some(matrix_event) = &self.matrix_event { - return matrix::handle(app, matrix_event) + match &self { + Event::MatrixEvent(event) => matrix::handle(app, event) .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 { - return command::handle(app, command_event).await.with_context(|| { - format!("Failed to handle command event: `{:#?}`", command_event) - }); - } + Event::CommandEvent(event) => command::handle(app, event) + .await + .with_context(|| format!("Failed to handle command event: `{:#?}`", event)), - if let Some(input_event) = &self.input_event { - let status = match app.status.state() { - State::None => EventStatus::Ok, - State::Main => main::handle(app, input_event).await.with_context(|| { - format!("Failed to handle input event: `{:#?}`", input_event) - })?, - State::Setup => setup::handle(app, input_event).await.with_context(|| { - format!("Failed to handle input event: `{:#?}`", input_event) - })?, - }; - return Ok(status); + Event::InputEvent(event) => match app.status.state() { + State::None => Ok(EventStatus::Ok), + State::Main => main::handle(app, event) + .await + .with_context(|| format!("Failed to handle input event: `{:#?}`", event)), + State::Setup => setup::handle(app, event) + .await + .with_context(|| format!("Failed to handle input event: `{:#?}`", event)), + }, } - - Ok(EventStatus::Ok) } } diff --git a/src/app/events/event_types/event_builder.rs b/src/app/events/event_types/event_builder.rs deleted file mode 100644 index c9e2a49..0000000 --- a/src/app/events/event_types/event_builder.rs +++ /dev/null @@ -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(), - } - } -} diff --git a/src/app/events/event_types/mod.rs b/src/app/events/event_types/mod.rs index 48a0186..78356d6 100644 --- a/src/app/events/event_types/mod.rs +++ b/src/app/events/event_types/mod.rs @@ -1,8 +1,5 @@ -pub mod event_builder; pub mod event; pub mod event_status; -pub use self::event_builder::*; pub use self::event::*; pub use self::event_status::*; - diff --git a/src/app/events/mod.rs b/src/app/events/mod.rs index 830cd61..ebee062 100644 --- a/src/app/events/mod.rs +++ b/src/app/events/mod.rs @@ -5,8 +5,6 @@ use matrix_sdk::{config::SyncSettings, Client, LoopCtrl}; use tokio::{sync::mpsc, time::Duration}; use tokio_util::sync::CancellationToken; -use crate::app::events::event_types::EventBuilder; - use self::event_types::Event; 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) -> Result<()> { loop { if crossterm::event::poll(Duration::from_millis(100))? { - let event = EventBuilder::default() - .input_event(crossterm::event::read()?) - .build(); - + let event = Event::InputEvent(crossterm::event::read()?); channel.send(event).await?; } else { tokio::task::yield_now().await; @@ -49,7 +44,7 @@ pub async fn poll_matrix_events( client .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 { Ok(_) => LoopCtrl::Continue, diff --git a/src/app/mod.rs b/src/app/mod.rs index 9ae1ef6..10652c9 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -14,10 +14,9 @@ use status::{State, Status}; use tokio::sync::mpsc; use tokio_util::sync::CancellationToken; -use crate::app::command::Command; 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> { ui: ui::UI<'ui>,