From e628378dbb60d7b30668c9f3fedf426f45400996 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 15 Nov 2016 20:39:56 -0800 Subject: [PATCH] Avoid using panic! outside of tests (#121) Don't panic. --- src/app.rs | 15 +++++++++------ src/lib.rs | 10 ++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/app.rs b/src/app.rs index e4a32a0..df1e0db 100644 --- a/src/app.rs +++ b/src/app.rs @@ -31,12 +31,12 @@ enum UseColor { } impl UseColor { - fn from_argument(use_color: &str) -> UseColor { + fn from_argument(use_color: &str) -> Option { match use_color { - "auto" => UseColor::Auto, - "always" => UseColor::Always, - "never" => UseColor::Never, - _ => panic!("Invalid argument to --color. This is a bug in just."), + "auto" => Some(UseColor::Auto), + "always" => Some(UseColor::Always), + "never" => Some(UseColor::Never), + _ => None, } } @@ -140,7 +140,10 @@ pub fn app() { .get_matches(); let use_color_argument = matches.value_of("color").expect("--color had no value"); - let use_color = UseColor::from_argument(use_color_argument); + let use_color = match UseColor::from_argument(use_color_argument) { + Some(use_color) => use_color, + None => die!("Invalid argument to --color. This is a bug in just."), + }; let justfile_option = matches.value_of("justfile"); let working_directory_option = matches.value_of("working-directory"); diff --git a/src/lib.rs b/src/lib.rs index 8783e35..93186d4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -280,12 +280,14 @@ impl<'a> Recipe<'a> { ) -> Result<(), RunError<'a>> { let argument_map = self.parameters.iter().enumerate() .map(|(i, parameter)| if i < arguments.len() { - (parameter.name, arguments[i]) + Ok((parameter.name, arguments[i])) } else if let Some(ref default) = parameter.default { - (parameter.name, default.as_str()) + Ok((parameter.name, default.as_str())) } else { - panic!("Recipe.run: missing parameter without default. This is a bug in just."); - }).collect(); + Err(RunError::InternalError{ + message: "missing parameter without default".to_string() + }) + }).collect::, _>>()?.into_iter().collect(); let mut evaluator = Evaluator { evaluated: empty(),