Add validator for existing input files

This commit is contained in:
Jake Howard 2017-07-18 13:56:41 +01:00
parent 240069f42c
commit c9ccc393a6
Signed by: jake
GPG key ID: 57AFB45680EDD477

View file

@ -3,6 +3,8 @@ use std::vec::Vec;
pub type ValidationResult = Result<(), String>; pub type ValidationResult = Result<(), String>;
use config::read;
fn check_required_keys(config: &Value) -> ValidationResult { fn check_required_keys(config: &Value) -> ValidationResult {
for key in vec!( for key in vec!(
@ -11,7 +13,17 @@ fn check_required_keys(config: &Value) -> ValidationResult {
"title" "title"
).iter() { ).iter() {
if config.get(key).is_none() { if config.get(key).is_none() {
return Err("Missing key".into()); return Err(format!("Missing required key {}.", key));
}
}
return Ok(());
}
fn check_input_files(config: &Value) -> ValidationResult {
let files = read::get_input_files(config);
for file in files.iter() {
if !file.exists() || !file.is_file() {
return Err(format!("Cannot find input file at {}.", file.as_path().display()));
} }
} }
return Ok(()); return Ok(());
@ -31,6 +43,7 @@ pub fn unwrap_group(config: &Value, funcs: Vec<&Fn(&Value) -> ValidationResult>)
pub fn validate(config: &Value) -> ValidationResult { pub fn validate(config: &Value) -> ValidationResult {
return unwrap_group(config, vec!( return unwrap_group(config, vec!(
&check_required_keys &check_required_keys,
&check_input_files
)); ));
} }