From fa601cb023d1310a509d1c889dd9ce407c99e04c Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 26 Jul 2017 15:22:20 +0100 Subject: [PATCH] try! all the things --- src/config/mod.rs | 17 ++++------------- src/config/read.rs | 17 +++-------------- src/config/validate.rs | 5 +---- src/input.rs | 10 +++------- src/main.rs | 3 ++- src/utils.rs | 15 +++++++++++++++ 6 files changed, 28 insertions(+), 39 deletions(-) create mode 100644 src/utils.rs diff --git a/src/config/mod.rs b/src/config/mod.rs index 6d5c17d..87547d7 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -2,6 +2,7 @@ use serde_yaml; use serde_yaml::Value; use std::path::PathBuf; use std::collections::HashMap; +use utils::result_prefix; pub mod read; pub mod validate; @@ -30,18 +31,8 @@ impl Config { pub fn get_config() -> Result { - let config_str = read::read(); - if config_str.is_err() { - return Err(config_str.unwrap_err()); - } - let config_value = serde_yaml::from_str(&config_str.unwrap()); - if config_value.is_err() { - return Err(format!("Failed to parse config. {}", config_value.unwrap_err())); - } - let config = config_value.unwrap(); - let validation_output = validate::validate(&config); - if validation_output.is_err() { - return Err(format!("Validation error: {}", validation_output.unwrap_err())); - }; + let config_str = try!(read::read()); + let config = try!(result_prefix(serde_yaml::from_str(&config_str), "Config Parse Error".into())); + try!(result_prefix(validate::validate(&config), "Config Validation Error".into())); return Ok(Config::new(config)); } diff --git a/src/config/read.rs b/src/config/read.rs index dc3b968..806667a 100644 --- a/src/config/read.rs +++ b/src/config/read.rs @@ -6,7 +6,7 @@ use serde_yaml::Value; use std::collections::HashMap; use config::consts; - +use utils::result_override; fn get_config_path() -> PathBuf { let mut working_dir = current_dir().unwrap(); @@ -14,22 +14,11 @@ fn get_config_path() -> PathBuf { return working_dir; } - pub fn read() -> Result { let config_path = get_config_path(); - if !config_path.is_file() { - return Err(format!("Failed to find config file at {}.", config_path.display())); - } - let file = File::open(config_path); - if file.is_err() { - return Err("Failed to open file".into()); - } - let mut config_file = file.unwrap(); + let mut config_file = try!(result_override(File::open(&config_path), format!("Unable to find config file at {}", config_path.display()))); let mut contents = String::new(); - let file_read = config_file.read_to_string(&mut contents); - if file_read.is_err() { - return Err("Failed to read config file".into()); - } + try!(result_override(config_file.read_to_string(&mut contents), format!("Failed to read config file at {}.", config_path.display()))); return Ok(contents); } diff --git a/src/config/validate.rs b/src/config/validate.rs index 596e476..73cd467 100644 --- a/src/config/validate.rs +++ b/src/config/validate.rs @@ -47,10 +47,7 @@ pub fn unwrap_group( funcs: Vec<&Fn(&Value) -> ValidationResult>, ) -> ValidationResult { for func in funcs.iter() { - let func_result = func(config); - if func_result.is_err() { - return Err(func_result.unwrap_err()); - } + try!(func(config)); } return Ok(()); } diff --git a/src/input.rs b/src/input.rs index a4d17a5..3bcde64 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,18 +1,14 @@ use std::fs::File; use std::io::Read; use std::path::PathBuf; +use utils::result_override; pub fn read_input_files(files: Vec) -> Result { let mut input = String::new(); for input_file_path in files.iter() { - let input_file_result = File::open(input_file_path); - if input_file_result.is_err() { - return Err(format!("Failed to open input file {}.", input_file_path.display())); - } - if input_file_result.unwrap().read_to_string(&mut input).is_err() { - return Err(format!("Failed to read input file {}.", input_file_path.display())); - } + let mut input_file = try!(result_override(File::open(input_file_path), format!("Failed to open input file {}.", input_file_path.display()))); + try!(result_override(input_file.read_to_string(&mut input), format!("Failed to read input file {}.", input_file_path.display()))); } return Ok(input); } diff --git a/src/main.rs b/src/main.rs index bc74b5b..f80acab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ mod args; mod config; mod process; mod input; +mod utils; #[cfg(test)] mod tests; @@ -16,7 +17,7 @@ mod tests; fn main() { let args = args::get_matches(); if args.subcommand_name().unwrap() == "build" { - let mut config = config::get_config().unwrap(); + let mut config = config::get_config().expect("Config error"); config.verbosity = args::get_verbose(args); process::build(config); } diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..016920b --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,15 @@ +use std::fmt::Debug; + +pub fn result_override(r: Result, msg: String) -> Result { + return match r { + Ok(t) => Ok(t), + Err(_) => Err(msg) + }; +} + +pub fn result_prefix(r: Result, prefix: String) -> Result { + return match r { + Ok(t) => Ok(t), + Err(e) => Err(format!("{}: {:?}", prefix, e)) + }; +}