changed to use string values for true and false

This commit is contained in:
Nickiel12 2021-12-20 15:57:15 -08:00
parent 3decc966c9
commit b44a16e0e1
2 changed files with 50 additions and 26 deletions

View file

@ -1,6 +1,6 @@
use std::ops::Add; use std::ops::Add;
use super::enums::{SubScenes, Scenes}; use super::enums::{SubScenes, Scenes, SlideChange};
use serde_json::Value; use serde_json::Value;
@ -17,6 +17,7 @@ pub enum StateUpdate {
TimerText(String), TimerText(String),
SubScene(SubScenes), SubScene(SubScenes),
Scene(Scenes), Scene(Scenes),
ChangeSlide(SlideChange),
UpdateClient, UpdateClient,
} }
@ -34,11 +35,12 @@ impl StateUpdate {
match scene { match scene {
"Scene_Camera" => {StateUpdate::Scene(Scenes::Camera)} "Scene_Camera" => {StateUpdate::Scene(Scenes::Camera)}
"Scene_Screen" => {StateUpdate::Scene(Scenes::Screen)} "Scene_Screen" => {StateUpdate::Scene(Scenes::Screen)}
"Scene_Is_Augmented" => {StateUpdate::SceneIsAugmented(incoming_json["data"].as_bool().unwrap())},
_ => {panic!("unknown Scene! {}", scene)} _ => {panic!("unknown Scene! {}", scene)}
} }
} }
"Scene_Is_Augmented" => {StateUpdate::SceneIsAugmented(string_to_bool(incoming_json["data"].as_str().unwrap()))},
//SubScenes //SubScenes
"SubScene" => { "SubScene" => {
let subscene = incoming_json["data"].as_str().unwrap(); let subscene = incoming_json["data"].as_str().unwrap();
@ -55,24 +57,25 @@ impl StateUpdate {
} }
//Slide changing behavior //Slide changing behavior
"Timer_Can_Run" => {StateUpdate::TimerCanRun(incoming_json["data"].as_bool().unwrap())} "Timer_Can_Run" => {StateUpdate::TimerCanRun(string_to_bool(incoming_json["data"].as_str().unwrap()))}
"Change_With_Clicker" => {StateUpdate::ChangeSceneOnChangeSlide(incoming_json["data"].as_bool().unwrap())}, "Change_With_Clicker" => {StateUpdate::ChangeSceneOnChangeSlide(string_to_bool(incoming_json["data"].as_str().unwrap()))},
"Timer_Length" => { "Timer_Length" => {
let new_timer_length = &incoming_json["data"]; let new_timer_length = &incoming_json["data"];
StateUpdate::TimerLength(new_timer_length.as_f64().unwrap() as f32) StateUpdate::TimerLength(new_timer_length.as_f64().unwrap() as f32)
}, },
//Extra Toggles //Extra Toggles
"Toggle_Computer_Volume" => {StateUpdate::ComputerSoundIsOn(incoming_json["data"].as_bool().unwrap())}, "Toggle_Computer_Volume" => {StateUpdate::ComputerSoundIsOn(string_to_bool(incoming_json["data"].as_str().unwrap()))},
"Toggle_Stream_Volume" => {StateUpdate::StreamIsMuted(incoming_json["data"].as_bool().unwrap())}, "Toggle_Stream_Volume" => {StateUpdate::StreamIsMuted(string_to_bool(incoming_json["data"].as_str().unwrap()))},
"Media_Pause_Play" => {StateUpdate::ComputerMediaDoPause(incoming_json["data"].as_bool().unwrap())}, "Media_Pause_Play" => {StateUpdate::ComputerMediaDoPause(string_to_bool(incoming_json["data"].as_str().unwrap()))},
"all" => {StateUpdate::UpdateClient}, "all" => {StateUpdate::UpdateClient},
"Stream_Running" => {StateUpdate::StreamRunning(string_to_bool(incoming_json["data"].as_str().unwrap()))}
"Next_Slide" => {StateUpdate::ChangeSlide(SlideChange::Next)},
"Prev_Slide" => {StateUpdate::ChangeSlide(SlideChange::Previous)}
//Unimplemented //Unimplemented
"Next_Slide" |
"Prev_Slide" |
_ => {panic!("trying to use a button type I don't know!: {}", value)} _ => {panic!("trying to use a button type I don't know!: {}", value)}
} }
}, },
@ -104,7 +107,9 @@ impl StateUpdate {
("SubScene", scene.to_string())}, ("SubScene", scene.to_string())},
StateUpdate::Scene(scene) => { StateUpdate::Scene(scene) => {
("Scene", scene.to_string())}, ("Scene", scene.to_string())},
StateUpdate::ComputerMediaDoPause(is_true) => todo!(), StateUpdate::ComputerMediaDoPause(is_true) => {
("Toggle_Computer_Volume", is_true.to_string())},
StateUpdate::ChangeSlide(value) => todo!(),
StateUpdate::UpdateClient => todo!(), StateUpdate::UpdateClient => todo!(),
}; };
serde_json::json!({ serde_json::json!({
@ -114,3 +119,13 @@ impl StateUpdate {
}) })
} }
} }
fn string_to_bool(input: &str) -> bool{
if input == "true" {
true
} else if input == "false" {
false
} else {
panic!("string to bool doesn't recognize the input: {}", input);
}
}

