From c62e229dfdef7d89c8007c1d1fed0bd1ae37ec81 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 19 Jul 2017 21:22:07 +0100 Subject: [PATCH] Validate output formats --- src/config/validate.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/config/validate.rs b/src/config/validate.rs index db472af..901a011 100644 --- a/src/config/validate.rs +++ b/src/config/validate.rs @@ -16,7 +16,12 @@ fn check_required_keys(config: &Value) -> ValidationResult { } fn check_input_files(config: &Value) -> ValidationResult { + match config.get("input").unwrap() { + &Value::Sequence(_) => (), + _ => return Err("Input must be sequence".into()) + } 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())); @@ -25,6 +30,24 @@ fn check_input_files(config: &Value) -> ValidationResult { return Ok(()); } +fn check_output_files(config: &Value) -> ValidationResult { + match config.get("output").unwrap() { + &Value::Mapping(_) => (), + _ => return Err("Output must be mapping".into()) + } + let files = read::get_output_files(config); + let output_types = vec!["pdf".into()]; + for file_def in files.iter() { + let dir = file_def.1.parent().unwrap(); + if !dir.exists() || !dir.is_dir() { + return Err(format!("Cannot find output directory at {}.", dir.display())); + } + if !output_types.contains(file_def.0) { + return Err(format!("Invalid output type {}.", file_def.0)); + } + } + return Ok(()); +} pub fn unwrap_group( config: &Value, @@ -41,5 +64,5 @@ pub fn unwrap_group( pub fn validate(config: &Value) -> ValidationResult { - return unwrap_group(config, vec![&check_required_keys, &check_input_files]); + return unwrap_group(config, vec![&check_required_keys, &check_input_files, &check_output_files]); }