2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
|
|
|
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
2016-05-02 22:57:41 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::collections::btree_map::{BTreeMap, Entry};
|
2016-01-28 18:40:58 +00:00
|
|
|
use std::str::Split;
|
|
|
|
|
|
|
|
use clap::ArgMatches;
|
|
|
|
use toml::Value;
|
|
|
|
|
|
|
|
use libimagutil::key_value_split::IntoKeyValue;
|
|
|
|
|
2016-11-17 11:41:37 +00:00
|
|
|
pub fn build_toml_header(matches: &ArgMatches, mut header: Value) -> Value {
|
2016-01-28 18:40:58 +00:00
|
|
|
debug!("Building header from cli spec");
|
|
|
|
if let Some(headerspecs) = matches.values_of("header") {
|
2016-01-29 19:14:27 +00:00
|
|
|
let kvs = headerspecs.into_iter()
|
|
|
|
.filter_map(|hs| {
|
|
|
|
debug!("- Processing: '{}'", hs);
|
|
|
|
let kv = String::from(hs).into_kv();
|
|
|
|
debug!("- got: '{:?}'", kv);
|
|
|
|
kv
|
|
|
|
});
|
|
|
|
for tpl in kvs {
|
2016-01-28 18:40:58 +00:00
|
|
|
let (key, value) = tpl.into();
|
|
|
|
debug!("Splitting: {:?}", key);
|
2016-05-02 22:57:41 +00:00
|
|
|
let mut split = key.split('.');
|
2016-11-17 11:41:37 +00:00
|
|
|
match (split.next(), &mut header) {
|
|
|
|
(Some(cur), &mut Value::Table(ref mut hdr)) =>
|
|
|
|
insert_key_into(String::from(cur), &mut split, Cow::Owned(value), hdr),
|
|
|
|
_ => { }
|
2016-01-28 18:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-17 11:41:37 +00:00
|
|
|
|
|
|
|
debug!("Header = {:?}", header);
|
|
|
|
header
|
2016-01-28 18:40:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 22:57:41 +00:00
|
|
|
fn insert_key_into<'a>(current: String,
|
|
|
|
rest_path: &mut Split<char>,
|
|
|
|
value: Cow<'a, str>,
|
2016-01-28 18:40:58 +00:00
|
|
|
map: &mut BTreeMap<String, Value>) {
|
|
|
|
let next = rest_path.next();
|
|
|
|
|
|
|
|
if next.is_none() {
|
2016-01-29 19:14:27 +00:00
|
|
|
debug!("Inserting into {:?} = {:?}", current, value);
|
2016-01-28 18:40:58 +00:00
|
|
|
map.insert(current, parse_value(value));
|
|
|
|
} else {
|
2016-01-29 19:14:27 +00:00
|
|
|
debug!("Inserting into {:?} ... = {:?}", current, value);
|
2016-05-02 22:57:41 +00:00
|
|
|
match map.entry(current) {
|
|
|
|
Entry::Occupied(ref mut e) => {
|
|
|
|
match *e.get_mut() {
|
|
|
|
Value::Table(ref mut t) => {
|
|
|
|
insert_key_into(String::from(next.unwrap()), rest_path, value, t);
|
|
|
|
},
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Entry::Vacant(v) => { v.insert(Value::Table( {
|
|
|
|
let mut submap = BTreeMap::new();
|
|
|
|
insert_key_into(String::from(next.unwrap()), rest_path, value, &mut submap);
|
|
|
|
debug!("Inserting submap = {:?}", submap);
|
|
|
|
submap }));
|
2016-01-28 18:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 22:57:41 +00:00
|
|
|
fn parse_value(value: Cow<str>) -> Value {
|
2016-01-31 11:54:59 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2016-05-02 22:57:41 +00:00
|
|
|
fn is_ary(v: &str) -> bool {
|
|
|
|
v.starts_with('[') && v.ends_with(']') && v.len() >= 3
|
2016-01-28 18:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if value == "true" {
|
2016-01-29 19:14:27 +00:00
|
|
|
debug!("Building Boolean out of: {:?}...", value);
|
2016-01-28 18:40:58 +00:00
|
|
|
Value::Boolean(true)
|
|
|
|
} else if value == "false" {
|
2016-01-29 19:14:27 +00:00
|
|
|
debug!("Building Boolean out of: {:?}...", value);
|
2016-01-28 18:40:58 +00:00
|
|
|
Value::Boolean(false)
|
|
|
|
} else if is_ary(&value) {
|
2016-01-29 19:14:27 +00:00
|
|
|
debug!("Building Array out of: {:?}...", value);
|
2016-01-28 18:40:58 +00:00
|
|
|
let sub = &value[1..(value.len()-1)];
|
2016-05-02 22:57:41 +00:00
|
|
|
Value::Array(sub.split(',').map(|x| parse_value(Cow::from(x))).collect())
|
2016-01-28 18:40:58 +00:00
|
|
|
} else {
|
2016-01-31 11:54:59 +00:00
|
|
|
FromStr::from_str(&value[..])
|
|
|
|
.map(|i: i64| {
|
|
|
|
debug!("Building Integer out of: {:?}...", value);
|
|
|
|
Value::Integer(i)
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
FromStr::from_str(&value[..])
|
|
|
|
.map(|f: f64| {
|
|
|
|
debug!("Building Float out of: {:?}...", value);
|
|
|
|
Value::Float(f)
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
debug!("Building String out of: {:?}...", value);
|
2016-05-02 22:57:41 +00:00
|
|
|
Value::String(value.into_owned())
|
2016-01-31 11:54:59 +00:00
|
|
|
})
|
|
|
|
})
|
2016-01-28 18:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|