From 81bee9513de9052bbe25c17d41c330c15fc555a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Saparelli?= Date: Sat, 9 Oct 2021 18:37:13 +1300 Subject: [PATCH] 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 --- cli/src/config.rs | 6 ++--- lib/Cargo.toml | 1 - lib/examples/demo.rs | 4 +-- lib/src/config.rs | 51 +++++++++++++++++++++++--------------- lib/src/lib.rs | 4 +-- lib/tests/error_handler.rs | 4 +-- 6 files changed, 39 insertions(+), 31 deletions(-) diff --git a/cli/src/config.rs b/cli/src/config.rs index a04af1a1..8b132cc0 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -21,11 +21,9 @@ pub fn new(args: &ArgMatches<'static>) -> Result<(InitConfig, RuntimeConfig, Arc } fn init(_args: &ArgMatches<'static>) -> Result { - 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)> { diff --git a/lib/Cargo.toml b/lib/Cargo.toml index bb2d4a40..1e822e30 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -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" diff --git a/lib/examples/demo.rs b/lib/examples/demo.rs index eb36d03a..d48647c3 100644 --- a/lib/examples/demo.rs +++ b/lib/examples/demo.rs @@ -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(); diff --git a/lib/src/config.rs b/lib/src/config.rs index 1f3ed02c..c8d44fe1 100644 --- a/lib/src/config.rs +++ b/lib/src/config.rs @@ -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 + 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 + 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 } } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 4319a9a1..f02dfce8 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -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(); diff --git a/lib/tests/error_handler.rs b/lib/tests/error_handler.rs index 6bbe57b4..21596487 100644 --- a/lib/tests/error_handler.rs +++ b/lib/tests/error_handler.rs @@ -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