diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-05-07 20:04:13 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-05-07 20:04:13 -0400 |
commit | e67ea4c5f8e6d4790c43159e965417b59603e5a5 (patch) | |
tree | 293a9b8929ef103776953e2e188ad1681b48b91c | |
parent | c8709ff70570e9e8aa5f44c5e30c1027d9a0c701 (diff) |
add more automatic error handling
-rw-r--r-- | src/commands/bulk_rename.rs | 27 | ||||
-rw-r--r-- | src/commands/open_file.rs | 21 | ||||
-rw-r--r-- | src/commands/rename_file.rs | 24 | ||||
-rw-r--r-- | src/error/error_kind.rs | 6 | ||||
-rw-r--r-- | src/error/error_type.rs | 9 | ||||
-rw-r--r-- | src/main.rs | 11 |
6 files changed, 48 insertions, 50 deletions
diff --git a/src/commands/bulk_rename.rs b/src/commands/bulk_rename.rs index 834629f..06137d2 100644 --- a/src/commands/bulk_rename.rs +++ b/src/commands/bulk_rename.rs @@ -14,15 +14,7 @@ const ENV_EDITOR: &str = "EDITOR"; pub fn _bulk_rename(context: &mut AppContext) -> JoshutoResult<()> { const PREFIX: &str = "joshuto-"; - let editor = match std::env::var(ENV_EDITOR) { - Ok(s) => s, - Err(_) => { - return Err(JoshutoError::new( - JoshutoErrorKind::EnvVarNotPresent, - format!("{} environment variable not set", ENV_EDITOR), - )); - } - }; + let editor = std::env::var(ENV_EDITOR)?; /* generate a random file name to write to */ let mut rand_str = String::with_capacity(PREFIX.len() + 10); @@ -36,13 +28,11 @@ pub fn _bulk_rename(context: &mut AppContext) -> JoshutoResult<()> { let mut file_path = path::PathBuf::from("/tmp"); file_path.push(rand_str); - let paths = { - let curr_tab = context.tab_context_ref().curr_tab_ref(); - match curr_tab.curr_list_ref() { - Some(s) => s.get_selected_paths(), - None => vec![], - } - }; + let paths = context + .tab_context_ref() + .curr_tab_ref() + .curr_list_ref() + .map_or(vec![], |s| s.get_selected_paths()); { let mut file = std::fs::File::create(&file_path)?; @@ -57,13 +47,14 @@ pub fn _bulk_rename(context: &mut AppContext) -> JoshutoResult<()> { let mut command = process::Command::new(editor); command.arg(&file_path); - let time = std::time::SystemTime::now(); + let last_modified = std::fs::metadata(&file_path)?.modified()?; { let mut handle = command.spawn()?; handle.wait()?; } + // check if the file was modified since it was created let metadata = std::fs::metadata(&file_path)?; - if time >= metadata.modified()? { + if metadata.modified()? <= last_modified { return Ok(()); } diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index 41f9a8b..71946d3 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -35,11 +35,12 @@ pub fn open(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult change_directory::cd(path.as_path(), context)?; LoadChild::load_child(context)?; } else { - let paths: Vec<path::PathBuf> = - match context.tab_context_ref().curr_tab_ref().curr_list_ref() { - Some(a) => a.get_selected_paths(), - None => vec![], - }; + let paths = context + .tab_context_ref() + .curr_tab_ref() + .curr_list_ref() + .map_or(vec![], |s| s.get_selected_paths()); + if paths.is_empty() { return Err(JoshutoError::new( JoshutoErrorKind::Io(io::ErrorKind::NotFound), @@ -133,10 +134,12 @@ where } pub fn open_with(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult<()> { - let paths: Vec<path::PathBuf> = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { - Some(a) => a.get_selected_paths(), - None => vec![], - }; + let paths = context + .tab_context_ref() + .curr_tab_ref() + .curr_list_ref() + .map_or(vec![], |s| s.get_selected_paths()); + if paths.is_empty() { return Err(JoshutoError::new( JoshutoErrorKind::Io(io::ErrorKind::NotFound), diff --git a/src/commands/rename_file.rs b/src/commands/rename_file.rs index d8f491b..7f20c90 100644 --- a/src/commands/rename_file.rs +++ b/src/commands/rename_file.rs @@ -26,16 +26,12 @@ pub fn _rename_file( } pub fn rename_file(context: &mut AppContext, dest: &path::Path) -> JoshutoResult<()> { - let mut path: Option<path::PathBuf> = None; - - if let Some(s) = context + let path: Option<path::PathBuf> = context .tab_context_ref() .curr_tab_ref() .curr_list_ref() .and_then(|s| s.curr_entry_ref()) - { - path = Some(s.file_path().to_path_buf()); - } + .and_then(|s| Some(s.file_path().to_path_buf())); if let Some(path) = path { _rename_file(context, path.as_path(), dest)?; @@ -49,15 +45,13 @@ pub fn _rename_file_append( backend: &mut TuiBackend, file_name: &str, ) -> JoshutoResult<()> { - let prefix; - let suffix; - if let Some(ext) = file_name.rfind('.') { - prefix = format!("rename {}", &file_name[0..ext]); - suffix = String::from(&file_name[ext..]); - } else { - prefix = format!("rename {}", file_name); - suffix = String::new(); - } + let (prefix, suffix): (String, String) = match file_name.rfind('.') { + Some(ext) => ( + format!("rename {}", &file_name[0..ext]), + file_name[ext..].to_string(), + ), + None => (format!("rename {}", file_name), "".to_string()), + }; command_line::readline(context, backend, &prefix, &suffix) } diff --git a/src/error/error_kind.rs b/src/error/error_kind.rs index b5aa835..679ad32 100644 --- a/src/error/error_kind.rs +++ b/src/error/error_kind.rs @@ -31,3 +31,9 @@ impl std::convert::From<&globset::ErrorKind> for JoshutoErrorKind { Self::Glob } } + +impl std::convert::From<std::env::VarError> for JoshutoErrorKind { + fn from(_: std::env::VarError) -> Self { + Self::EnvVarNotPresent + } +} diff --git a/src/error/error_type.rs b/src/error/error_type.rs index 80dc4bc..9ca849a 100644 --- a/src/error/error_type.rs +++ b/src/error/error_type.rs @@ -42,6 +42,15 @@ impl std::convert::From<globset::Error> for JoshutoError { } } +impl std::convert::From<std::env::VarError> for JoshutoError { + fn from(err: std::env::VarError) -> Self { + Self { + _kind: JoshutoErrorKind::from(err), + _cause: "Environment variable not found".to_string(), + } + } +} + impl std::convert::From<trash::Error> for JoshutoError { fn from(err: trash::Error) -> Self { let err = match err { diff --git a/src/main.rs b/src/main.rs index 282d291..cdb6214 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ use crate::config::{ AppConfig, AppKeyMapping, AppMimetypeRegistry, AppTheme, ConfigStructure, JoshutoPreview, }; use crate::context::AppContext; -use crate::error::{JoshutoError, JoshutoErrorKind}; +use crate::error::JoshutoError; use crate::run::run; const PROGRAM_NAME: &str = "joshuto"; @@ -67,12 +67,8 @@ pub struct Args { fn run_joshuto(args: Args) -> Result<(), JoshutoError> { if args.version { let version = env!("CARGO_PKG_VERSION"); - println!("{}", version); - let err = JoshutoError::new( - JoshutoErrorKind::EnvVarNotPresent, - "CARGO_PKG_VERSION variable not found".to_string(), - ); - return Err(err); + println!("{}-{}", PROGRAM_NAME, version); + return Ok(()); } if let Some(p) = args.path.as_ref() { match std::env::set_current_dir(p.as_path()) { @@ -106,7 +102,6 @@ fn run_joshuto(args: Args) -> Result<(), JoshutoError> { )?; file.write_all("\n".as_bytes())?; } - Ok(()) } |