archive
/
md-pdf-rs
Archived
1
Fork 0

Result-ify things

This commit is contained in:
Jake Howard 2017-07-21 19:25:39 +01:00
parent 8c8a05ae58
commit b33f156dea
Signed by: jake
GPG Key ID: 57AFB45680EDD477
5 changed files with 40 additions and 14 deletions

View File

@ -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_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));
}

View File

@ -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<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 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);
}

View File

@ -3,11 +3,16 @@ use std::io::Read;
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();
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);
}

View File

@ -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);
}

View File

@ -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);
}