From 7cc8b8159943d08d52603c47416a7e7cbff13abd Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Fri, 23 Dec 2022 15:28:19 -0800 Subject: [PATCH] refactor: moved socket_message code to function --- src/main.rs | 115 ++++++++++++++++++++++++---------------------------- 1 file changed, 52 insertions(+), 63 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4f65b3e..9701878 100644 --- a/src/main.rs +++ b/src/main.rs @@ -124,29 +124,21 @@ fn main() { Ok(req) => match req { UIRequest::Play => { sink.play(); - sockets[i] - .write_message( - serde_json::to_string(&ServerResponse { - message: "Player Resumed".into(), - search_results: vec![], - }) - .unwrap() - .into(), - ) - .unwrap(); + write_to_socket( + &mut sockets[i], + "Player Paused".to_string(), + vec![], + ) + .unwrap(); } UIRequest::Pause => { sink.pause(); - sockets[i] - .write_message( - serde_json::to_string(&ServerResponse { - message: "Player Paused".into(), - search_results: vec![], - }) - .unwrap() - .into(), - ) - .unwrap(); + write_to_socket( + &mut sockets[i], + "Player Paused".to_string(), + vec![], + ) + .unwrap(); } UIRequest::Skip(skip_direction) => todo!(), UIRequest::Search(request) => { @@ -161,16 +153,12 @@ fn main() { match items { None => sockets[i].write_message("None".into()).unwrap(), Some(items) => { - sockets[i] - .write_message( - serde_json::to_string(&ServerResponse { - message: "Here are the results:".to_string(), - search_results: items, - }) - .unwrap() - .into(), - ) - .unwrap(); + write_to_socket( + &mut sockets[i], + "Here are the results:".to_string(), + items, + ) + .unwrap(); } } @@ -186,31 +174,21 @@ fn main() { match items { None => { - sockets[i] - .write_message( - serde_json::to_string(&ServerResponse { - message: "No song found with that title!" - .to_string(), - search_results: vec![], - }) - .unwrap() - .into(), - ) - .unwrap(); + write_to_socket( + &mut sockets[i], + "No song found with that title!".to_string(), + vec![], + ) + .unwrap(); } Some(items) => { if items.len() > 1 { - sockets[i] - .write_message( - serde_json::to_string(&ServerResponse { - message: "Please be more specific" - .to_string(), - search_results: items, - }) - .unwrap() - .into(), - ) - .unwrap(); + write_to_socket( + &mut sockets[i], + "Please be more specific".to_string(), + items, + ) + .unwrap(); } else { println!( "Switching song to: '{}'", @@ -228,17 +206,13 @@ fn main() { sink.append(source); println!("{}", items.get(0).unwrap().path.clone()); - sockets[i] - .write_message( - serde_json::to_string(&ServerResponse { - message: "Switching now playing" - .to_string(), - search_results: items, - }) - .unwrap() - .into(), - ) - .unwrap(); + write_to_socket( + &mut sockets[i], + "Switching now playing".to_string(), + items, + ) + .unwrap(); + sink.play(); println!("{}", sink.is_paused()); } @@ -253,3 +227,18 @@ fn main() { } } } + +fn write_to_socket( + socket: &mut WebSocket, + message: String, + results: Vec, +) -> Result<(), tungstenite::Error> { + socket.write_message( + serde_json::to_string(&ServerResponse { + message, + search_results: results, + }) + .unwrap() + .into(), + ) +}