Try to parse the cli commands
This commit is contained in:
parent
5426c6186e
commit
d2a22d6ee1
|
@ -2,6 +2,6 @@
|
||||||
.vscode
|
.vscode
|
||||||
|
|
||||||
# Debug related directories that we don't want included
|
# Debug related directories that we don't want included
|
||||||
/torrents
|
/torrents*
|
||||||
/output
|
/output
|
||||||
config.toml
|
config.toml
|
|
@ -1,4 +1,5 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::slice::Iter;
|
||||||
|
|
||||||
use figment::{Provider, Metadata, Profile, Error};
|
use figment::{Provider, Metadata, Profile, Error};
|
||||||
use figment::value::{Map, Dict, Value, Tag};
|
use figment::value::{Map, Dict, Value, Tag};
|
||||||
|
@ -49,7 +50,58 @@ impl Provider for CliProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fetch<'a, T: Deserialize<'a>>(args: &Vec<std::string::String>) -> Result<T, Error> {
|
fn parse_keys(keys: &mut Iter<&str>, dict: &Dict, vals: &Vec<String>) -> Value {
|
||||||
|
let key = keys.next();
|
||||||
|
|
||||||
|
match key {
|
||||||
|
None => {
|
||||||
|
if vals.len() == 1 {
|
||||||
|
parse_from_string(&vals[0])
|
||||||
|
} else {
|
||||||
|
let mut values = Vec::new();
|
||||||
|
for val in vals.iter() {
|
||||||
|
values.push(parse_from_string(val));
|
||||||
|
}
|
||||||
|
|
||||||
|
Value::Array(Tag::Default, values)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Some(key) => {
|
||||||
|
let key = key.to_string();
|
||||||
|
println!("Key is {}", key);
|
||||||
|
|
||||||
|
println!("Dict is {:?}", dict);
|
||||||
|
|
||||||
|
match dict.get(&key) {
|
||||||
|
Some(val) => {
|
||||||
|
println!("Val is {:?}", val);
|
||||||
|
|
||||||
|
match val.as_dict() {
|
||||||
|
Some(dict) => parse_keys(keys, &dict, vals),
|
||||||
|
None => panic!("Expected a `Dict`, got some other value"),
|
||||||
|
}
|
||||||
|
//parse_keys(keys, &dict, vals)
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
let mut current_dict = Dict::new();
|
||||||
|
let val = parse_keys(keys, ¤t_dict, vals);
|
||||||
|
|
||||||
|
current_dict.insert(key.to_string(), val);
|
||||||
|
|
||||||
|
Value::from(current_dict)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* let mut current_dict = Dict::new();
|
||||||
|
let val = parse_keys(keys, ¤t_dict, vals);
|
||||||
|
|
||||||
|
current_dict.insert(key.to_string(), val);
|
||||||
|
|
||||||
|
Value::from(current_dict) */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_cli(args: &Vec<std::string::String>)-> Result<Dict, Error> {
|
||||||
let (args, argv) = argmap::parse(args.iter());
|
let (args, argv) = argmap::parse(args.iter());
|
||||||
|
|
||||||
let mut dict = Dict::new();
|
let mut dict = Dict::new();
|
||||||
|
@ -60,11 +112,18 @@ impl Provider for CliProvider {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let key_vec: Vec<&str> = key.split(".").collect();
|
let key_vec = key.split(".").collect::<Vec<_>>();
|
||||||
for key in key_vec.iter() {
|
if key_vec.len() > 1 {
|
||||||
dict.insert(key.to_owned(), Value::from(key.to_owned()));
|
let mut key_iter = key_vec.iter();
|
||||||
}
|
|
||||||
|
|
||||||
|
//let key = key_iter.next();
|
||||||
|
let key = key_vec.first();
|
||||||
|
let val = parse_keys(&mut key_iter, &dict, &vals);
|
||||||
|
|
||||||
|
println!("Final val is {:?}", val);
|
||||||
|
|
||||||
|
dict.insert(key.unwrap().to_string(), val);
|
||||||
|
} else {
|
||||||
if len == 1 {
|
if len == 1 {
|
||||||
dict.insert(key, parse_from_string(&vals[0]));
|
dict.insert(key, parse_from_string(&vals[0]));
|
||||||
} else {
|
} else {
|
||||||
|
@ -77,18 +136,22 @@ impl Provider for CliProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(T::deserialize(dict).unwrap())
|
println!("Dict: {:?}", dict);
|
||||||
|
}
|
||||||
|
|
||||||
//Ok(T::deserialize(args.unwrap_or(&std::env::args()))?)
|
|
||||||
|
|
||||||
//Profile::default()
|
|
||||||
|
Ok(dict)
|
||||||
}
|
}
|
||||||
|
|
||||||
match &self.profile {
|
match &self.profile {
|
||||||
// Don't nest: `fetch` into a `Dict`.
|
// Don't nest: `fetch` into a `Dict`.
|
||||||
Some(profile) => Ok(profile.collect(fetch(&self.args)?)),
|
Some(profile) => Ok(profile.collect(parse_cli(&self.args)?)),
|
||||||
// Nest: `fetch` into a `Map<Profile, Dict>`.
|
None => {
|
||||||
None => fetch(&self.args),
|
let mut map = Map::new();
|
||||||
|
map.insert(Profile::default(), parse_cli(&self.args)?);
|
||||||
|
Ok(map)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -49,9 +49,9 @@ impl Config {
|
||||||
// TODO: Figure out priority
|
// TODO: Figure out priority
|
||||||
// Merge the config files
|
// Merge the config files
|
||||||
let figment = Figment::new()
|
let figment = Figment::new()
|
||||||
.join(Toml::file(format!("{}.toml", path)))
|
.join(CliProvider::new())
|
||||||
.join(Env::prefixed("CROSS_SEED_"))
|
.join(Env::prefixed("CROSS_SEED_"))
|
||||||
.join(CliProvider::new());
|
.join(Toml::file(format!("{}.toml", path)));
|
||||||
|
|
||||||
let mut config: Config = figment.extract().unwrap();
|
let mut config: Config = figment.extract().unwrap();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue