try! all the things
This commit is contained in:
parent
b33f156dea
commit
fa601cb023
6 changed files with 28 additions and 39 deletions
|
@ -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<Config, String> {
|
||||
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));
|
||||
}
|
||||
|
|
|
@ -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<String, String> {
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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(());
|
||||
}
|
||||
|
|
10
src/input.rs
10
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<PathBuf>) -> Result<String, String> {
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
15
src/utils.rs
Normal file
15
src/utils.rs
Normal 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))
|
||||
};
|
||||
}
|
Reference in a new issue