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..e3f3c8c --- /dev/null +++ b/src/app/components/cmd_executor.rs @@ -0,0 +1,18 @@ +use crate::app::*; +use crate::exec::*; + +#[derive(new)] +pub struct CmdExecutor { + pub command: String, + pub executor: std::sync::Arc, +} + +#[async_trait::async_trait] +impl 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..51ff2ff --- /dev/null +++ b/src/app/components/cmd_not_found.rs @@ -0,0 +1,38 @@ +use crate::app::*; + +#[derive(new)] +pub struct PrintableCmdNotFound { + pub command: String, + pub inner: C, +} + +#[async_trait::async_trait] +impl> + Send + Sync> 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..7c24242 --- /dev/null +++ b/src/app/components/wait.rs @@ -0,0 +1,14 @@ +use crate::app::*; + +pub struct WaitSec { + pub sec: f64, +} + +#[async_trait::async_trait] +impl 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;