Type checking for config
This commit is contained in:
parent
79e6c7cfcc
commit
cf6aacc4c3
3 changed files with 68 additions and 9 deletions
|
@ -6,6 +6,8 @@ use std::collections::HashMap;
|
|||
pub mod read;
|
||||
pub mod validate;
|
||||
pub mod consts;
|
||||
pub mod validate_types;
|
||||
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||
pub struct Config {
|
||||
|
|
|
@ -4,6 +4,7 @@ use std::vec::Vec;
|
|||
pub type ValidationResult = Result<(), String>;
|
||||
|
||||
use config::read;
|
||||
use config::validate_types::check_config_types;
|
||||
|
||||
|
||||
fn check_required_keys(config: &Value) -> ValidationResult {
|
||||
|
@ -16,10 +17,6 @@ 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() {
|
||||
|
@ -31,10 +28,6 @@ fn check_input_files(config: &Value) -> ValidationResult {
|
|||
}
|
||||
|
||||
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() {
|
||||
|
@ -66,6 +59,6 @@ pub fn unwrap_group(
|
|||
pub fn validate(config: &Value) -> ValidationResult {
|
||||
return unwrap_group(
|
||||
config,
|
||||
vec![&check_required_keys, &check_input_files, &check_output_files]
|
||||
vec![&check_required_keys, &check_config_types, &check_input_files, &check_output_files]
|
||||
);
|
||||
}
|
||||
|
|
64
src/config/validate_types.rs
Normal file
64
src/config/validate_types.rs
Normal file
|
@ -0,0 +1,64 @@
|
|||
use config::validate::{unwrap_group, ValidationResult};
|
||||
use serde_yaml::Value;
|
||||
|
||||
|
||||
fn check_root(config: &Value) -> ValidationResult {
|
||||
if !config.is_mapping() {
|
||||
return Err("Config should be a mapping".into());
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
fn check_input(config: &Value) -> ValidationResult {
|
||||
let input = config.get("input").unwrap();
|
||||
if !input.is_sequence() {
|
||||
return Err("Input must be sequence".into());
|
||||
}
|
||||
|
||||
if input.as_sequence().into_iter().count() == 0 {
|
||||
return Err("Must provide input files".into());
|
||||
}
|
||||
|
||||
for input_file in input.as_sequence().unwrap() {
|
||||
if !input_file.is_string() {
|
||||
return Err("Input must be string".into());
|
||||
}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
fn check_output(config: &Value) -> ValidationResult {
|
||||
let output = config.get("output").unwrap();
|
||||
if !output.is_mapping() {
|
||||
return Err("Output must be mapping".into());
|
||||
}
|
||||
|
||||
if output.as_mapping().into_iter().count() == 0 {
|
||||
return Err("Must provide output files".into());
|
||||
}
|
||||
|
||||
for output_def in output.as_mapping().unwrap() {
|
||||
if !output_def.0.is_string() {
|
||||
return Err("Output keys must be strings".into());
|
||||
}
|
||||
if !output_def.1.is_string() {
|
||||
return Err("Output values must be strings".into());
|
||||
}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
fn check_title(config: &Value) -> ValidationResult {
|
||||
if !config.get("title").unwrap().is_string() {
|
||||
return Err("Title should be a string".into());
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
|
||||
pub fn check_config_types(config: &Value) -> ValidationResult {
|
||||
return unwrap_group(
|
||||
config,
|
||||
vec![&check_root, &check_input, &check_output, &check_title]
|
||||
);
|
||||
}
|
Reference in a new issue