From e3f3f85ef55e05059fc6ca5535f0bce0b56609c6 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Wed, 29 May 2024 00:58:51 +0200 Subject: [PATCH] Refactor the utils (remove them) --- src/handlers/v1/relays.rs | 2 +- src/handlers/v1/ws/controllers/mod.rs | 24 ++++++++++-------------- src/main.rs | 1 - src/utils.rs | 7 ------- 4 files changed, 11 insertions(+), 23 deletions(-) delete mode 100644 src/utils.rs diff --git a/src/handlers/v1/relays.rs b/src/handlers/v1/relays.rs index a03ac1b..525d439 100644 --- a/src/handlers/v1/relays.rs +++ b/src/handlers/v1/relays.rs @@ -20,7 +20,7 @@ pub async fn load_state_for_relay(relay: &mut Relay, app_state: &Addr) Ok(()) } -pub async fn load_state_for_relays(relays: &mut Vec, app_state: &Addr) -> Result<(), EmgauwaError>{ +pub async fn load_state_for_relays(relays: &mut [Relay], app_state: &Addr) -> Result<(), EmgauwaError>{ let stated_relays = get_stated_relays(app_state).await?; relays.iter_mut().for_each(|r| r.find_and_apply_state(&stated_relays)); Ok(()) diff --git a/src/handlers/v1/ws/controllers/mod.rs b/src/handlers/v1/ws/controllers/mod.rs index 9e5c63f..16f92ab 100644 --- a/src/handlers/v1/ws/controllers/mod.rs +++ b/src/handlers/v1/ws/controllers/mod.rs @@ -1,20 +1,20 @@ -mod handlers; - use std::time::Instant; use actix::{Actor, ActorContext, Addr, AsyncContext, Handler, StreamHandler}; use actix_web_actors::ws; use actix_web_actors::ws::ProtocolError; +use futures::executor::block_on; +use sqlx::{Pool, Sqlite}; +use sqlx::pool::PoolConnection; +use ws::Message; + use emgauwa_common::constants::{HEARTBEAT_INTERVAL, HEARTBEAT_TIMEOUT}; use emgauwa_common::errors::EmgauwaError; use emgauwa_common::types::{ControllerWsAction, EmgauwaUid}; -use futures::executor::block_on; -use sqlx::pool::PoolConnection; -use sqlx::{Pool, Sqlite}; -use ws::Message; use crate::app_state::{AppState, DisconnectController}; -use crate::utils::flatten_result; + +mod handlers; pub struct ControllersWs { pub pool: Pool, @@ -32,13 +32,9 @@ impl Actor for ControllersWs { fn stopped(&mut self, _ctx: &mut Self::Context) { if let Some(controller_uid) = &self.controller_uid { - let flat_res = flatten_result( - block_on(self.app_state.send(DisconnectController { - controller_uid: controller_uid.clone(), - })) - .map_err(EmgauwaError::from), - ); - if let Err(err) = flat_res { + if let Err(err) = block_on(self.app_state.send(DisconnectController { + controller_uid: controller_uid.clone(), + })).unwrap_or_else(|err| Err(EmgauwaError::from(err))) { log::error!("Error disconnecting controller: {:?}", err); } } diff --git a/src/main.rs b/src/main.rs index c604d41..d7d80b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,6 @@ use crate::app_state::AppState; mod app_state; mod handlers; mod settings; -mod utils; #[actix_web::main] async fn main() -> Result<(), std::io::Error> { diff --git a/src/utils.rs b/src/utils.rs deleted file mode 100644 index a35de53..0000000 --- a/src/utils.rs +++ /dev/null @@ -1,7 +0,0 @@ -pub fn flatten_result(res: Result, E>) -> Result { - match res { - Ok(Ok(t)) => Ok(t), - Ok(Err(e)) => Err(e), - Err(e) => Err(e), - } -}