From 0ea8da064d0b7d2cc2eae12fe1c93f230d4e1d26 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Fri, 3 Feb 2023 16:44:02 -0800 Subject: [PATCH] cli_util: make `CliRunner` builder functions mutate `self` It's easier to mutate `self` than to create a new instance. It does increase the risk of forgetting to update a field when adding a new field or a new function, so maybe that's why @yuja did it this way? --- src/cli_util.rs | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/src/cli_util.rs b/src/cli_util.rs index 884f6b497..f214c2dcc 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -1947,17 +1947,13 @@ impl CliRunner { } /// Replaces `StoreFactories` to be used. - pub fn set_store_factories(self, store_factories: StoreFactories) -> Self { - CliRunner { - tracing_subscription: self.tracing_subscription, - app: self.app, - store_factories: Some(store_factories), - dispatch_fn: self.dispatch_fn, - } + pub fn set_store_factories(mut self, store_factories: StoreFactories) -> Self { + self.store_factories = Some(store_factories); + self } /// Registers new subcommands in addition to the default ones. - pub fn add_subcommand(self, custom_dispatch_fn: F) -> Self + pub fn add_subcommand(mut self, custom_dispatch_fn: F) -> Self where C: clap::Subcommand, F: FnOnce(&mut Ui, &CommandHelper, C) -> Result<(), CommandError> + 'static, @@ -1970,16 +1966,13 @@ impl CliRunner { Err(_) => old_dispatch_fn(ui, command_helper, matches), } }; - CliRunner { - tracing_subscription: self.tracing_subscription, - app: C::augment_subcommands(self.app), - store_factories: self.store_factories, - dispatch_fn: Box::new(new_dispatch_fn), - } + self.app = C::augment_subcommands(self.app); + self.dispatch_fn = Box::new(new_dispatch_fn); + self } /// Registers new global arguments in addition to the default ones. - pub fn add_global_args(self, process_before: F) -> Self + pub fn add_global_args(mut self, process_before: F) -> Self where A: clap::Args, F: FnOnce(&mut Ui, A) -> Result<(), CommandError> + 'static, @@ -1991,12 +1984,9 @@ impl CliRunner { process_before(ui, custom_args)?; old_dispatch_fn(ui, command_helper, matches) }; - CliRunner { - tracing_subscription: self.tracing_subscription, - app: A::augment_args(self.app), - store_factories: self.store_factories, - dispatch_fn: Box::new(new_dispatch_fn), - } + self.app = A::augment_args(self.app); + self.dispatch_fn = Box::new(new_dispatch_fn); + self } fn run_internal(