From 3cb3123381c3897679033d51ef60a54e9fad16ff Mon Sep 17 00:00:00 2001 From: Nickiel12 <35903114+Nickiel12@users.noreply.github.com> Date: Sat, 18 Dec 2021 23:03:32 -0800 Subject: [PATCH] moved json processing to state_update --- src/modules/message_handler.rs | 22 --------------- src/modules/stream_states/state_update.rs | 33 ++++++++++++++++++++++- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/modules/message_handler.rs b/src/modules/message_handler.rs index 40a9c4d..90d6709 100644 --- a/src/modules/message_handler.rs +++ b/src/modules/message_handler.rs @@ -8,7 +8,6 @@ pub enum StateMessage { pub trait MessageHandler { fn handle_update(&mut self, update: StateUpdate) -> (); - fn create_update_from_string(update_json: String) -> StateUpdate; fn get_states(&self) -> StreamState; } @@ -19,26 +18,5 @@ impl MessageHandler for StreamState { fn get_states(&self) -> StreamState{ self.clone() } - fn create_update_from_string(update_json: String) -> StateUpdate { - let json: serde_json::Value = serde_json::from_str(&update_json[1..]).unwrap(); - let message_type = &json["type"]; - match message_type.as_str().unwrap() { - "button" => { - let value = &json["button"]; - }, - "Timer_Length" => { - let new_timer_length = &json["data"]; - }, - "update" => { - println!("Update all!! *Poof*!"); - }, - _ => { - - } - } - - println!("type: {}", json["type"]); - StateUpdate::ChangeSceneOnChangeSlideHotkey(false) - } } \ No newline at end of file diff --git a/src/modules/stream_states/state_update.rs b/src/modules/stream_states/state_update.rs index 88119f7..a7edc1a 100644 --- a/src/modules/stream_states/state_update.rs +++ b/src/modules/stream_states/state_update.rs @@ -1,4 +1,5 @@ -use super::enums::Scenes; +use super::enums::{SubScenes, Scenes}; +use serde_json::Value; #[derive(Debug, PartialEq, Clone)] @@ -11,5 +12,35 @@ pub enum StateUpdate { TimerCanRun(bool), TimerLength(f32), TimerText(String), + SubScene(SubScenes), Scene(Scenes), + UpdateClient, +} + +impl StateUpdate { + pub fn json_to_state_update(incoming_json: Value) -> Self { + let message_type = &incoming_json["type"]; + + match message_type.as_str().unwrap() { + "button" => { + let value = &incoming_json["button"]; + match value.as_str().unwrap() { + "Scene_Camera" => {StateUpdate::Scene(Scenes::Camera)} + + _ => {panic!("trying to use a button type I don't know!")} + } + }, + "Timer_Length" => { + let new_timer_length = &incoming_json["data"]; + StateUpdate::TimerLength(new_timer_length.as_f64().unwrap() as f32) + }, + "update" => { + StateUpdate::UpdateClient + }, + _ => { + panic!("State Update Could Not Cast the json: {:?}", incoming_json.as_str()); + } + } + + } } \ No newline at end of file