2017-07-17 09:52:22 +01:00
|
|
|
use serde_yaml;
|
2017-07-18 09:20:39 +01:00
|
|
|
use serde_yaml::Value;
|
2017-07-18 13:39:31 +01:00
|
|
|
use std::path::PathBuf;
|
2017-07-19 19:54:10 +01:00
|
|
|
use std::collections::HashMap;
|
2017-07-17 09:52:22 +01:00
|
|
|
|
|
|
|
pub mod read;
|
|
|
|
pub mod validate;
|
2017-07-17 09:59:54 +01:00
|
|
|
pub mod consts;
|
2017-07-19 22:10:47 +01:00
|
|
|
pub mod validate_types;
|
|
|
|
|
2017-07-17 09:52:22 +01:00
|
|
|
|
2017-07-18 09:20:39 +01:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Default)]
|
2017-07-17 09:52:22 +01:00
|
|
|
pub struct Config {
|
2017-07-20 09:14:48 +01:00
|
|
|
pub input: Vec<PathBuf>,
|
|
|
|
pub output: HashMap<String, PathBuf>,
|
2017-07-20 09:24:04 +01:00
|
|
|
pub title: String,
|
2017-07-20 09:31:19 +01:00
|
|
|
pub verbosity: u64
|
2017-07-17 09:52:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2017-07-18 09:40:03 +01:00
|
|
|
fn new(raw: Value) -> Config {
|
2017-07-18 09:20:39 +01:00
|
|
|
return Config {
|
2017-07-18 13:56:27 +01:00
|
|
|
input: read::get_input_files(&raw),
|
2017-07-19 19:54:10 +01:00
|
|
|
output: read::get_output_files(&raw),
|
2017-07-19 20:06:57 +01:00
|
|
|
title: read::get_string(&raw, "title"),
|
2017-07-18 09:20:39 +01:00
|
|
|
..Default::default()
|
|
|
|
};
|
2017-07-17 14:53:18 +01:00
|
|
|
}
|
2017-07-17 09:52:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-21 19:25:39 +01:00
|
|
|
pub fn get_config() -> Result<Config, String> {
|
2017-07-17 09:52:22 +01:00
|
|
|
let config_str = read::read();
|
2017-07-21 19:25:39 +01:00
|
|
|
if config_str.is_err() {
|
|
|
|
return Err(config_str.unwrap_err());
|
|
|
|
}
|
|
|
|
let config_value = serde_yaml::from_str(&config_str.unwrap());
|
|
|
|
if config_value.is_err() {
|
|
|
|
return Err(format!("Failed to parse config. {}", config_value.unwrap_err()));
|
|
|
|
}
|
|
|
|
let config = config_value.unwrap();
|
|
|
|
let validation_output = validate::validate(&config);
|
|
|
|
if validation_output.is_err() {
|
|
|
|
return Err(format!("Validation error: {}", validation_output.unwrap_err()));
|
|
|
|
};
|
|
|
|
return Ok(Config::new(config));
|
2017-07-17 09:52:22 +01:00
|
|
|
}
|