watchexec/lib/src/config.rs

87 lines
3.1 KiB
Rust
Raw Normal View History

use std::fmt;
2021-08-19 11:28:56 +02:00
use derive_builder::Builder;
use crate::{error::RuntimeError, handler::Handler};
/// Runtime configuration for [`Watchexec`][crate::Watchexec].
2021-08-19 11:28:56 +02:00
///
/// This is used both when constructing the instance (as initial configuration) and to reconfigure
/// it at runtime via [`Watchexec::reconfig()`][crate::Watchexec::reconfig()].
2021-08-19 11:28:56 +02:00
///
/// Use [`RuntimeConfigBuilder`] 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.
2021-08-19 11:28:56 +02:00
#[derive(Builder, Clone, Debug)]
#[non_exhaustive]
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.
#[builder(default)]
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.
#[builder(default)]
pub action: crate::action::WorkingData,
}
2021-08-20 18:43:55 +02:00
/// Initialisation configuration for [`Watchexec`][crate::Watchexec].
///
/// This is used only for constructing the instance.
///
/// Use [`InitConfigBuilder`] 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. Note that this
/// builder uses a different style (consuming `self`) for technical reasons (cannot be `Clone`d).
#[derive(Builder)]
#[builder(pattern = "owned")]
#[non_exhaustive]
pub struct InitConfig {
/// Runtime error handler.
2021-08-19 11:28:56 +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
/// given to the handler. If that second handler call errors as well, its error is ignored.
2021-08-19 11:28:56 +02:00
///
/// # Examples
///
/// ```
/// # use std::convert::Infallible;
/// # use watchexec::config::InitConfigBuilder;
/// InitConfigBuilder::default()
/// .error_handler(Box::new(|err| async move {
/// tracing::error!("{}", err);
/// Ok::<(), Infallible>(())
/// }));
/// ```
#[builder(default = "Box::new(()) as _")]
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
#[builder(default = "64")]
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.
#[builder(default = "1024")]
pub event_channel_size: usize,
2021-08-18 15:12:50 +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()
}
}