Add initial validator
This commit is contained in:
parent
c8656cb89c
commit
5b0bf034c7
2 changed files with 26 additions and 13 deletions
|
@ -7,17 +7,14 @@ pub mod consts;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
raw: String,
|
|
||||||
input: Vec<String>,
|
input: Vec<String>,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
fn new(raw: String) -> Config {
|
fn new(raw: Value) -> Config {
|
||||||
let raw_conf: Value = serde_yaml::from_str(&raw).unwrap();
|
|
||||||
return Config {
|
return Config {
|
||||||
raw: raw,
|
input: read::get_inputs(raw),
|
||||||
input: read::get_inputs(raw_conf),
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -26,6 +23,7 @@ impl Config {
|
||||||
|
|
||||||
pub fn get_config() -> Config {
|
pub fn get_config() -> Config {
|
||||||
let config_str = read::read();
|
let config_str = read::read();
|
||||||
|
let config_value: Value = serde_yaml::from_str(&config_str).unwrap();
|
||||||
return Config::new(config_str);
|
validate::validate(&config_value).expect("Validation Error");
|
||||||
|
return Config::new(config_value);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,24 @@
|
||||||
use config::Config;
|
use serde_yaml::Value;
|
||||||
|
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
|
|
||||||
|
pub type ValidationResult = Result<(), String>;
|
||||||
|
|
||||||
pub fn unwrap_group(config: &Config, funcs: Vec<&Fn(&Config) -> Result<(), String>>) -> Result<(), String> {
|
|
||||||
|
fn check_required_keys(config: &Value) -> ValidationResult {
|
||||||
|
for key in vec!(
|
||||||
|
"input",
|
||||||
|
"output",
|
||||||
|
"title"
|
||||||
|
).iter() {
|
||||||
|
if config.get(key).is_none() {
|
||||||
|
return Err("Missing key".into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn unwrap_group(config: &Value, funcs: Vec<&Fn(&Value) -> ValidationResult>) -> ValidationResult {
|
||||||
for func in funcs.iter() {
|
for func in funcs.iter() {
|
||||||
let func_result = func(config);
|
let func_result = func(config);
|
||||||
if func_result.is_err() {
|
if func_result.is_err() {
|
||||||
|
@ -14,8 +29,8 @@ pub fn unwrap_group(config: &Config, funcs: Vec<&Fn(&Config) -> Result<(), Strin
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn validate(config: Config) -> Result<(), String> {
|
pub fn validate(config: &Value) -> ValidationResult {
|
||||||
return unwrap_group(&config, vec!(
|
return unwrap_group(config, vec!(
|
||||||
&|c| { Ok(()) }
|
&check_required_keys
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue