Construct config with basic defaults
This commit is contained in:
parent
c83d847051
commit
6d389c6fbd
3 changed files with 23 additions and 8 deletions
|
@ -1,24 +1,31 @@
|
|||
use serde_yaml;
|
||||
use serde_yaml::Value;
|
||||
|
||||
pub mod read;
|
||||
pub mod validate;
|
||||
pub mod consts;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||
pub struct Config {
|
||||
foo: String
|
||||
raw: String,
|
||||
input: Vec<String>,
|
||||
|
||||
}
|
||||
|
||||
impl Config {
|
||||
fn new(raw: String) -> Config {
|
||||
return serde_yaml::from_str(&raw).unwrap();
|
||||
let raw_conf: Value = serde_yaml::from_str(&raw).unwrap();
|
||||
return Config {
|
||||
raw: raw,
|
||||
input: read::get_inputs(raw_conf),
|
||||
..Default::default()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn get_config() {
|
||||
pub fn get_config() -> Config {
|
||||
let config_str = read::read();
|
||||
let config = Config::new(config_str);
|
||||
println!("{:?}", config);
|
||||
validate::validate(config);
|
||||
|
||||
return Config::new(config_str);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ use std::env::current_dir;
|
|||
use std::path::PathBuf;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use serde_yaml::Value;
|
||||
|
||||
use config::consts;
|
||||
|
||||
|
@ -19,3 +20,9 @@ pub fn read() -> String {
|
|||
config_file.read_to_string(&mut contents).expect("Failed to read file");
|
||||
return contents;
|
||||
}
|
||||
|
||||
|
||||
pub fn get_inputs(conf: Value) -> Vec<String> {
|
||||
let input_values = conf.get("input").unwrap().as_sequence().unwrap().to_vec();
|
||||
return input_values.into_iter().map(|x| x.as_str().unwrap().to_string()).collect();
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ mod tests;
|
|||
fn main() {
|
||||
let args = args::get_matches();
|
||||
if args.subcommand_name().unwrap() == "build" {
|
||||
config::get_config();
|
||||
println!("{:?}", config::get_config());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue