Add validator for existing input files
This commit is contained in:
parent
240069f42c
commit
c9ccc393a6
1 changed files with 15 additions and 2 deletions
|
@ -3,6 +3,8 @@ use std::vec::Vec;
|
|||
|
||||
pub type ValidationResult = Result<(), String>;
|
||||
|
||||
use config::read;
|
||||
|
||||
|
||||
fn check_required_keys(config: &Value) -> ValidationResult {
|
||||
for key in vec!(
|
||||
|
@ -11,7 +13,17 @@ fn check_required_keys(config: &Value) -> ValidationResult {
|
|||
"title"
|
||||
).iter() {
|
||||
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(());
|
||||
|
@ -31,6 +43,7 @@ pub fn unwrap_group(config: &Value, funcs: Vec<&Fn(&Value) -> ValidationResult>)
|
|||
|
||||
pub fn validate(config: &Value) -> ValidationResult {
|
||||
return unwrap_group(config, vec!(
|
||||
&check_required_keys
|
||||
&check_required_keys,
|
||||
&check_input_files
|
||||
));
|
||||
}
|
||||
|
|
Reference in a new issue