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)]
|
||||
pub struct Config {
|
||||
raw: String,
|
||||
input: Vec<String>,
|
||||
|
||||
}
|
||||
|
||||
impl Config {
|
||||
fn new(raw: String) -> Config {
|
||||
let raw_conf: Value = serde_yaml::from_str(&raw).unwrap();
|
||||
fn new(raw: Value) -> Config {
|
||||
return Config {
|
||||
raw: raw,
|
||||
input: read::get_inputs(raw_conf),
|
||||
input: read::get_inputs(raw),
|
||||
..Default::default()
|
||||
};
|
||||
}
|
||||
|
@ -26,6 +23,7 @@ impl Config {
|
|||
|
||||
pub fn get_config() -> Config {
|
||||
let config_str = read::read();
|
||||
|
||||
return Config::new(config_str);
|
||||
let config_value: Value = serde_yaml::from_str(&config_str).unwrap();
|
||||
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;
|
||||
|
||||
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() {
|
||||
let func_result = func(config);
|
||||
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> {
|
||||
return unwrap_group(&config, vec!(
|
||||
&|c| { Ok(()) }
|
||||
pub fn validate(config: &Value) -> ValidationResult {
|
||||
return unwrap_group(config, vec!(
|
||||
&check_required_keys
|
||||
));
|
||||
}
|
||||
|
|
Reference in a new issue