View file

@ -1,4 +1,4 @@
use crate::modules::stream_states::{state_update::StateUpdate, enums::{Scenes, SubScenes}}; use crate::modules::stream_states::{state_update::StateUpdate, enums::{Scenes, SubScenes, SlideChange}};
@ -13,47 +13,47 @@ fn test_json_to_state_update() {
).unwrap()), StateUpdate::Scene(Scenes::Screen)); ).unwrap()), StateUpdate::Scene(Scenes::Screen));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
"{\"type\": \"update\", \"update\": \"Scene_Is_Augmented\", \"data\": true}" "{\"type\": \"update\", \"update\": \"Scene_Is_Augmented\", \"data\": \"true\"}"
).unwrap()), StateUpdate::SceneIsAugmented(true)); ).unwrap()), StateUpdate::SceneIsAugmented(true));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
"{\"type\": \"update\", \"update\": \"Auto_Change_To_Camera\", \"data\": true}" "{\"type\": \"update\", \"update\": \"Timer_Can_Run\", \"data\": \"true\"}"
).unwrap()), StateUpdate::TimerCanRun(true)); ).unwrap()), StateUpdate::TimerCanRun(true));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
"{\"type\": \"update\", \"update\": \"Auto_Change_To_Camera\", \"data\": false}" "{\"type\": \"update\", \"update\": \"Timer_Can_Run\", \"data\": \"false\"}"
).unwrap()), StateUpdate::TimerCanRun(false)); ).unwrap()), StateUpdate::TimerCanRun(false));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
"{\"type\": \"update\", \"update\": \"Change_With_Clicker\", \"data\": true}" "{\"type\": \"update\", \"update\": \"Change_With_Clicker\", \"data\": \"true\"}"
).unwrap()), StateUpdate::ChangeSceneOnChangeSlide(true)); ).unwrap()), StateUpdate::ChangeSceneOnChangeSlide(true));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
"{\"type\": \"update\", \"update\": \"Change_With_Clicker\", \"data\": false}" "{\"type\": \"update\", \"update\": \"Change_With_Clicker\", \"data\": \"false\"}"
).unwrap()), StateUpdate::ChangeSceneOnChangeSlide(false)); ).unwrap()), StateUpdate::ChangeSceneOnChangeSlide(false));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
"{\"type\": \"update\", \"update\": \"Toggle_Computer_Volume\", \"data\": true}" "{\"type\": \"update\", \"update\": \"Toggle_Computer_Volume\", \"data\": \"true\"}"
).unwrap()), StateUpdate::ComputerSoundIsOn(true)); ).unwrap()), StateUpdate::ComputerSoundIsOn(true));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
"{\"type\": \"update\", \"update\": \"Toggle_Computer_Volume\", \"data\": false}" "{\"type\": \"update\", \"update\": \"Toggle_Computer_Volume\", \"data\": \"false\"}"
).unwrap()), StateUpdate::ComputerSoundIsOn(false)); ).unwrap()), StateUpdate::ComputerSoundIsOn(false));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
"{\"type\": \"update\", \"update\": \"Toggle_Stream_Volume\", \"data\": true}" "{\"type\": \"update\", \"update\": \"Toggle_Stream_Volume\", \"data\": \"true\"}"
).unwrap()), StateUpdate::StreamIsMuted(true)); ).unwrap()), StateUpdate::StreamIsMuted(true));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
"{\"type\": \"update\", \"update\": \"Toggle_Stream_Volume\", \"data\": false}" "{\"type\": \"update\", \"update\": \"Toggle_Stream_Volume\", \"data\": \"false\"}"
).unwrap()), StateUpdate::StreamIsMuted(false)); ).unwrap()), StateUpdate::StreamIsMuted(false));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
"{\"type\": \"update\", \"update\": \"Media_Pause_Play\", \"data\": true}" "{\"type\": \"update\", \"update\": \"Media_Pause_Play\", \"data\": \"true\"}"
).unwrap()), StateUpdate::ComputerMediaDoPause(true)); ).unwrap()), StateUpdate::ComputerMediaDoPause(true));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
"{\"type\": \"update\", \"update\": \"Media_Pause_Play\", \"data\": false}" "{\"type\": \"update\", \"update\": \"Media_Pause_Play\", \"data\": \"false\"}"
).unwrap()), StateUpdate::ComputerMediaDoPause(false)); ).unwrap()), StateUpdate::ComputerMediaDoPause(false));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
@ -85,9 +85,17 @@ fn test_json_to_state_update() {
).unwrap()), StateUpdate::SubScene(SubScenes::ScreenWithLowerRight)); ).unwrap()), StateUpdate::SubScene(SubScenes::ScreenWithLowerRight));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
"{\"type\": \"update\", \"update\": \"Timer_Length\" \"data\": 5.5}" "{\"type\": \"update\", \"update\": \"Timer_Length\", \"data\": 5.5}"
).unwrap()), StateUpdate::TimerLength(5.5)); ).unwrap()), StateUpdate::TimerLength(5.5));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
"{\"type\": \"update\", \"update\": \"Next_Slide\"}"
).unwrap()), StateUpdate::ChangeSlide(SlideChange::Next));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
"{\"type\": \"update\", \"update\": \"Prev_Slide\"}"
).unwrap()), StateUpdate::ChangeSlide(SlideChange::Previous));
assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str(
"{\"type\": \"update\", \"update\":\"all\"}" "{\"type\": \"update\", \"update\":\"all\"}"
).unwrap()), StateUpdate::UpdateClient); ).unwrap()), StateUpdate::UpdateClient);
@ -103,6 +111,7 @@ fn test_json_to_state_update_fails() {
#[test] #[test]
fn test_state_update_to_json() { fn test_state_update_to_json() {
println!("{:?}", StateUpdate::StreamRunning(true).to_json());
//Note, this one needs to is dependant on test_json_to_update for correctedness //Note, this one needs to is dependant on test_json_to_update for correctedness
assert_eq!(StateUpdate::StreamRunning(true), (StateUpdate::json_to_state_update(StateUpdate::StreamRunning(true).to_json()))); assert_eq!(StateUpdate::StreamRunning(true), (StateUpdate::json_to_state_update(StateUpdate::StreamRunning(true).to_json())));
} }