Remove derive-builder

* the InitConfigBuilder non-miette error goes away
* creating an InitConfig is no longer faillible for no reason
* the "builder" style is consistent between the two config structs
This commit is contained in:
Félix Saparelli 2021-10-09 18:37:13 +13:00
parent 07878f8357
commit 81bee9513d
No known key found for this signature in database
GPG key ID: B948C4BAE44FC474
6 changed files with 39 additions and 31 deletions

View file

@ -21,11 +21,9 @@ pub fn new(args: &ArgMatches<'static>) -> Result<(InitConfig, RuntimeConfig, Arc
}
fn init(_args: &ArgMatches<'static>) -> Result<InitConfig> {
let mut config = InitConfig::builder();
let mut config = InitConfig::default();
config.on_error(PrintDisplay(stderr()));
Ok(config.build()?)
Ok(config)
}
fn runtime(args: &ArgMatches<'static>) -> Result<(RuntimeConfig, Arc<TaggedFilterer>)> {

View file

@ -18,7 +18,6 @@ edition = "2018"
async-recursion = "0.3.2"
atomic-take = "1.0.0"
clearscreen = "1.0.6"
derive_builder = "0.10.2"
dunce = "1.0.2"
futures = "0.3.16"
globset = "0.4.8"

View file

@ -15,7 +15,7 @@ async fn main() -> color_eyre::eyre::Result<()> {
tracing_subscriber::fmt::init();
color_eyre::install()?;
let mut init = InitConfig::builder();
let mut init = InitConfig::default();
init.on_error(|err| async move {
eprintln!("Watchexec Runtime Error: {}", err);
Ok::<(), std::convert::Infallible>(())
@ -25,7 +25,7 @@ async fn main() -> color_eyre::eyre::Result<()> {
runtime.pathset(["src", "dontexist", "examples"]);
runtime.command(["date"]);
let wx = Watchexec::new(init.build()?, runtime.clone())?;
let wx = Watchexec::new(init, runtime.clone())?;
let w = wx.clone();
let config = runtime.clone();

View file

@ -3,7 +3,6 @@
use std::{fmt, path::Path, sync::Arc, time::Duration};
use atomic_take::AtomicTake;
use derive_builder::Builder;
use crate::{
action::{Action, PostSpawn, PreSpawn},
@ -147,11 +146,8 @@ impl RuntimeConfig {
///
/// 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")]
/// 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.
#[non_exhaustive]
pub struct InitConfig {
/// Runtime error handler.
@ -166,45 +162,60 @@ pub struct InitConfig {
///
/// ```
/// # use std::convert::Infallible;
/// # use watchexec::config::InitConfigBuilder;
/// let mut init = InitConfigBuilder::default();
/// # use watchexec::config::InitConfig;
/// let mut init = InitConfig::default();
/// init.on_error(|err| async move {
/// tracing::error!("{}", err);
/// Ok::<(), Infallible>(())
/// });
/// ```
#[builder(private, default = "Box::new(()) as _")]
// TODO: figure out how to remove the builder setter entirely
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.
#[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,
}
impl InitConfig {
/// Returns a new [`InitConfigBuilder`] for builder the initial configuration.
pub fn builder() -> InitConfigBuilder {
InitConfigBuilder::default()
}
impl Default for InitConfig {
fn default() -> Self {
Self {
error_handler: Box::new(()) as _,
error_channel_size: 64,
event_channel_size: 1024,
}
}
}
impl InitConfigBuilder {
impl InitConfig {
/// Set the runtime error handler.
///
/// See the [documentation on the field][InitConfig#structfield.error_handler] for more details.
/// See the [documentation on the field](InitConfig#structfield.error_handler) for more details.
pub fn on_error(&mut self, handler: impl Handler<RuntimeError> + Send + 'static) -> &mut Self {
self.error_handler = Some(Box::new(handler) as _);
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;
self
}
}

View file

@ -23,7 +23,7 @@
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Report> {
//! let mut init = InitConfig::builder();
//! let mut init = InitConfig::default();
//! init.on_error(PrintDebug(std::io::stderr()));
//!
//! let mut runtime = RuntimeConfig::default();
@ -32,7 +32,7 @@
//! let conf = YourConfigFormat::load_from_file("watchexec.conf").await?;
//! conf.apply(&mut runtime);
//!
//! let we = Watchexec::new(init.build().unwrap(), runtime.clone())?;
//! let we = Watchexec::new(init, runtime.clone())?;
//! let w = we.clone();
//!
//! let c = runtime.clone();

View file

@ -11,7 +11,7 @@ async fn main() -> color_eyre::eyre::Result<()> {
tracing_subscriber::fmt::init();
color_eyre::install()?;
let mut init = InitConfig::builder();
let mut init = InitConfig::default();
init.on_error(|err| async move {
eprintln!("Watchexec Runtime Error: {}", err);
Ok::<(), std::convert::Infallible>(())
@ -19,7 +19,7 @@ async fn main() -> color_eyre::eyre::Result<()> {
let runtime = RuntimeConfig::default();
let wx = Watchexec::new(init.build()?, runtime)?;
let wx = Watchexec::new(init, runtime)?;
wx.main();
// TODO: induce an error here