Result-ify things
This commit is contained in:
parent
8c8a05ae58
commit
b33f156dea
5 changed files with 40 additions and 14 deletions
|
@ -29,9 +29,19 @@ impl Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn get_config() -> Config {
|
pub fn get_config() -> Result<Config, String> {
|
||||||
let config_str = read::read();
|
let config_str = read::read();
|
||||||
let config_value: Value = serde_yaml::from_str(&config_str).unwrap();
|
if config_str.is_err() {
|
||||||
validate::validate(&config_value).expect("Validation Error");
|
return Err(config_str.unwrap_err());
|
||||||
return Config::new(config_value);
|
}
|
||||||
|
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));
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,11 +15,22 @@ fn get_config_path() -> PathBuf {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn read() -> String {
|
pub fn read() -> Result<String, String> {
|
||||||
let mut config_file = File::open(get_config_path()).expect("Failed to open file");
|
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();
|
let mut contents = String::new();
|
||||||
config_file.read_to_string(&mut contents).expect("Failed to read file");
|
let file_read = config_file.read_to_string(&mut contents);
|
||||||
return contents;
|
if file_read.is_err() {
|
||||||
|
return Err("Failed to read config file".into());
|
||||||
|
}
|
||||||
|
return Ok(contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
13
src/input.rs
13
src/input.rs
|
@ -3,11 +3,16 @@ use std::io::Read;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
|
||||||
pub fn read_input_files(files: Vec<PathBuf>) -> 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 mut input_file = File::open(input_file_path).expect("Unable to open file");
|
let input_file_result = File::open(input_file_path);
|
||||||
input_file.read_to_string(&mut input).expect("Failed to read file");
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,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();
|
let mut config = config::get_config().unwrap();
|
||||||
config.verbosity = args::get_verbose(args);
|
config.verbosity = args::get_verbose(args);
|
||||||
process::build(config);
|
process::build(config);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,6 @@ use config::Config;
|
||||||
|
|
||||||
|
|
||||||
pub fn build(config: Config) {
|
pub fn build(config: Config) {
|
||||||
let input = read_input_files(config.input);
|
let input = read_input_files(config.input).unwrap();
|
||||||
println!("{}", input);
|
println!("{}", input);
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue