From cf400799bbed212497b409e0ae31ffa7c4dc57d6 Mon Sep 17 00:00:00 2001 From: scoobybejesus Date: Sun, 1 Sep 2019 14:37:00 -0400 Subject: [PATCH] Protects from crash if user enters ~ for home dir. Temporary solution for #32. --- src/cli_user_choices.rs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/cli_user_choices.rs b/src/cli_user_choices.rs index a00f717..a41caa4 100644 --- a/src/cli_user_choices.rs +++ b/src/cli_user_choices.rs @@ -22,16 +22,40 @@ pub fn choose_file_for_import() -> Result> { println!("Please input a file (absolute or relative path) to import: "); - let file_str = _get_path(); - Ok( PathBuf::from(file_str.unwrap()) ) + let file_string = _get_path()?; + + let has_tilde = begins_with_tilde(&file_string); + + if has_tilde { + println!("Unfortunately, the tilde '~' cannot be used as a shortcut for your home directory.\n"); + choose_file_for_import() + } else { + Ok( PathBuf::from(file_string) ) + } } pub fn choose_export_dir() -> Result> { println!("Please input a file path for exports: "); - let file_str = _get_path(); - Ok( PathBuf::from(file_str.unwrap()) ) + let file_string = _get_path()?; + + let has_tilde = begins_with_tilde(&file_string); + + if has_tilde { + println!("Unfortunately, the tilde '~' cannot be used as a shortcut for your home directory.\n"); + choose_export_dir() + } else { + Ok( PathBuf::from(file_string) ) + } +} + +pub fn begins_with_tilde(unchecked_path: &String) -> bool { + + match unchecked_path.find("~") { + Some(0) => return true, + _ => return false + } } fn _get_path() -> Result<(String), Box> {