diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2019-07-24 21:32:21 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2019-07-24 21:32:21 +0200 |
commit | 5f10ab976fcc75683985d2551ff16f7858cac49a (patch) | |
tree | 90c60a680252c985b2cf3e571ab0bdb32dcece3f /lib/core | |
parent | 03fec5f4dbdd3fd46ae949957f3b89ec92a188ab (diff) |
Rewrite get_rtp_match() to not panic
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'lib/core')
-rw-r--r-- | lib/core/libimagrt/src/runtime.rs | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/lib/core/libimagrt/src/runtime.rs b/lib/core/libimagrt/src/runtime.rs index 8845616d..43fa1eaf 100644 --- a/lib/core/libimagrt/src/runtime.rs +++ b/lib/core/libimagrt/src/runtime.rs @@ -79,7 +79,7 @@ impl<'a> Runtime<'a> { { let matches = cli_app.clone().matches(); - let rtp = get_rtp_match(&matches); + let rtp = get_rtp_match(&matches)?; let configpath = matches.value_of(Runtime::arg_config_name()) .map_or_else(|| rtp.clone(), PathBuf::from); @@ -122,7 +122,7 @@ impl<'a> Runtime<'a> { Runtime::init_logger(&matches, config.as_ref()) } - let rtp = get_rtp_match(&matches); + let rtp = get_rtp_match(&matches)?; let storepath = matches.value_of(Runtime::arg_storepath_name()) .map_or_else(|| { @@ -640,24 +640,26 @@ pub trait IdPathProvider { } /// Exported for the `imag` command, you probably do not want to use that. -pub fn get_rtp_match<'a>(matches: &ArgMatches<'a>) -> PathBuf { - matches.value_of(Runtime::arg_runtimepath_name()) - .map_or_else(|| { - if let Ok(home) = env::var("IMAG_RTP") { - return PathBuf::from(home); - } +pub fn get_rtp_match<'a>(matches: &ArgMatches<'a>) -> Result<PathBuf> { + if let Some(p) = matches + .value_of(Runtime::arg_runtimepath_name()) + .map(PathBuf::from) + { + return Ok(p) + } - match env::var("HOME") { - Ok(home) => { - let mut p = PathBuf::from(home); - p.push(".imag"); - return p; - }, - Err(_) => panic!("You seem to be $HOME-less. Please get a $HOME before using this \ - software. We are sorry for you and hope you have some \ - accommodation anyways."), - } - }, PathBuf::from) + if let Ok(home) = env::var("IMAG_RTP") { + return Ok(PathBuf::from(home)) + } + + env::var("HOME") + .map(PathBuf::from) + .map(|mut p| { p.push(".imag"); p }) + .map_err(|_| { + err_msg("You seem to be $HOME-less. Please get a $HOME before using this \ + software. We are sorry for you and hope you have some \ + accommodation anyways.") + }) } fn get_override_specs(matches: &ArgMatches) -> Vec<String> { |