ok/jj
1
0
Fork 0
forked from mirrors/jj

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.
This commit is contained in:
Yuya Nishihara 2023-01-03 21:48:48 +09:00
parent 0110814885
commit e0690f9d66
4 changed files with 8 additions and 9 deletions

View file

@ -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),
}
}

View file

@ -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),
}
}

View file

@ -1733,7 +1733,7 @@ impl CliRunner<()> {
// TODO: use crate::commands::run_command() by default
pub fn set_dispatch_fn<F>(self, dispatch_fn: F) -> CliRunner<F>
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<F> CliRunner<F>
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) -> ! {

View file

@ -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();
}