From e8d76f360ccb1ea1291e286cadc0b961bc81f872 Mon Sep 17 00:00:00 2001 From: Robert Bennett Date: Wed, 4 Dec 2024 10:01:36 -0500 Subject: [PATCH] Simplified save args --- numbat/src/command.rs | 92 +++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/numbat/src/command.rs b/numbat/src/command.rs index 495b9280..15c9902a 100644 --- a/numbat/src/command.rs +++ b/numbat/src/command.rs @@ -112,16 +112,43 @@ enum Command<'a, 'b, Editor> { Clear { clear_fn: fn(&mut Editor) -> CommandControlFlow, }, - Save { - session_history: &'a SessionHistory, - dst: &'b str, - print_fn: Option, - #[allow(clippy::type_complexity)] - save_fn: fn(&SessionHistory, &str, Option) -> Result<(), Box>, - }, + Save(SaveCmdArgs<'a, 'b>), Quit, } +struct SaveCmdArgs<'a, 'b> { + session_history: &'a SessionHistory, + dst: &'b str, + print_fn: Option, +} + +impl SaveCmdArgs<'_, '_> { + fn save(&self) -> Result<(), Box> { + let Self { + session_history, + dst, + print_fn, + } = self; + + session_history.save( + dst, + SessionHistoryOptions { + include_err_lines: false, + trim_lines: true, + }, + )?; + + if let Some(print_markup) = *print_fn { + let markup = m::text("successfully saved session history to") + + m::space() + + m::string(dst.to_compact_string()); + print_markup(&markup) + } + + Ok(()) + } +} + pub struct CommandRunner { print_markup: Option, clear: Option CommandControlFlow>, @@ -260,29 +287,6 @@ impl CommandRunner { Command::Clear { clear_fn } } CommandKind::Save => { - fn save_fn( - session_history: &SessionHistory, - dst: &str, - print_markup: Option, - ) -> Result<(), Box> { - session_history.save( - dst, - SessionHistoryOptions { - include_err_lines: false, - trim_lines: true, - }, - )?; - - if let Some(print_markup) = print_markup { - let markup = m::text("successfully saved session history to") - + m::space() - + m::string(dst.to_compact_string()); - print_markup(&markup) - } - - Ok(()) - } - let Some(session_history) = session_history else { return Ok(None); }; @@ -306,12 +310,11 @@ impl CommandRunner { None => "history.nbt", }; - Command::Save { + Command::Save(SaveCmdArgs { session_history, dst, print_fn, - save_fn, - } + }) } CommandKind::Quit(quit_alias) => { let Some(_) = quit else { @@ -336,11 +339,13 @@ impl CommandRunner { /// Try to run the input line as a command. /// /// If the line is recognized as an (enabled) command, this handles the happy path, - /// but it is up to frontends to handle the error path if this returns an error. If - /// the command was recognized then this returns one of - /// `CommandControlFlow::Continue` or `CommandControlFlow::Return`. If the line is - /// not an enabled command (whether that's because it's not a command at all, or - /// it's a disabled command), then this returns `CommandControlFlow::NotACommand`. + /// but it is up to frontends to handle the error path if this returns an error, + /// which can happen because arguments to the command were incorrect (eg `list + /// foobar`) or because the command failed at runtime (eg `save /`). If the command + /// was recognized then this returns one of `CommandControlFlow::Continue` or + /// `CommandControlFlow::Return`. If the line is not an enabled command (whether + /// that's because it's not a command at all, or it's a disabled command), then this + /// returns `CommandControlFlow::NotACommand`. pub fn try_run_command>( &self, line: &str, @@ -374,13 +379,8 @@ impl CommandRunner { CommandControlFlow::Continue } Command::Clear { clear_fn } => clear_fn(editor), - Command::Save { - session_history, - dst, - print_fn, - save_fn, - } => { - save_fn(session_history, dst, print_fn).map_err(|err| Box::new((*err).into()))?; + Command::Save(save_args) => { + save_args.save().map_err(|err| Box::new((*err).into()))?; CommandControlFlow::Continue } Command::Quit => CommandControlFlow::Return, @@ -407,7 +407,7 @@ impl<'b, Editor> Command<'_, 'b, Editor> { Command::Info { item, .. } => BareCommand::Info { item }, Command::List { items, .. } => BareCommand::List { items }, Command::Clear { clear_fn: _ } => BareCommand::Clear, - Command::Save { dst, .. } => BareCommand::Save { dst }, + Command::Save(SaveCmdArgs { dst, .. }) => BareCommand::Save { dst }, Command::Quit => BareCommand::Quit, } }