From 05ab9aac71bcc23a8fe7959876fbfdfde9b37ae6 Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Sun, 8 Oct 2023 20:46:27 +0200 Subject: [PATCH] backport: fix clippy warnings --- ffplayout-api/src/utils/control.rs | 69 +++++++++++++++++------------- 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/ffplayout-api/src/utils/control.rs b/ffplayout-api/src/utils/control.rs index bece19a3..09849bfb 100644 --- a/ffplayout-api/src/utils/control.rs +++ b/ffplayout-api/src/utils/control.rs @@ -296,45 +296,56 @@ pub async fn media_info( post_request(conn, id, json_obj).await } +async fn execute_systemd( + conn: &Pool, + id: i32, + command: &ServiceCmd, +) -> Result { + let system_d = SystemD::new(conn, id).await?; + match command { + ServiceCmd::Enable => system_d.enable(), + ServiceCmd::Disable => system_d.disable(), + ServiceCmd::Start => system_d.start(), + ServiceCmd::Stop => system_d.stop(), + ServiceCmd::Restart => system_d.restart(), + ServiceCmd::Status => system_d.status().await, + } +} + pub async fn control_service( conn: &Pool, id: i32, command: &ServiceCmd, engine: Option>, ) -> Result { - if engine.is_some() && engine.as_ref().unwrap().piggyback.load(Ordering::SeqCst) { - match command { - ServiceCmd::Start => engine.unwrap().start().await, - ServiceCmd::Stop => { - if control_state(conn, id, "stop_all").await.is_ok() { - engine.unwrap().stop().await - } else { - Err(ServiceError::NoContent("Nothing to stop".to_string())) + if let Some(en) = engine { + if en.piggyback.load(Ordering::SeqCst) { + match command { + ServiceCmd::Start => en.start().await, + ServiceCmd::Stop => { + if control_state(conn, id, "stop_all").await.is_ok() { + en.stop().await + } else { + Err(ServiceError::NoContent("Nothing to stop".to_string())) + } } - } - ServiceCmd::Restart => { - if control_state(conn, id, "stop_all").await.is_ok() { - engine.unwrap().restart().await - } else { - Err(ServiceError::NoContent("Nothing to stop".to_string())) + ServiceCmd::Restart => { + if control_state(conn, id, "stop_all").await.is_ok() { + en.restart().await + } else { + Err(ServiceError::NoContent("Nothing to restart".to_string())) + } } + ServiceCmd::Status => en.status(), + _ => Err(ServiceError::Conflict( + "Engine runs in piggyback mode, in this mode this command is not allowed." + .to_string(), + )), } - ServiceCmd::Status => engine.unwrap().status(), - _ => Err(ServiceError::Conflict( - "Engine runs in piggyback mode, in this mode this command is not allowed." - .to_string(), - )), + } else { + execute_systemd(conn, id, command).await } } else { - let system_d = SystemD::new(conn, id).await?; - - match command { - ServiceCmd::Enable => system_d.enable(), - ServiceCmd::Disable => system_d.disable(), - ServiceCmd::Start => system_d.start(), - ServiceCmd::Stop => system_d.stop(), - ServiceCmd::Restart => system_d.restart(), - ServiceCmd::Status => system_d.status().await, - } + execute_systemd(conn, id, command).await } }