diff options
author | Tim Oram <dev@mitmaro.ca> | 2020-09-17 10:45:49 -0230 |
---|---|---|
committer | Tim Oram <dev@mitmaro.ca> | 2020-09-17 22:01:14 -0230 |
commit | c6dbcc6017a7a1b343a585729239ad52ccb6ceba (patch) | |
tree | 5f27828bc435ef9eed85ebd835d55418985c07cb /src/external_editor | |
parent | 10d797ebf3910f45f6ed3cd01573e531f388f31d (diff) |
Move error handling to process module
The error handling module needed to have state provided to it from other
modules, which meant that the other modules needed to understand how the
error module worked. This was awkward and fragile. This moves the error
handling code into the process module and makes it a global system. This
has the advantage of not requiring tracking of the previous state and
allows for the removal of the HandleInputResult struct.
Diffstat (limited to 'src/external_editor')
-rw-r--r-- | src/external_editor/mod.rs | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/src/external_editor/mod.rs b/src/external_editor/mod.rs index e1f87eb..e53d13a 100644 --- a/src/external_editor/mod.rs +++ b/src/external_editor/mod.rs @@ -6,9 +6,8 @@ use crate::git_interactive::GitInteractive; use crate::input::input_handler::{InputHandler, InputMode}; use crate::input::Input; use crate::process::exit_status::ExitStatus; -use crate::process::handle_input_result::{HandleInputResult, HandleInputResultBuilder}; use crate::process::process_module::ProcessModule; -use crate::process::process_result::{ProcessResult, ProcessResultBuilder}; +use crate::process::process_result::ProcessResult; use crate::process::state::State; use crate::view::view_data::ViewData; use crate::view::View; @@ -67,12 +66,12 @@ impl<'e> ProcessModule for ExternalEditor<'e> { input_handler: &InputHandler<'_>, _git_interactive: &mut GitInteractive, _view: &View<'_>, - ) -> HandleInputResult + ) -> ProcessResult { match self.state { ExternalEditorState::Active => Self::handle_input_active(input_handler), ExternalEditorState::Empty => self.handle_input_empty(input_handler), - _ => HandleInputResult::new(Input::Other), + _ => ProcessResult::new().input(Input::Other), } } } @@ -129,21 +128,21 @@ impl<'e> ExternalEditor<'e> { } fn process_active(&mut self, git_interactive: &GitInteractive) -> ProcessResult { - let mut result = ProcessResultBuilder::new(); + let mut result = ProcessResult::new(); if let Err(e) = self.run_editor(git_interactive) { - result = result.error(e.as_str(), State::ExternalEditor); + result = result.error(e.as_str()); self.state = ExternalEditorState::Error; } else { self.state = ExternalEditorState::Finish; } - result.build() + result } fn process_finish(&mut self, git_interactive: &mut GitInteractive) -> ProcessResult { - let mut result = ProcessResultBuilder::new(); + let mut result = ProcessResult::new(); if let Err(e) = git_interactive.reload_file() { - result = result.error(e.as_str(), State::ExternalEditor); + result = result.error(e.as_str()); self.state = ExternalEditorState::Error; } else if git_interactive.get_lines().is_empty() { @@ -152,11 +151,11 @@ impl<'e> ExternalEditor<'e> { else { result = result.state(State::List); } - result.build() + result } fn process_error(git_interactive: &GitInteractive) -> ProcessResult { - let mut result = ProcessResultBuilder::new().state(State::Exiting); + let mut result = ProcessResult::new().state(State::Exiting); if git_interactive.get_lines().is_empty() { result = result.exit_status(ExitStatus::Good); @@ -164,23 +163,23 @@ impl<'e> ExternalEditor<'e> { else { result = result.exit_status(ExitStatus::StateError); } - result.build() + result } - fn handle_input_active(input_handler: &InputHandler<'_>) -> HandleInputResult { + fn handle_input_active(input_handler: &InputHandler<'_>) -> ProcessResult { let input = input_handler.get_input(InputMode::Default); - let mut result = HandleInputResultBuilder::new(input); + let mut result = ProcessResult::new().input(input); if let Input::Resize = input { } else { result = result.state(State::List); } - result.build() + result } - fn handle_input_empty(&mut self, input_handler: &InputHandler<'_>) -> HandleInputResult { + fn handle_input_empty(&mut self, input_handler: &InputHandler<'_>) -> ProcessResult { let input = input_handler.get_input(InputMode::Confirm); - let mut result = HandleInputResultBuilder::new(input); + let mut result = ProcessResult::new().input(input); match input { Input::Yes => { result = result.exit_status(ExitStatus::Good).state(State::Exiting); @@ -191,6 +190,6 @@ impl<'e> ExternalEditor<'e> { }, _ => {}, } - result.build() + result } } |