moved json processing to state_update
This commit is contained in:
parent
3d79ea1633
commit
3cb3123381
2 changed files with 32 additions and 23 deletions
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue