diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-04-14 17:57:50 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-04-14 17:57:50 -0400 |
commit | 71e0c7faf3cc283b2b24e70631e5b18a7a7525cd (patch) | |
tree | b27794c91a930ae2a1bc5cdc6a468b9b4d553134 /src/commands/change_directory.rs | |
parent | 5a820a7a275bf5cacd2c545c0a0ac533789ec349 (diff) |
rework error handling system
rather than letting each command separately handle errors, we return a
Result<(), JoshutoError> instead and allow for run.rs to handle all errors
Diffstat (limited to 'src/commands/change_directory.rs')
-rw-r--r-- | src/commands/change_directory.rs | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/src/commands/change_directory.rs b/src/commands/change_directory.rs index 8406940..0e7fd1f 100644 --- a/src/commands/change_directory.rs +++ b/src/commands/change_directory.rs @@ -1,10 +1,13 @@ use std::path; -use crate::commands::{JoshutoCommand, JoshutoRunnable}; +use crate::commands; + use crate::context::JoshutoContext; +use crate::error::JoshutoError; use crate::preview; use crate::ui; use crate::window::JoshutoView; +use commands::{JoshutoCommand, JoshutoRunnable}; #[derive(Clone, Debug)] pub struct ChangeDirectory { @@ -23,20 +26,13 @@ impl ChangeDirectory { path: &path::PathBuf, context: &mut JoshutoContext, view: &JoshutoView, - ) { - if !path.exists() { - ui::wprint_err(&view.bot_win, "Error: No such file or directory"); - return; - } + ) -> Result<(), JoshutoError> { let curr_tab = &mut context.tabs[context.curr_tab_index]; match std::env::set_current_dir(path.as_path()) { - Ok(_) => { - curr_tab.curr_path = path.clone(); - } + Ok(_) => curr_tab.curr_path = path.clone(), Err(e) => { - ui::wprint_err(&view.bot_win, e.to_string().as_str()); - return; + return Err(JoshutoError::IO(e)); } } @@ -79,6 +75,7 @@ impl ChangeDirectory { &context.username, &context.hostname, ); + Ok(()) } } @@ -91,13 +88,18 @@ impl std::fmt::Display for ChangeDirectory { } impl JoshutoRunnable for ChangeDirectory { - fn execute(&self, context: &mut JoshutoContext, view: &JoshutoView) { - Self::change_directory(&self.path, context, view); + fn execute( + &self, + context: &mut JoshutoContext, + view: &JoshutoView, + ) -> Result<(), JoshutoError> { + let res = Self::change_directory(&self.path, context, view); preview::preview_file( &mut context.tabs[context.curr_tab_index], &view, &context.config_t, ); ncurses::doupdate(); + res } } |