From 5314d201a438aa31861e7685fba5eb4e6686145e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Saparelli?= Date: Mon, 23 Aug 2021 02:36:58 +1200 Subject: [PATCH] Expand print_out example into watchexec test case --- lib/examples/print_out.rs | 28 ++++++++++++++++++++++------ lib/src/watchexec.rs | 14 ++++++++++---- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/examples/print_out.rs b/lib/examples/print_out.rs index 5edf48cf..d803c95e 100644 --- a/lib/examples/print_out.rs +++ b/lib/examples/print_out.rs @@ -1,8 +1,9 @@ -use std::time::Duration; +use std::convert::Infallible; -use tokio::time::sleep; use watchexec::{ + action::{Action, Outcome}, config::{InitConfigBuilder, RuntimeConfig}, + signal::Signal, Watchexec, }; @@ -18,12 +19,27 @@ async fn main() -> color_eyre::eyre::Result<()> { Ok::<(), std::convert::Infallible>(()) }); - let runtime = RuntimeConfig::default(); + let mut runtime = RuntimeConfig::default(); + runtime.command(["date"]); + runtime.on_action(|action: Action| async move { + eprintln!("Watchexec Action: {:?}", action); + + if action + .events + .iter() + .flat_map(|event| event.signals()) + .any(|sig| sig == Signal::Interrupt) + { + action.outcome(Outcome::Exit); + } else { + action.outcome(Outcome::both(Outcome::Stop, Outcome::Start)); + } + + Ok::<(), Infallible>(()) + }); let wx = Watchexec::new(init.build()?, runtime)?; - wx.main(); - - sleep(Duration::from_secs(1)).await; + wx.main().await??; Ok(()) } diff --git a/lib/src/watchexec.rs b/lib/src/watchexec.rs index dade8a84..c153ea9b 100644 --- a/lib/src/watchexec.rs +++ b/lib/src/watchexec.rs @@ -77,10 +77,16 @@ impl Watchexec { let error_hook = subtask!(error_hook, error_hook(er_r, eh)); - try_join!(action, error_hook, fs, signal).map(drop).or_else(|e| if matches!(e, CriticalError::Exit) { - trace!("got graceful exit request via critical error, erasing the error"); - Ok(()) - } else { Err(e) }) + try_join!(action, error_hook, fs, signal) + .map(drop) + .or_else(|e| { + if matches!(e, CriticalError::Exit) { + trace!("got graceful exit request via critical error, erasing the error"); + Ok(()) + } else { + Err(e) + } + }) }); trace!("done with setup");