From ef37f875ec87d97ce971f993e6bd7f132102394e Mon Sep 17 00:00:00 2001 From: Nickiel12 <35903114+Nickiel12@users.noreply.github.com> Date: Thu, 16 Dec 2021 15:39:36 -0800 Subject: [PATCH] changed message_handler to be a trait --- src/modules/message_handler.rs | 34 +++++--------- .../stream_states/stream_states_class.rs | 16 +++---- src/tests/message_handler_tests.rs | 44 ++++++++++--------- src/tests/stream_states_tests.rs | 8 ++-- 4 files changed, 47 insertions(+), 55 deletions(-) diff --git a/src/modules/message_handler.rs b/src/modules/message_handler.rs index 9b28cd1..8ae249d 100644 --- a/src/modules/message_handler.rs +++ b/src/modules/message_handler.rs @@ -1,31 +1,21 @@ -use std::sync::mpsc::{Receiver}; - -use super::stream_states::{stream_states_class::StreamStates, enums::StateUpdate}; +use super::stream_states::{enums::StateUpdate, stream_states_class::StreamState}; pub enum StateMessage { - StateUpdateContainer(StateUpdate), + StateUpdate(StateUpdate), + GetStates, CloseListener, } -pub trait MessageListnerThread { - fn listen(self, rx: Receiver) -> std::thread::JoinHandle<()>; +pub trait MessageHandler { + fn handle_update(&mut self, update: StateUpdate) -> (); + fn get_states(&self) -> StreamState; } -#[derive(Debug)] -pub struct MessageHandler { - pub state: StreamStates, -} - -impl MessageHandler { - pub fn new() -> Self { - return MessageHandler { - state: StreamStates::new(), - } +impl MessageHandler for StreamState { + fn handle_update(&mut self, update: StateUpdate) { + self.update(update.clone()); + } + fn get_states(&self) -> StreamState{ + self.clone() } - - pub fn handle_update(&mut self, update: StateUpdate) { - self.state.update(update.clone()); - } - - } \ No newline at end of file diff --git a/src/modules/stream_states/stream_states_class.rs b/src/modules/stream_states/stream_states_class.rs index 5414d6a..3331e0e 100644 --- a/src/modules/stream_states/stream_states_class.rs +++ b/src/modules/stream_states/stream_states_class.rs @@ -1,7 +1,7 @@ use super::enums::{self, StateUpdate}; -#[derive(Debug)] -pub struct StreamStates { +#[derive(Debug, Clone)] +pub struct StreamState { pub stream_running: bool, pub stream_is_muted: bool, pub computer_sound_is_on: bool, @@ -18,9 +18,9 @@ pub struct StreamStates { screen_sub_scene: enums::Scenes, } -impl Default for StreamStates { +impl Default for StreamState { fn default() -> Self { - StreamStates { + StreamState { stream_running: false, stream_is_muted: false, computer_sound_is_on: true, @@ -39,9 +39,9 @@ impl Default for StreamStates { } } -impl StreamStates { +impl StreamState { pub fn new() -> Self { - StreamStates{..Default::default()} + StreamState{..Default::default()} } pub fn update(&mut self, update: StateUpdate) { @@ -76,10 +76,10 @@ impl StreamStates { match scene { enums::Scenes::CameraDefault | enums::Scenes::CameraWithUpperRight | enums::Scenes::CameraWithLargeUpperRight | enums::Scenes::CameraWithLowerRight - => {StreamStates::set_camera_scene(self, scene)}, + => {StreamState::set_camera_scene(self, scene)}, enums::Scenes::ScreenDefault | enums::Scenes::ScreenWithUpperRight | enums::Scenes::ScreenWithLowerRight - => {StreamStates::set_screen_scene(self, scene)}, + => {StreamState::set_screen_scene(self, scene)}, enums::Scenes::Augmented => {self.current_scene = *scene;} } } diff --git a/src/tests/message_handler_tests.rs b/src/tests/message_handler_tests.rs index 90ee00c..63e70ac 100644 --- a/src/tests/message_handler_tests.rs +++ b/src/tests/message_handler_tests.rs @@ -1,35 +1,36 @@ use std::{sync::mpsc::{self, Receiver}, thread::{self, JoinHandle}}; -use crate::modules::{message_handler::{MessageHandler, StateMessage}, stream_states::{enums::{StateUpdate, Scenes}}}; - - +use crate::modules::{message_handler::{MessageHandler, StateMessage}, stream_states::{enums::{StateUpdate, Scenes}, stream_states_class::StreamState}}; #[test] -fn create_message_handler() { - MessageHandler::new(); +fn does_stream_state_implement_message_handler() { + let mut state = StreamState::new(); + state.handle_update(StateUpdate::ComputerSoundIsOn(false)); + assert_eq!(state.computer_sound_is_on, false); } #[test] fn message_hander_can_handle_basic_message() { let (tx, rx) = mpsc::channel(); - fn listen(mut handler: MessageHandler, rx: Receiver) -> JoinHandle<()>{ + fn listen(mut handler: StreamState, rx: Receiver) -> JoinHandle<()>{ thread::spawn(move || { for message in rx { match message { - StateMessage::StateUpdateContainer(update) => {handler.handle_update(update)}, + StateMessage::StateUpdate(update) => {handler.handle_update(update)}, + StateMessage::GetStates => {panic!("Why is getstates being called in this test?");}, StateMessage::CloseListener => {break;}, }; } - assert_eq!(handler.state.computer_sound_is_on, false); + assert_eq!(handler.computer_sound_is_on, false); }) } - let handler = MessageHandler::new(); + let handler = StreamState::new(); let join_handle = listen(handler, rx); - tx.send(StateMessage::StateUpdateContainer(StateUpdate::ComputerSoundIsOn(false))).unwrap(); + tx.send(StateMessage::StateUpdate(StateUpdate::ComputerSoundIsOn(false))).unwrap(); thread::sleep(std::time::Duration::from_millis(1000)); tx.send(StateMessage::CloseListener).unwrap(); join_handle.join().unwrap(); @@ -40,29 +41,30 @@ fn message_handler_can_handle_multiple_messages() { let (tx, rx) = mpsc::channel(); - fn listen(mut handler: MessageHandler, rx: Receiver) -> JoinHandle<()>{ + fn listen(mut handler: StreamState, rx: Receiver) -> JoinHandle<()>{ thread::spawn(move || { for message in rx { match message { - StateMessage::StateUpdateContainer(update) => {handler.handle_update(update)}, + StateMessage::StateUpdate(update) => {handler.handle_update(update)}, + StateMessage::GetStates => {panic!("Why is getstates being called in this test?");}, StateMessage::CloseListener => {break;}, }; } - assert_eq!(handler.state.computer_sound_is_on, false); - assert_eq!(handler.state.scene_is_augmented, true); - assert_eq!(handler.state.scene_is_augmented, true); - assert_eq!(handler.state.get_current_scene(), Scenes::Augmented); - assert_eq!(handler.state.timer_can_run, false); + assert_eq!(handler.computer_sound_is_on, false); + assert_eq!(handler.scene_is_augmented, true); + assert_eq!(handler.scene_is_augmented, true); + assert_eq!(handler.get_current_scene(), Scenes::Augmented); + assert_eq!(handler.timer_can_run, false); }) } - let handler = MessageHandler::new(); + let handler = StreamState::new(); let join_handle = listen(handler, rx); - tx.send(StateMessage::StateUpdateContainer(StateUpdate::ComputerSoundIsOn(false))).unwrap(); - tx.send(StateMessage::StateUpdateContainer(StateUpdate::SceneIsAugmented(true))).unwrap(); - tx.send(StateMessage::StateUpdateContainer(StateUpdate::TimerCanRun(false))).unwrap(); + tx.send(StateMessage::StateUpdate(StateUpdate::ComputerSoundIsOn(false))).unwrap(); + tx.send(StateMessage::StateUpdate(StateUpdate::SceneIsAugmented(true))).unwrap(); + tx.send(StateMessage::StateUpdate(StateUpdate::TimerCanRun(false))).unwrap(); thread::sleep(std::time::Duration::from_millis(1000)); tx.send(StateMessage::CloseListener).unwrap(); join_handle.join().unwrap(); diff --git a/src/tests/stream_states_tests.rs b/src/tests/stream_states_tests.rs index 434f56e..de147f5 100644 --- a/src/tests/stream_states_tests.rs +++ b/src/tests/stream_states_tests.rs @@ -27,7 +27,7 @@ fn has_all_enums() { #[test] fn create_stream_states_class() { - let stream_state = s_s::stream_states_class::StreamStates::new(); + let stream_state = s_s::stream_states_class::StreamState::new(); assert_eq!(stream_state.stream_running, false); assert_eq!(stream_state.stream_is_muted, false); assert_eq!(stream_state.computer_sound_is_on, true); @@ -46,7 +46,7 @@ fn create_stream_states_class() { #[test] fn scene_correctness(){ - let mut stream_state = s_s::stream_states_class::StreamStates::new(); + let mut stream_state = s_s::stream_states_class::StreamState::new(); assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::CameraDefault); assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraDefault); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault); @@ -91,7 +91,7 @@ fn scene_correctness(){ #[test] fn test_updating() { - let mut stream_state = s_s::stream_states_class::StreamStates::new(); + let mut stream_state = s_s::stream_states_class::StreamState::new(); assert_eq!(stream_state.timer_can_run, true); stream_state.update(StateUpdate::TimerCanRun(false)); @@ -132,7 +132,7 @@ fn can_run_in_thread() { let (tx, rx) = mpsc::channel(); let rx_thread = thread::spawn(move || { - let mut stream_state = s_s::stream_states_class::StreamStates::new(); + let mut stream_state = s_s::stream_states_class::StreamState::new(); for received in rx { assert_eq!(received, StateUpdate::StreamRunning(true)); stream_state.update(received);