From b33f156deadd3fe5fa1e788811ececbb34bef733 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 21 Jul 2017 19:25:39 +0100 Subject: [PATCH] Result-ify things --- src/config/mod.rs | 18 ++++++++++++++---- src/config/read.rs | 19 +++++++++++++++---- src/input.rs | 13 +++++++++---- src/main.rs | 2 +- src/process.rs | 2 +- 5 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 7abef0e..6d5c17d 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -29,9 +29,19 @@ impl Config { } -pub fn get_config() -> Config { +pub fn get_config() -> Result { let config_str = read::read(); - let config_value: Value = serde_yaml::from_str(&config_str).unwrap(); - validate::validate(&config_value).expect("Validation Error"); - return Config::new(config_value); + 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())); + }; + return Ok(Config::new(config)); } diff --git a/src/config/read.rs b/src/config/read.rs index 940736c..dc3b968 100644 --- a/src/config/read.rs +++ b/src/config/read.rs @@ -15,11 +15,22 @@ fn get_config_path() -> PathBuf { } -pub fn read() -> String { - let mut config_file = File::open(get_config_path()).expect("Failed to open file"); +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 contents = String::new(); - config_file.read_to_string(&mut contents).expect("Failed to read file"); - return contents; + let file_read = config_file.read_to_string(&mut contents); + if file_read.is_err() { + return Err("Failed to read config file".into()); + } + return Ok(contents); } diff --git a/src/input.rs b/src/input.rs index a8d7e8d..a4d17a5 100644 --- a/src/input.rs +++ b/src/input.rs @@ -3,11 +3,16 @@ use std::io::Read; use std::path::PathBuf; -pub fn read_input_files(files: Vec) -> String { +pub fn read_input_files(files: Vec) -> Result { let mut input = String::new(); for input_file_path in files.iter() { - let mut input_file = File::open(input_file_path).expect("Unable to open file"); - input_file.read_to_string(&mut input).expect("Failed to read file"); + 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())); + } } - return input; + return Ok(input); } diff --git a/src/main.rs b/src/main.rs index fb5119b..bc74b5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ mod tests; fn main() { let args = args::get_matches(); if args.subcommand_name().unwrap() == "build" { - let mut config = config::get_config(); + let mut config = config::get_config().unwrap(); config.verbosity = args::get_verbose(args); process::build(config); } diff --git a/src/process.rs b/src/process.rs index 363e959..b1be3d8 100644 --- a/src/process.rs +++ b/src/process.rs @@ -3,6 +3,6 @@ use config::Config; pub fn build(config: Config) { - let input = read_input_files(config.input); + let input = read_input_files(config.input).unwrap(); println!("{}", input); }