Skip to content

Commit

Permalink
Merge pull request #20 from tmtmtoo/tweak
Browse files Browse the repository at this point in the history
refactor: tweak
  • Loading branch information
tmtmtoo authored Jan 5, 2024
2 parents 3b95e2e + eb07472 commit 1c4314c
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 98 deletions.
8 changes: 3 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,7 +17,7 @@ pub enum Transition<N, D> {
Done(D),
}

#[async_trait]
#[async_trait::async_trait]
pub trait StateMachine: Sized {
type Output;

Expand All @@ -39,7 +37,7 @@ pub async fn run<S: StateMachine>(mut machine: S) -> S::Output {
mod tests {
use super::*;

#[async_trait]
#[async_trait::async_trait]
impl StateMachine for u32 {
type Output = u32;

Expand Down
73 changes: 6 additions & 67 deletions src/app/components.rs
Original file line number Diff line number Diff line change
@@ -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<dyn PipedCmdExecutor + Send + Sync>,
}

#[async_trait]
impl super::Component for CmdExecutor {
type Output = Result<Exit>;

async fn handle(&self) -> Self::Output {
let output = self.executor.piped_exec(self.command.as_str()).await?;
Ok(output)
}
}

#[derive(new)]
pub struct PrintableCmdNotFound<C> {
pub command: String,
pub inner: C,
}

#[async_trait]
impl<T: 'static, C: super::Component<Output = Result<T>> + Send + Sync> super::Component
for PrintableCmdNotFound<C>
{
type Output = Result<T>;

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::<Vec<_>>()
.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::*;
18 changes: 18 additions & 0 deletions src/app/components/cmd_executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::app::*;
use crate::exec::*;

#[derive(new)]
pub struct CmdExecutor {
pub command: String,
pub executor: std::sync::Arc<dyn PipedCmdExecutor + Send + Sync>,
}

#[async_trait::async_trait]
impl Component for CmdExecutor {
type Output = anyhow::Result<Exit>;

async fn handle(&self) -> Self::Output {
let output = self.executor.piped_exec(self.command.as_str()).await?;
Ok(output)
}
}
38 changes: 38 additions & 0 deletions src/app/components/cmd_not_found.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::app::*;

#[derive(new)]
pub struct PrintableCmdNotFound<C> {
pub command: String,
pub inner: C,
}

#[async_trait::async_trait]
impl<T: 'static, C: Component<Output = anyhow::Result<T>> + Send + Sync> Component
for PrintableCmdNotFound<C>
{
type Output = anyhow::Result<T>;

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::<Vec<_>>()
.get(0)
.unwrap_or(&"")
)
}
}
_ => (),
};

result
}
}
14 changes: 14 additions & 0 deletions src/app/components/wait.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
19 changes: 9 additions & 10 deletions src/app/retry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::{components::*, *};
use crate::exec::*;
use crate::prelude::*;

pub enum RetryResult {
Success,
Expand All @@ -17,10 +16,10 @@ pub struct RetryApp<E, S> {
count: Option<usize>,
}

#[async_trait]
#[async_trait::async_trait]
impl<E, S> StateMachine for RetryApp<E, S>
where
E: Component<Output = Result<Exit>> + Into<S> + Send + Sync,
E: Component<Output = anyhow::Result<Exit>> + Into<S> + Send + Sync,
S: Component<Output = ()> + Into<E> + Send + Sync,
{
type Output = RetryResult;
Expand Down Expand Up @@ -56,11 +55,11 @@ where
pub struct SharedParams<C> {
command: String,
interval: f64,
executor: Arc<dyn PipedCmdExecutor + Send + Sync>,
executor: std::sync::Arc<dyn PipedCmdExecutor + Send + Sync>,
inner: C,
}

#[async_trait]
#[async_trait::async_trait]
impl<T: 'static, C: Component<Output = T> + Send + Sync> Component for SharedParams<C> {
type Output = T;

Expand Down Expand Up @@ -101,7 +100,7 @@ impl From<SharedParams<WaitSec>> for SharedParams<PrintableCmdNotFound<CmdExecut

impl RetryApp<SharedParams<PrintableCmdNotFound<CmdExecutor>>, SharedParams<WaitSec>> {
pub fn new(command: String, count: Option<usize>, 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(
Expand All @@ -120,20 +119,20 @@ mod tests {
use super::*;

struct TestE {
output: Box<dyn Fn() -> Result<Exit> + Send + Sync>,
output: Box<dyn Fn() -> anyhow::Result<Exit> + Send + Sync>,
}

#[async_trait]
#[async_trait::async_trait]
impl Component for TestE {
type Output = Result<Exit>;
type Output = anyhow::Result<Exit>;
async fn handle(&self) -> Self::Output {
(*self.output)()
}
}

struct TestS;

#[async_trait]
#[async_trait::async_trait]
impl Component for TestS {
type Output = ();
async fn handle(&self) -> Self::Output {
Expand Down
17 changes: 8 additions & 9 deletions src/app/supervise.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::{components::*, *};
use crate::exec::*;
use crate::prelude::*;

enum State<E, S> {
ExecuteCommand(E),
Expand All @@ -12,10 +11,10 @@ pub struct SuperviseApp<E, S> {
count: Option<usize>,
}

#[async_trait]
#[async_trait::async_trait]
impl<E, S> StateMachine for SuperviseApp<E, S>
where
E: Component<Output = Result<Exit>> + Into<S> + Send + Sync,
E: Component<Output = anyhow::Result<Exit>> + Into<S> + Send + Sync,
S: Component<Output = ()> + Into<E> + Send + Sync,
{
type Output = ();
Expand Down Expand Up @@ -51,11 +50,11 @@ where
pub struct SharedParams<C> {
command: String,
interval: f64,
executor: Arc<dyn PipedCmdExecutor + Send + Sync>,
executor: std::sync::Arc<dyn PipedCmdExecutor + Send + Sync>,
inner: C,
}

#[async_trait]
#[async_trait::async_trait]
impl<T: 'static, C: Component<Output = T> + Send + Sync> Component for SharedParams<C> {
type Output = T;

Expand Down Expand Up @@ -96,7 +95,7 @@ impl From<SharedParams<WaitSec>> for SharedParams<PrintableCmdNotFound<CmdExecut

impl SuperviseApp<SharedParams<PrintableCmdNotFound<CmdExecutor>>, SharedParams<WaitSec>> {
pub fn new(command: String, count: Option<usize>, 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(
Expand All @@ -116,17 +115,17 @@ mod tests {

struct TestE;

#[async_trait]
#[async_trait::async_trait]
impl Component for TestE {
type Output = Result<Exit>;
type Output = anyhow::Result<Exit>;
async fn handle(&self) -> Self::Output {
Ok(Exit::new(0))
}
}

struct TestS;

#[async_trait]
#[async_trait::async_trait]
impl Component for TestS {
type Output = ();
async fn handle(&self) -> Self::Output {
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions src/prelude.rs

This file was deleted.

0 comments on commit 1c4314c

Please sign in to comment.