From 8d532ffd82c963524c98eac7d93d59b7bf739f9a Mon Sep 17 00:00:00 2001 From: Nickiel12 <35903114+Nickiel12@users.noreply.github.com> Date: Sat, 6 Apr 2024 11:05:29 -0700 Subject: [PATCH] cargo fmt --- src/config.rs | 24 +++++++--------- src/coordinator.rs | 66 +++++++++++++++++++++++++++----------------- src/joystick_loop.rs | 15 +++++----- src/main.rs | 1 - src/socket_loop.rs | 35 ++++++++++++----------- src/ui_code.rs | 13 +++++---- 6 files changed, 84 insertions(+), 70 deletions(-) diff --git a/src/config.rs b/src/config.rs index a06a1a7..9676bb6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,7 @@ use config::{Config, FileFormat}; +use log::{error, info}; use std::fs::File; use std::io::Write; -use log::{error, info}; use crate::ui_code::AppState; @@ -23,23 +23,19 @@ pub fn load_config() -> AppState { pub fn save_config(config: &AppState) { match toml::to_string(&config) { - Ok(toml_str) => { - match File::create("./settings.toml") { - Ok(mut file) => { - match file.write_all(toml_str.as_bytes()) { - Ok(_) => { - info!("Config file saved succesfully"); - } - Err(e) => { - error!("Couldn't write config file contents to open file: {e}"); - } - } + Ok(toml_str) => match File::create("./settings.toml") { + Ok(mut file) => match file.write_all(toml_str.as_bytes()) { + Ok(_) => { + info!("Config file saved succesfully"); } Err(e) => { - error!("Couldn't open settings file: {e}"); + error!("Couldn't write config file contents to open file: {e}"); } + }, + Err(e) => { + error!("Couldn't open settings file: {e}"); } - } + }, Err(e) => { error!("Could not serialize app state: {e}"); } diff --git a/src/coordinator.rs b/src/coordinator.rs index 5557c31..e786f4e 100644 --- a/src/coordinator.rs +++ b/src/coordinator.rs @@ -1,5 +1,8 @@ -use std::sync::{atomic::{AtomicBool, Ordering}, Arc}; use std::pin::{pin, Pin}; +use std::sync::{ + atomic::{AtomicBool, Ordering}, + Arc, +}; use async_channel::{Receiver, Sender}; use futures_util::stream::StreamExt; @@ -34,8 +37,13 @@ struct CoordState<'a> { pub rt: Handle, } -impl <'a> CoordState<'a> { - pub fn new(mec: Pin<&'a mut Receiver>, to_mec: Sender, to_gui: Sender, rt: Handle) -> Self { +impl<'a> CoordState<'a> { + pub fn new( + mec: Pin<&'a mut Receiver>, + to_mec: Sender, + to_gui: Sender, + rt: Handle, + ) -> Self { CoordState { to_socket: None, @@ -43,7 +51,10 @@ impl <'a> CoordState<'a> { sck_recv_alive: Arc::new(AtomicBool::new(false)), joystick_loop_alive: Arc::new(AtomicBool::new(false)), - mec, to_mec, to_gui, rt + mec, + to_mec, + to_gui, + rt, } } @@ -60,8 +71,9 @@ impl <'a> CoordState<'a> { } pub async fn socket_start(&mut self, conn: String) { - if !(self.sck_recv_alive.load(Ordering::SeqCst) && self.sck_send_alive.load(Ordering::SeqCst)) { - + if !(self.sck_recv_alive.load(Ordering::SeqCst) + && self.sck_send_alive.load(Ordering::SeqCst)) + { info!("Starting socket"); let (to_socket, socket_sender_rx) = async_channel::bounded::(10); @@ -73,17 +85,23 @@ impl <'a> CoordState<'a> { socket_sender_rx, self.sck_send_alive.clone(), self.sck_recv_alive.clone(), - self.rt.clone() - ).await; + self.rt.clone(), + ) + .await; } } pub async fn check_states(&mut self) { if !self.joystick_loop_alive.load(Ordering::SeqCst) { - self.rt.spawn(joystick_loop(self.to_mec.clone(), self.joystick_loop_alive.clone())); + self.rt.spawn(joystick_loop( + self.to_mec.clone(), + self.joystick_loop_alive.clone(), + )); } - if !self.sck_recv_alive.load(Ordering::SeqCst) || !self.sck_send_alive.load(Ordering::SeqCst) { + if !self.sck_recv_alive.load(Ordering::SeqCst) + || !self.sck_send_alive.load(Ordering::SeqCst) + { self.socket_close().await; if let Err(e) = self.to_gui.send(GuiUpdate::SocketState(false)).await { @@ -105,29 +123,28 @@ impl <'a> CoordState<'a> { } pub async fn socket_close(&mut self) { - self.sck_send_alive.store(false, Ordering::SeqCst); - self.sck_recv_alive.store(false, Ordering::SeqCst); - if let Some(tx) = self.to_socket.take() { - tx.close(); - } + self.sck_send_alive.store(false, Ordering::SeqCst); + self.sck_recv_alive.store(false, Ordering::SeqCst); + if let Some(tx) = self.to_socket.take() { + tx.close(); + } } } // Main_Event_Channel -pub async fn start_coordinator(mec: Receiver, to_mec: Sender, to_gui: Sender, runtime: Handle) { - info!("Starting coordinator!"); +pub async fn start_coordinator( + mec: Receiver, + to_mec: Sender, + to_gui: Sender, + runtime: Handle, +) { + info!("Starting coordinator!"); let mec = pin!(mec); - let mut state = CoordState::new( - mec, - to_mec, - to_gui, - runtime - ); + let mut state = CoordState::new(mec, to_mec, to_gui, runtime); while let Some(msg) = state.mec.next().await { - state.check_states().await; match msg { @@ -169,5 +186,4 @@ pub async fn start_coordinator(mec: Receiver, to_mec: Sender, - is_alive: Arc -) { +pub async fn joystick_loop(tx: Sender, is_alive: Arc) { let mut gilrs = GilrsBuilder::new().set_update_state(false).build().unwrap(); is_alive.store(true, std::sync::atomic::Ordering::SeqCst); @@ -88,16 +85,18 @@ pub async fn joystick_loop( count_zeros = 0; } - match tx.try_send(ApplicationEvent::MoveEvent(MoveEvent { + match tx.try_send(ApplicationEvent::MoveEvent(MoveEvent { x: curr_x, y: curr_y, })) { Ok(_) => {} Err(async_channel::TrySendError::Closed(_)) => { info!("MEC is closed, stopping Joystick loop"); - break + break; + } + Err(async_channel::TrySendError::Full(_)) => { + warn!("[joystick loop] The MEC is full!") } - Err(async_channel::TrySendError::Full(_)) => {warn!("[joystick loop] The MEC is full!")} } } diff --git a/src/main.rs b/src/main.rs index 164db4b..0d88d56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,6 @@ mod socket_loop; mod ui_code; const APP_ID: &str = "net.nickiel.joystick-controller-client"; - fn main() -> glib::ExitCode { env::set_var("gtk_csd", "0"); diff --git a/src/socket_loop.rs b/src/socket_loop.rs index 7c6bb07..ffd8b7b 100644 --- a/src/socket_loop.rs +++ b/src/socket_loop.rs @@ -1,4 +1,7 @@ -use std::sync::{atomic::{AtomicBool, Ordering}, Arc}; +use std::sync::{ + atomic::{AtomicBool, Ordering}, + Arc, +}; use async_channel::{Receiver, Sender}; use futures_util::{ @@ -17,7 +20,7 @@ async fn socket_listen( mut reader: SplitStream>>, ) { if socket_recv_is_alive.load(std::sync::atomic::Ordering::SeqCst) { - while let Some(msg) = reader.next().await { + while let Some(msg) = reader.next().await { match msg { Ok(val) => { if let Err(e) = mec.send(ApplicationEvent::SocketMessage(val)).await { @@ -25,11 +28,11 @@ async fn socket_listen( break; } } - Err(e) => { + Err(e) => { error!("Websocket error: {:#?}", e); } } - }; + } socket_recv_is_alive.store(false, Ordering::SeqCst); } @@ -69,24 +72,24 @@ pub async fn socket_loop( send_to_socket: Receiver, socket_send_is_alive: Arc, socket_recv_is_alive: Arc, - rt: Handle + rt: Handle, ) { info!("Starting Socket Loop"); - socket_send_is_alive.store(true, Ordering::SeqCst); socket_recv_is_alive.store(true, Ordering::SeqCst); - let socket: Option>> = match connect_async(connection_string).await { - Ok((val, _)) => { - info!("Socket connection made successfully"); - Some(val) - } - Err(_) => { - error!("Couldn't connect to URL"); - None - } - }; + let socket: Option>> = + match connect_async(connection_string).await { + Ok((val, _)) => { + info!("Socket connection made successfully"); + Some(val) + } + Err(_) => { + error!("Couldn't connect to URL"); + None + } + }; if let Some(sckt) = socket { let (outbound, inbound) = sckt.split(); diff --git a/src/ui_code.rs b/src/ui_code.rs index 8a54313..9715925 100644 --- a/src/ui_code.rs +++ b/src/ui_code.rs @@ -9,7 +9,6 @@ use crate::config::{load_config, save_config}; // use crate::{joystick_loop, JoystickThreadUpdate}; use crate::coordinator::{start_coordinator, ApplicationEvent, MoveEvent}; - #[derive(Debug, Serialize, Deserialize, Clone)] pub struct AppState { pub ip: String, @@ -38,8 +37,12 @@ pub fn build_ui(app: &Application, runtime: Handle) { let (to_mec, mec) = async_channel::unbounded::(); let (to_gui, gui_recv) = async_channel::bounded::(10); - runtime.spawn(start_coordinator(mec, to_mec.clone(), to_gui, runtime.clone())); - + runtime.spawn(start_coordinator( + mec, + to_mec.clone(), + to_gui, + runtime.clone(), + )); // let conn_status_label = Label::new(Some(&"No Connection".to_string())); let conn_status_label = Label::builder() @@ -157,9 +160,7 @@ pub fn build_ui(app: &Application, runtime: Handle) { .child(&main_box) .build(); - window.connect_close_request(move |_| { - glib::Propagation::Proceed - }); + window.connect_close_request(move |_| glib::Propagation::Proceed); // Present window window.present();