2021-08-22 12:05:09 +02:00
|
|
|
//! Configuration and builders for [`crate::Watchexec`].
|
|
|
|
|
|
|
|
use std::{fmt, path::Path, sync::Arc, time::Duration};
|
2021-08-21 10:46:44 +02:00
|
|
|
|
2021-08-22 10:49:24 +02:00
|
|
|
use atomic_take::AtomicTake;
|
2021-08-19 11:28:56 +02:00
|
|
|
|
2021-08-24 18:41:14 +02:00
|
|
|
use crate::{
|
|
|
|
action::{Action, PostSpawn, PreSpawn},
|
|
|
|
command::Shell,
|
|
|
|
error::RuntimeError,
|
2021-09-28 11:23:23 +02:00
|
|
|
filter::Filterer,
|
2021-08-24 18:41:14 +02:00
|
|
|
fs::Watcher,
|
|
|
|
handler::Handler,
|
|
|
|
};
|
2021-08-21 10:46:44 +02:00
|
|
|
|
|
|
|
/// Runtime configuration for [`Watchexec`][crate::Watchexec].
|
2021-08-19 11:28:56 +02:00
|
|
|
///
|
2021-08-21 10:46:44 +02:00
|
|
|
/// This is used both when constructing the instance (as initial configuration) and to reconfigure
|
2021-08-22 14:31:39 +02:00
|
|
|
/// it at runtime via [`Watchexec::reconfigure()`][crate::Watchexec::reconfigure()].
|
2021-08-19 11:28:56 +02:00
|
|
|
///
|
2021-08-22 10:49:24 +02:00
|
|
|
/// Use [`RuntimeConfig::default()`] to build a new one, or modify an existing one. This struct is
|
|
|
|
/// marked non-exhaustive such that new options may be added without breaking change. You can make
|
|
|
|
/// changes through the fields directly, or use the convenience (chainable!) methods instead.
|
2021-10-16 14:22:55 +02:00
|
|
|
///
|
|
|
|
/// You should see the detailed documentation on [fs::WorkingData][crate::fs::WorkingData] and
|
|
|
|
/// [action::WorkingData][crate::action::WorkingData] for important information and particulars
|
|
|
|
/// about each field, especially the handlers.
|
2021-08-22 10:49:24 +02:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2021-08-19 11:28:56 +02:00
|
|
|
#[non_exhaustive]
|
2021-08-21 10:46:44 +02:00
|
|
|
pub struct RuntimeConfig {
|
2021-08-19 11:28:56 +02:00
|
|
|
/// Working data for the filesystem event source.
|
|
|
|
///
|
|
|
|
/// This notably includes the path set to be watched.
|
2021-08-18 15:12:50 +02:00
|
|
|
pub fs: crate::fs::WorkingData,
|
2021-08-19 11:28:56 +02:00
|
|
|
|
2021-08-20 18:43:55 +02:00
|
|
|
/// Working data for the action processing.
|
|
|
|
///
|
|
|
|
/// This is the task responsible for scheduling the actions in response to events, applying the
|
|
|
|
/// filtering, etc.
|
|
|
|
pub action: crate::action::WorkingData,
|
2021-08-21 10:46:44 +02:00
|
|
|
}
|
2021-08-20 18:43:55 +02:00
|
|
|
|
2021-08-22 12:05:09 +02:00
|
|
|
impl RuntimeConfig {
|
|
|
|
/// Set the pathset to be watched.
|
|
|
|
pub fn pathset<I, P>(&mut self, pathset: I) -> &mut Self
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item = P>,
|
|
|
|
P: AsRef<Path>,
|
|
|
|
{
|
|
|
|
self.fs.pathset = pathset.into_iter().map(|p| p.as_ref().into()).collect();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the file watcher type to use.
|
|
|
|
pub fn file_watcher(&mut self, watcher: Watcher) -> &mut Self {
|
|
|
|
self.fs.watcher = watcher;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the action throttle.
|
|
|
|
pub fn action_throttle(&mut self, throttle: impl Into<Duration>) -> &mut Self {
|
|
|
|
self.action.throttle = throttle.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-08-22 12:06:31 +02:00
|
|
|
/// Set the shell to use to invoke commands.
|
|
|
|
pub fn command_shell(&mut self, shell: Shell) -> &mut Self {
|
|
|
|
self.action.shell = shell;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-08-22 14:27:45 +02:00
|
|
|
/// Toggle whether to use process groups or not.
|
|
|
|
pub fn command_grouped(&mut self, grouped: bool) -> &mut Self {
|
|
|
|
self.action.grouped = grouped;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-08-22 12:06:31 +02:00
|
|
|
/// Set the command to run on action.
|
2021-08-24 10:14:01 +02:00
|
|
|
pub fn command<I, S>(&mut self, command: I) -> &mut Self
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item = S>,
|
|
|
|
S: AsRef<str>,
|
|
|
|
{
|
|
|
|
self.action.command = command.into_iter().map(|c| c.as_ref().to_owned()).collect();
|
2021-08-22 12:06:31 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-10-16 14:22:55 +02:00
|
|
|
/// Set the filterer implementation to use.
|
2021-09-28 11:23:23 +02:00
|
|
|
pub fn filterer(&mut self, filterer: Arc<dyn Filterer>) -> &mut Self {
|
|
|
|
self.action.filterer = filterer;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-08-22 12:05:09 +02:00
|
|
|
/// Set the action handler.
|
|
|
|
pub fn on_action(&mut self, handler: impl Handler<Action> + Send + 'static) -> &mut Self {
|
|
|
|
self.action.action_handler = Arc::new(AtomicTake::new(Box::new(handler) as _));
|
|
|
|
self
|
|
|
|
}
|
2021-08-22 17:59:02 +02:00
|
|
|
|
|
|
|
/// Keep the action handler the same.
|
|
|
|
///
|
|
|
|
/// This is especially useful when reconfiguring _within_ the action handler.
|
|
|
|
///
|
2021-10-16 14:22:55 +02:00
|
|
|
/// Passing this config to [`Watchexec::new()`][crate::Watchexec::new()] will cause a
|
|
|
|
/// [`CriticalError::MissingHandler`][crate::error::CriticalError::MissingHandler].
|
2021-08-22 17:59:02 +02:00
|
|
|
pub fn keep_action(&mut self) -> &mut Self {
|
|
|
|
self.action.action_handler = Arc::new(AtomicTake::empty());
|
|
|
|
self
|
|
|
|
}
|
2021-08-24 18:41:14 +02:00
|
|
|
|
|
|
|
/// Set the pre-spawn handler.
|
|
|
|
pub fn on_pre_spawn(&mut self, handler: impl Handler<PreSpawn> + Send + 'static) -> &mut Self {
|
|
|
|
self.action.pre_spawn_handler = Arc::new(AtomicTake::new(Box::new(handler) as _));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Keep the pre-spawn handler the same.
|
|
|
|
///
|
|
|
|
/// This is especially useful when reconfiguring _within_ the action handler.
|
|
|
|
///
|
2021-10-16 14:22:55 +02:00
|
|
|
/// Passing this config to [`Watchexec::new()`][crate::Watchexec::new()] will cause a
|
|
|
|
/// [`CriticalError::MissingHandler`][crate::error::CriticalError::MissingHandler].
|
2021-08-24 18:41:14 +02:00
|
|
|
pub fn keep_pre_spawn(&mut self) -> &mut Self {
|
|
|
|
self.action.pre_spawn_handler = Arc::new(AtomicTake::empty());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the post-spawn handler.
|
|
|
|
pub fn on_post_spawn(
|
|
|
|
&mut self,
|
|
|
|
handler: impl Handler<PostSpawn> + Send + 'static,
|
|
|
|
) -> &mut Self {
|
|
|
|
self.action.post_spawn_handler = Arc::new(AtomicTake::new(Box::new(handler) as _));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Keep the post-spawn handler the same.
|
|
|
|
///
|
|
|
|
/// This is especially useful when reconfiguring _within_ the action handler.
|
|
|
|
///
|
2021-10-16 14:22:55 +02:00
|
|
|
/// Passing this config to [`Watchexec::new()`][crate::Watchexec::new()] will cause a
|
|
|
|
/// [`CriticalError::MissingHandler`][crate::error::CriticalError::MissingHandler].
|
2021-08-24 18:41:14 +02:00
|
|
|
pub fn keep_post_spawn(&mut self) -> &mut Self {
|
|
|
|
self.action.post_spawn_handler = Arc::new(AtomicTake::empty());
|
|
|
|
self
|
|
|
|
}
|
2021-08-22 12:05:09 +02:00
|
|
|
}
|
|
|
|
|
2021-08-21 10:46:44 +02:00
|
|
|
/// Initialisation configuration for [`Watchexec`][crate::Watchexec].
|
|
|
|
///
|
|
|
|
/// This is used only for constructing the instance.
|
|
|
|
///
|
2021-10-09 07:37:13 +02:00
|
|
|
/// Use [`InitConfig::default()`] to build a new one, and the inherent methods to change values.
|
|
|
|
/// This struct is marked non-exhaustive such that new options may be added without breaking change.
|
2021-08-21 10:46:44 +02:00
|
|
|
#[non_exhaustive]
|
|
|
|
pub struct InitConfig {
|
|
|
|
/// Runtime error handler.
|
2021-08-19 11:28:56 +02:00
|
|
|
///
|
2021-08-21 10:46:44 +02:00
|
|
|
/// This is run on every runtime error that occurs within watchexec. By default the placeholder
|
|
|
|
/// `()` handler is used, which discards all errors.
|
|
|
|
///
|
|
|
|
/// If the handler errors, [_that_ error][crate::error::RuntimeError::Handler] is immediately
|
2021-08-22 12:05:09 +02:00
|
|
|
/// given to the handler. If this second handler call errors as well, its error is ignored.
|
2021-08-19 11:28:56 +02:00
|
|
|
///
|
2021-08-21 11:12:40 +02:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use std::convert::Infallible;
|
2021-10-09 07:37:13 +02:00
|
|
|
/// # use watchexec::config::InitConfig;
|
|
|
|
/// let mut init = InitConfig::default();
|
2021-08-22 12:05:09 +02:00
|
|
|
/// init.on_error(|err| async move {
|
|
|
|
/// tracing::error!("{}", err);
|
|
|
|
/// Ok::<(), Infallible>(())
|
|
|
|
/// });
|
2021-08-21 11:12:40 +02:00
|
|
|
/// ```
|
2021-08-21 10:46:44 +02:00
|
|
|
pub error_handler: Box<dyn Handler<RuntimeError> + Send>,
|
|
|
|
|
|
|
|
/// Internal: the buffer size of the channel which carries runtime errors.
|
|
|
|
///
|
|
|
|
/// The default (64) is usually fine. If you expect a much larger throughput of runtime errors,
|
|
|
|
/// or if your `error_handler` is slow, adjusting this value may help.
|
2021-08-19 11:28:56 +02:00
|
|
|
pub error_channel_size: usize,
|
|
|
|
|
|
|
|
/// Internal: the buffer size of the channel which carries events.
|
|
|
|
///
|
|
|
|
/// The default (1024) is usually fine. If you expect a much larger throughput of events,
|
|
|
|
/// adjusting this value may help.
|
|
|
|
pub event_channel_size: usize,
|
2021-08-18 15:12:50 +02:00
|
|
|
}
|
2021-08-21 10:46:44 +02:00
|
|
|
|
2021-10-09 07:37:13 +02:00
|
|
|
impl Default for InitConfig {
|
2021-10-10 05:03:05 +02:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2021-10-09 07:37:13 +02:00
|
|
|
error_handler: Box::new(()) as _,
|
|
|
|
error_channel_size: 64,
|
|
|
|
event_channel_size: 1024,
|
|
|
|
}
|
2021-10-10 05:03:05 +02:00
|
|
|
}
|
2021-08-24 09:59:11 +02:00
|
|
|
}
|
|
|
|
|
2021-10-09 07:37:13 +02:00
|
|
|
impl InitConfig {
|
2021-08-22 12:05:09 +02:00
|
|
|
/// Set the runtime error handler.
|
|
|
|
///
|
2021-10-09 07:37:13 +02:00
|
|
|
/// See the [documentation on the field](InitConfig#structfield.error_handler) for more details.
|
2021-08-22 12:05:09 +02:00
|
|
|
pub fn on_error(&mut self, handler: impl Handler<RuntimeError> + Send + 'static) -> &mut Self {
|
2021-10-09 07:37:13 +02:00
|
|
|
self.error_handler = Box::new(handler) as _;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the buffer size of the channel which carries runtime errors.
|
|
|
|
///
|
|
|
|
/// See the [documentation on the field](InitConfig#structfield.error_channel_size) for more details.
|
|
|
|
pub fn error_channel_size(&mut self, size: usize) -> &mut Self {
|
|
|
|
self.error_channel_size = size;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the buffer size of the channel which carries events.
|
|
|
|
///
|
|
|
|
/// See the [documentation on the field](InitConfig#structfield.event_channel_size) for more details.
|
|
|
|
pub fn event_channel_size(&mut self, size: usize) -> &mut Self {
|
|
|
|
self.event_channel_size = size;
|
2021-08-22 12:05:09 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 10:46:44 +02:00
|
|
|
impl fmt::Debug for InitConfig {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_struct("InitConfig")
|
|
|
|
.field("error_channel_size", &self.error_channel_size)
|
|
|
|
.field("event_channel_size", &self.event_channel_size)
|
|
|
|
.finish_non_exhaustive()
|
|
|
|
}
|
|
|
|
}
|