try! all the things

This commit is contained in:
Jake Howard 2017-07-26 15:22:20 +01:00
parent b33f156dea
commit fa601cb023
Signed by: jake
GPG key ID: 57AFB45680EDD477
6 changed files with 28 additions and 39 deletions

View file

@ -2,6 +2,7 @@ use serde_yaml;
use serde_yaml::Value; use serde_yaml::Value;
use std::path::PathBuf; use std::path::PathBuf;
use std::collections::HashMap; use std::collections::HashMap;
use utils::result_prefix;
pub mod read; pub mod read;
pub mod validate; pub mod validate;
@ -30,18 +31,8 @@ impl Config {
pub fn get_config() -> Result<Config, String> { pub fn get_config() -> Result<Config, String> {
let config_str = read::read(); let config_str = try!(read::read());
if config_str.is_err() { let config = try!(result_prefix(serde_yaml::from_str(&config_str), "Config Parse Error".into()));
return Err(config_str.unwrap_err()); try!(result_prefix(validate::validate(&config), "Config Validation Error".into()));
}
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)); return Ok(Config::new(config));
} }

View file

@ -6,7 +6,7 @@ use serde_yaml::Value;
use std::collections::HashMap; use std::collections::HashMap;
use config::consts; use config::consts;
use utils::result_override;
fn get_config_path() -> PathBuf { fn get_config_path() -> PathBuf {
let mut working_dir = current_dir().unwrap(); let mut working_dir = current_dir().unwrap();
@ -14,22 +14,11 @@ fn get_config_path() -> PathBuf {
return working_dir; return working_dir;
} }
pub fn read() -> Result<String, String> { pub fn read() -> Result<String, String> {
let config_path = get_config_path(); let config_path = get_config_path();
if !config_path.is_file() { let mut config_file = try!(result_override(File::open(&config_path), format!("Unable to find config file at {}", config_path.display())));
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(); let mut contents = String::new();
let file_read = config_file.read_to_string(&mut contents); try!(result_override(config_file.read_to_string(&mut contents), format!("Failed to read config file at {}.", config_path.display())));
if file_read.is_err() {
return Err("Failed to read config file".into());
}
return Ok(contents); return Ok(contents);
} }

View file

@ -47,10 +47,7 @@ pub fn unwrap_group(
funcs: Vec<&Fn(&Value) -> ValidationResult>, funcs: Vec<&Fn(&Value) -> ValidationResult>,
) -> ValidationResult { ) -> ValidationResult {
for func in funcs.iter() { for func in funcs.iter() {
let func_result = func(config); try!(func(config));
if func_result.is_err() {
return Err(func_result.unwrap_err());
}
} }
return Ok(()); return Ok(());
} }

View file

@ -1,18 +1,14 @@
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::path::PathBuf; use std::path::PathBuf;
use utils::result_override;
pub fn read_input_files(files: Vec<PathBuf>) -> Result<String, String> { pub fn read_input_files(files: Vec<PathBuf>) -> Result<String, String> {
let mut input = String::new(); let mut input = String::new();
for input_file_path in files.iter() { for input_file_path in files.iter() {
let input_file_result = File::open(input_file_path); let mut input_file = try!(result_override(File::open(input_file_path), format!("Failed to open input file {}.", input_file_path.display())));
if input_file_result.is_err() { try!(result_override(input_file.read_to_string(&mut input), format!("Failed to read input file {}.", input_file_path.display())));
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 Ok(input); return Ok(input);
} }

View file

@ -9,6 +9,7 @@ mod args;
mod config; mod config;
mod process; mod process;
mod input; mod input;
mod utils;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
@ -16,7 +17,7 @@ mod tests;
fn main() { fn main() {
let args = args::get_matches(); let args = args::get_matches();
if args.subcommand_name().unwrap() == "build" { 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); config.verbosity = args::get_verbose(args);
process::build(config); process::build(config);
} }

15
src/utils.rs Normal file
View file

@ -0,0 +1,15 @@
use std::fmt::Debug;
pub fn result_override<T, E: Debug>(r: Result<T, E>, msg: String) -> Result<T, String> {
return match r {
Ok(t) => Ok(t),
Err(_) => Err(msg)
};
}
pub fn result_prefix<T: Debug, E: Debug>(r: Result<T, E>, prefix: String) -> Result<T, String> {
return match r {
Ok(t) => Ok(t),
Err(e) => Err(format!("{}: {:?}", prefix, e))
};
}