diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-01-28 19:40:58 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-02-09 13:45:13 +0100 |
commit | 7fec8a1b23e3b94ce98f9b42c442cae9868132dc (patch) | |
tree | 71cdf434dded51a37b2759a784954beec4a737ef /imag-store | |
parent | 947d7900bf48a724aa7717e067ea4e81d4aa82ce (diff) |
Move commandline-header parsing code to utility module
as the code can be re-used in the update() implementation.
Diffstat (limited to 'imag-store')
-rw-r--r-- | imag-store/src/create.rs | 62 | ||||
-rw-r--r-- | imag-store/src/main.rs | 1 | ||||
-rw-r--r-- | imag-store/src/util.rs | 68 |
3 files changed, 70 insertions, 61 deletions
diff --git a/imag-store/src/create.rs b/imag-store/src/create.rs index 8752c84a..bb3eaad0 100644 --- a/imag-store/src/create.rs +++ b/imag-store/src/create.rs @@ -1,24 +1,20 @@ -use std::collections::BTreeMap; use std::path::PathBuf; use std::io::stdin; use std::fs::OpenOptions; use std::result::Result as RResult; use std::io::Read; use std::ops::DerefMut; -use std::str::Split; use clap::ArgMatches; -use toml::Table; -use toml::Value; use libimagrt::runtime::Runtime; use libimagstore::store::Entry; use libimagstore::store::EntryHeader; -use libimagutil::key_value_split::IntoKeyValue; use error::StoreError; use error::StoreErrorKind; use util::build_entry_path; +use util::build_toml_header; type Result<T> = RResult<T, StoreError>; @@ -128,59 +124,3 @@ fn entry_from_raw(raw_src: &str) -> String { content } -fn build_toml_header(matches: &ArgMatches, header: EntryHeader) -> EntryHeader { - if let Some(headerspecs) = matches.values_of("header") { - let mut main = BTreeMap::new(); - for tpl in headerspecs.into_iter().filter_map(|hs| String::from(hs).into_kv()) { - let (key, value) = tpl.into(); - let mut split = key.split("."); - let current = split.next(); - if current.is_some() { - insert_key_into(String::from(current.unwrap()), &mut split, value, &mut main); - } - } - } - header -} - -fn insert_key_into(current: String, - rest_path: &mut Split<&str>, - value: String, - map: &mut BTreeMap<String, Value>) { - let next = rest_path.next(); - - if next.is_none() { - map.insert(current, parse_value(value)); - } else { - if map.contains_key(¤t) { - match map.get_mut(¤t).unwrap() { - &mut Value::Table(ref mut t) => { - insert_key_into(String::from(next.unwrap()), rest_path, value, t); - }, - _ => unreachable!(), - } - } else { - let mut submap = BTreeMap::new(); - insert_key_into(String::from(next.unwrap()), rest_path, value, &mut submap); - map.insert(current, Value::Table(submap)); - } - } -} - -fn parse_value(value: String) -> Value { - fn is_ary(v: &String) -> bool { - v.chars().next() == Some('[') && v.chars().last() == Some(']') && v.len() >= 3 - } - - if value == "true" { - Value::Boolean(true) - } else if value == "false" { - Value::Boolean(false) - } else if is_ary(&value) { - let sub = &value[1..(value.len()-1)]; - Value::Array(sub.split(",").map(|v| parse_value(String::from(v))).collect()) - } else { - Value::String(value) - } -} - diff --git a/imag-store/src/main.rs b/imag-store/src/main.rs index 950228e2..103ef44f 100644 --- a/imag-store/src/main.rs +++ b/imag-store/src/main.rs @@ -16,6 +16,7 @@ mod create; mod retrieve; mod update; mod delete; +mod util; use ui::build_ui; use create::create; diff --git a/imag-store/src/util.rs b/imag-store/src/util.rs new file mode 100644 index 00000000..3a15165b --- /dev/null +++ b/imag-store/src/util.rs @@ -0,0 +1,68 @@ +use std::collections::BTreeMap; +use std::str::Split; + +use clap::ArgMatches; +use toml::Value; + +use libimagstore::store::EntryHeader; +use libimagutil::key_value_split::IntoKeyValue; + +pub fn build_toml_header(matches: &ArgMatches, header: EntryHeader) -> EntryHeader { + debug!("Building header from cli spec"); + if let Some(headerspecs) = matches.values_of("header") { + let mut main = BTreeMap::new(); + for tpl in headerspecs.into_iter().filter_map(|hs| String::from(hs).into_kv()) { + let (key, value) = tpl.into(); + debug!("Splitting: {:?}", key); + let mut split = key.split("."); + let current = split.next(); + if current.is_some() { + insert_key_into(String::from(current.unwrap()), &mut split, value, &mut main); + } + } + } + header +} + +fn insert_key_into(current: String, + rest_path: &mut Split<&str>, + value: String, + map: &mut BTreeMap<String, Value>) { + let next = rest_path.next(); + + if next.is_none() { + map.insert(current, parse_value(value)); + } else { + if map.contains_key(¤t) { + match map.get_mut(¤t).unwrap() { + &mut Value::Table(ref mut t) => { + insert_key_into(String::from(next.unwrap()), rest_path, value, t); + }, + _ => unreachable!(), + } + } else { + let mut submap = BTreeMap::new(); + insert_key_into(String::from(next.unwrap()), rest_path, value, &mut submap); + map.insert(current, Value::Table(submap)); + } + } +} + +fn parse_value(value: String) -> Value { + fn is_ary(v: &String) -> bool { + v.chars().next() == Some('[') && v.chars().last() == Some(']') && v.len() >= 3 + } + + debug!("Building value out of: {:?}", value); + if value == "true" { + Value::Boolean(true) + } else if value == "false" { + Value::Boolean(false) + } else if is_ary(&value) { + let sub = &value[1..(value.len()-1)]; + Value::Array(sub.split(",").map(|v| parse_value(String::from(v))).collect()) + } else { + Value::String(value) + } +} + |