From 7ef26603b888895da8fff495e63af29dff1b347d Mon Sep 17 00:00:00 2001 From: matsu Date: Fri, 5 Jan 2024 05:59:22 +0000 Subject: [PATCH 1/3] refactor: tweak --- src/app.rs | 8 ++-- src/app/components.rs | 73 +++-------------------------- src/app/components/cmd_executor.rs | 17 +++++++ src/app/components/cmd_not_found.rs | 36 ++++++++++++++ src/app/components/wait.rs | 12 +++++ src/app/retry.rs | 19 ++++---- src/app/supervise.rs | 17 ++++--- src/main.rs | 6 +-- src/prelude.rs | 4 -- 9 files changed, 94 insertions(+), 98 deletions(-) create mode 100644 src/app/components/cmd_executor.rs create mode 100644 src/app/components/cmd_not_found.rs create mode 100644 src/app/components/wait.rs delete mode 100644 src/prelude.rs diff --git a/src/app.rs b/src/app.rs index 7cd4728..6a53942 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,12 +2,10 @@ mod components; mod retry; mod supervise; -use crate::prelude::*; - pub use retry::*; pub use supervise::*; -#[async_trait] +#[async_trait::async_trait] pub trait Component { type Output; @@ -19,7 +17,7 @@ pub enum Transition { Done(D), } -#[async_trait] +#[async_trait::async_trait] pub trait StateMachine: Sized { type Output; @@ -39,7 +37,7 @@ pub async fn run(mut machine: S) -> S::Output { mod tests { use super::*; - #[async_trait] + #[async_trait::async_trait] impl StateMachine for u32 { type Output = u32; diff --git a/src/app/components.rs b/src/app/components.rs index 6b47bca..60dd8b8 100644 --- a/src/app/components.rs +++ b/src/app/components.rs @@ -1,68 +1,7 @@ -use crate::exec::*; -use crate::prelude::*; +mod cmd_executor; +mod cmd_not_found; +mod wait; -#[derive(new)] -pub struct CmdExecutor { - pub command: String, - pub executor: Arc, -} - -#[async_trait] -impl super::Component for CmdExecutor { - type Output = Result; - - async fn handle(&self) -> Self::Output { - let output = self.executor.piped_exec(self.command.as_str()).await?; - Ok(output) - } -} - -#[derive(new)] -pub struct PrintableCmdNotFound { - pub command: String, - pub inner: C, -} - -#[async_trait] -impl> + Send + Sync> super::Component - for PrintableCmdNotFound -{ - type Output = Result; - - async fn handle(&self) -> Self::Output { - let result = self.inner.handle().await; - - match &result { - Err(_) => { - if self.command.is_empty() { - eprintln!("cx: no command entered") - } else { - eprintln!( - "cx: command not found '{}'", - self.command - .split(" ") - .collect::>() - .get(0) - .unwrap_or(&"") - ) - } - } - _ => (), - }; - - result - } -} - -pub struct WaitSec { - pub sec: f64, -} - -#[async_trait] -impl super::Component for WaitSec { - type Output = (); - - async fn handle(&self) -> Self::Output { - tokio::time::sleep(tokio::time::Duration::from_secs_f64(self.sec)).await - } -} +pub use cmd_executor::*; +pub use cmd_not_found::*; +pub use wait::*; diff --git a/src/app/components/cmd_executor.rs b/src/app/components/cmd_executor.rs new file mode 100644 index 0000000..dc91eed --- /dev/null +++ b/src/app/components/cmd_executor.rs @@ -0,0 +1,17 @@ +use crate::exec::*; + +#[derive(new)] +pub struct CmdExecutor { + pub command: String, + pub executor: std::sync::Arc, +} + +#[async_trait::async_trait] +impl crate::app::Component for CmdExecutor { + type Output = anyhow::Result; + + async fn handle(&self) -> Self::Output { + let output = self.executor.piped_exec(self.command.as_str()).await?; + Ok(output) + } +} diff --git a/src/app/components/cmd_not_found.rs b/src/app/components/cmd_not_found.rs new file mode 100644 index 0000000..801ec7e --- /dev/null +++ b/src/app/components/cmd_not_found.rs @@ -0,0 +1,36 @@ +#[derive(new)] +pub struct PrintableCmdNotFound { + pub command: String, + pub inner: C, +} + +#[async_trait::async_trait] +impl> + Send + Sync> + crate::app::Component for PrintableCmdNotFound +{ + type Output = anyhow::Result; + + async fn handle(&self) -> Self::Output { + let result = self.inner.handle().await; + + match &result { + Err(_) => { + if self.command.is_empty() { + eprintln!("cx: no command entered") + } else { + eprintln!( + "cx: command not found '{}'", + self.command + .split(" ") + .collect::>() + .get(0) + .unwrap_or(&"") + ) + } + } + _ => (), + }; + + result + } +} diff --git a/src/app/components/wait.rs b/src/app/components/wait.rs new file mode 100644 index 0000000..e951e75 --- /dev/null +++ b/src/app/components/wait.rs @@ -0,0 +1,12 @@ +pub struct WaitSec { + pub sec: f64, +} + +#[async_trait::async_trait] +impl crate::app::Component for WaitSec { + type Output = (); + + async fn handle(&self) -> Self::Output { + tokio::time::sleep(tokio::time::Duration::from_secs_f64(self.sec)).await + } +} diff --git a/src/app/retry.rs b/src/app/retry.rs index 00909e4..73c3224 100644 --- a/src/app/retry.rs +++ b/src/app/retry.rs @@ -1,6 +1,5 @@ use super::{components::*, *}; use crate::exec::*; -use crate::prelude::*; pub enum RetryResult { Success, @@ -17,10 +16,10 @@ pub struct RetryApp { count: Option, } -#[async_trait] +#[async_trait::async_trait] impl StateMachine for RetryApp where - E: Component> + Into + Send + Sync, + E: Component> + Into + Send + Sync, S: Component + Into + Send + Sync, { type Output = RetryResult; @@ -56,11 +55,11 @@ where pub struct SharedParams { command: String, interval: f64, - executor: Arc, + executor: std::sync::Arc, inner: C, } -#[async_trait] +#[async_trait::async_trait] impl + Send + Sync> Component for SharedParams { type Output = T; @@ -101,7 +100,7 @@ impl From> for SharedParams>, SharedParams> { pub fn new(command: String, count: Option, interval: f64) -> Self { - let executor = Arc::new(tokio_impl::TokioPipedCmdExecutor); + let executor = std::sync::Arc::new(tokio_impl::TokioPipedCmdExecutor); Self { state: State::ExecuteCommand(SharedParams::new( @@ -120,12 +119,12 @@ mod tests { use super::*; struct TestE { - output: Box Result + Send + Sync>, + output: Box anyhow::Result + Send + Sync>, } - #[async_trait] + #[async_trait::async_trait] impl Component for TestE { - type Output = Result; + type Output = anyhow::Result; async fn handle(&self) -> Self::Output { (*self.output)() } @@ -133,7 +132,7 @@ mod tests { struct TestS; - #[async_trait] + #[async_trait::async_trait] impl Component for TestS { type Output = (); async fn handle(&self) -> Self::Output { diff --git a/src/app/supervise.rs b/src/app/supervise.rs index d9a4517..3b53db9 100644 --- a/src/app/supervise.rs +++ b/src/app/supervise.rs @@ -1,6 +1,5 @@ use super::{components::*, *}; use crate::exec::*; -use crate::prelude::*; enum State { ExecuteCommand(E), @@ -12,10 +11,10 @@ pub struct SuperviseApp { count: Option, } -#[async_trait] +#[async_trait::async_trait] impl StateMachine for SuperviseApp where - E: Component> + Into + Send + Sync, + E: Component> + Into + Send + Sync, S: Component + Into + Send + Sync, { type Output = (); @@ -51,11 +50,11 @@ where pub struct SharedParams { command: String, interval: f64, - executor: Arc, + executor: std::sync::Arc, inner: C, } -#[async_trait] +#[async_trait::async_trait] impl + Send + Sync> Component for SharedParams { type Output = T; @@ -96,7 +95,7 @@ impl From> for SharedParams>, SharedParams> { pub fn new(command: String, count: Option, interval: f64) -> Self { - let executor = Arc::new(tokio_impl::TokioPipedCmdExecutor); + let executor = std::sync::Arc::new(tokio_impl::TokioPipedCmdExecutor); Self { state: State::ExecuteCommand(SharedParams::new( @@ -116,9 +115,9 @@ mod tests { struct TestE; - #[async_trait] + #[async_trait::async_trait] impl Component for TestE { - type Output = Result; + type Output = anyhow::Result; async fn handle(&self) -> Self::Output { Ok(Exit::new(0)) } @@ -126,7 +125,7 @@ mod tests { struct TestS; - #[async_trait] + #[async_trait::async_trait] impl Component for TestS { type Output = (); async fn handle(&self) -> Self::Output { diff --git a/src/main.rs b/src/main.rs index ad5bfef..699a304 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,15 +23,15 @@ extern crate derive_new; mod app; mod config; mod exec; -mod prelude; use app::*; use config::*; -use prelude::*; -use structopt::StructOpt; #[tokio::main] async fn main() { + use futures::{future::Either, FutureExt}; + use structopt::StructOpt; + let config = Config::from_args(); let state_machine = match config { diff --git a/src/prelude.rs b/src/prelude.rs deleted file mode 100644 index c53e269..0000000 --- a/src/prelude.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub use anyhow::*; -pub use async_trait::*; -pub use futures::{future::Either, FutureExt}; -pub use std::sync::Arc; From 34cafd1bce45a2e38af1cbcd5dcce4c6c1786803 Mon Sep 17 00:00:00 2001 From: matsu Date: Fri, 5 Jan 2024 06:06:32 +0000 Subject: [PATCH 2/3] refactor: tweak --- src/app/components/cmd_executor.rs | 3 ++- src/app/components/cmd_not_found.rs | 6 ++++-- src/app/components/wait.rs | 4 +++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/app/components/cmd_executor.rs b/src/app/components/cmd_executor.rs index dc91eed..135fc90 100644 --- a/src/app/components/cmd_executor.rs +++ b/src/app/components/cmd_executor.rs @@ -1,4 +1,5 @@ use crate::exec::*; +use crate::app::*; #[derive(new)] pub struct CmdExecutor { @@ -7,7 +8,7 @@ pub struct CmdExecutor { } #[async_trait::async_trait] -impl crate::app::Component for CmdExecutor { +impl Component for CmdExecutor { type Output = anyhow::Result; async fn handle(&self) -> Self::Output { diff --git a/src/app/components/cmd_not_found.rs b/src/app/components/cmd_not_found.rs index 801ec7e..e8c1eba 100644 --- a/src/app/components/cmd_not_found.rs +++ b/src/app/components/cmd_not_found.rs @@ -1,3 +1,5 @@ +use crate::app::*; + #[derive(new)] pub struct PrintableCmdNotFound { pub command: String, @@ -5,8 +7,8 @@ pub struct PrintableCmdNotFound { } #[async_trait::async_trait] -impl> + Send + Sync> - crate::app::Component for PrintableCmdNotFound +impl> + Send + Sync> + Component for PrintableCmdNotFound { type Output = anyhow::Result; diff --git a/src/app/components/wait.rs b/src/app/components/wait.rs index e951e75..7c24242 100644 --- a/src/app/components/wait.rs +++ b/src/app/components/wait.rs @@ -1,9 +1,11 @@ +use crate::app::*; + pub struct WaitSec { pub sec: f64, } #[async_trait::async_trait] -impl crate::app::Component for WaitSec { +impl Component for WaitSec { type Output = (); async fn handle(&self) -> Self::Output { From eb074725035e717cbcabcb7a22c2c163f0e131fa Mon Sep 17 00:00:00 2001 From: matsu Date: Fri, 5 Jan 2024 06:07:45 +0000 Subject: [PATCH 3/3] chore: cargo fmt --- src/app/components/cmd_executor.rs | 2 +- src/app/components/cmd_not_found.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app/components/cmd_executor.rs b/src/app/components/cmd_executor.rs index 135fc90..e3f3c8c 100644 --- a/src/app/components/cmd_executor.rs +++ b/src/app/components/cmd_executor.rs @@ -1,5 +1,5 @@ -use crate::exec::*; use crate::app::*; +use crate::exec::*; #[derive(new)] pub struct CmdExecutor { diff --git a/src/app/components/cmd_not_found.rs b/src/app/components/cmd_not_found.rs index e8c1eba..51ff2ff 100644 --- a/src/app/components/cmd_not_found.rs +++ b/src/app/components/cmd_not_found.rs @@ -7,8 +7,8 @@ pub struct PrintableCmdNotFound { } #[async_trait::async_trait] -impl> + Send + Sync> - Component for PrintableCmdNotFound +impl> + Send + Sync> Component + for PrintableCmdNotFound { type Output = anyhow::Result;