From e0690f9d660c13249b308d5afa80cb03a4afa6d4 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Tue, 3 Jan 2023 21:48:48 +0900 Subject: [PATCH] cli: pass CommandHelper to dispatch function by reference This allows us to chain custom dispatch functions. If CommandHelper were moved or passed by mutable reference, weird thing could happen depending on the call order. --- examples/custom-backend/main.rs | 4 ++-- examples/custom-command/main.rs | 4 ++-- src/cli_util.rs | 7 +++---- src/main.rs | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/examples/custom-backend/main.rs b/examples/custom-backend/main.rs index 812103e77..ccf0044b3 100644 --- a/examples/custom-backend/main.rs +++ b/examples/custom-backend/main.rs @@ -47,7 +47,7 @@ fn create_store_factories() -> StoreFactories { fn run( ui: &mut Ui, - command_helper: CommandHelper, + command_helper: &CommandHelper, matches: &ArgMatches, ) -> Result<(), CommandError> { match CustomCommands::from_arg_matches(matches) { @@ -61,7 +61,7 @@ fn run( Ok(()) } // Handle default commands - Err(_) => run_command(ui, &command_helper, matches), + Err(_) => run_command(ui, command_helper, matches), } } diff --git a/examples/custom-command/main.rs b/examples/custom-command/main.rs index 4637fcc8a..0f21d4717 100644 --- a/examples/custom-command/main.rs +++ b/examples/custom-command/main.rs @@ -32,7 +32,7 @@ struct FrobnicateArgs { fn run( ui: &mut Ui, - command_helper: CommandHelper, + command_helper: &CommandHelper, matches: &ArgMatches, ) -> Result<(), CommandError> { match CustomCommands::from_arg_matches(matches) { @@ -55,7 +55,7 @@ fn run( Ok(()) } // Handle default commands - Err(_) => run_command(ui, &command_helper, matches), + Err(_) => run_command(ui, command_helper, matches), } } diff --git a/src/cli_util.rs b/src/cli_util.rs index 31c658a20..1e7201ea5 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -1733,7 +1733,7 @@ impl CliRunner<()> { // TODO: use crate::commands::run_command() by default pub fn set_dispatch_fn(self, dispatch_fn: F) -> CliRunner where - F: FnOnce(&mut Ui, CommandHelper, &ArgMatches) -> Result<(), CommandError>, + F: FnOnce(&mut Ui, &CommandHelper, &ArgMatches) -> Result<(), CommandError>, { CliRunner { tracing_subscription: self.tracing_subscription, @@ -1746,7 +1746,7 @@ impl CliRunner<()> { impl CliRunner where - F: FnOnce(&mut Ui, CommandHelper, &ArgMatches) -> Result<(), CommandError>, + F: FnOnce(&mut Ui, &CommandHelper, &ArgMatches) -> Result<(), CommandError>, { pub fn run(self, ui: &mut Ui) -> Result<(), CommandError> { ui.reset(crate::config::read_config()?); @@ -1759,8 +1759,7 @@ where if let Some(store_factories) = self.store_factories { command_helper.set_store_factories(store_factories); } - // TODO: pass CommandHelper by reference - (self.dispatch_fn)(ui, command_helper, &matches) + (self.dispatch_fn)(ui, &command_helper, &matches) } pub fn run_and_exit(self) -> ! { diff --git a/src/main.rs b/src/main.rs index 8b5f039b2..2aeeb9e72 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,6 @@ use jujutsu::commands::run_command; fn main() { CliRunner::init() - .set_dispatch_fn(|ui, command_helper, matches| run_command(ui, &command_helper, matches)) + .set_dispatch_fn(run_command) .run_and_exit(); }