diff options
author | Ryan Leckey <leckey.ryan@gmail.com> | 2017-02-07 17:14:15 -0800 |
---|---|---|
committer | Ryan Leckey <leckey.ryan@gmail.com> | 2017-02-07 17:14:15 -0800 |
commit | 0c2569271a83728a73f201e98d3d1d4c4fa00c18 (patch) | |
tree | 58165572324e11478348e72a254fc43bbe6be5f0 /src | |
parent | 6c568726be3680a98bfff24a67e8bc3082a0cc90 (diff) | |
parent | 2107b3f0b58a111f42ce1a2d6c7d318155912f05 (diff) |
Merge branch 'master' of https://github.com/mehcode/config-rs
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 61 | ||||
-rw-r--r-- | src/value.rs | 37 |
2 files changed, 92 insertions, 6 deletions
diff --git a/src/config.rs b/src/config.rs index 1de8f44..4c064de 100644 --- a/src/config.rs +++ b/src/config.rs @@ -284,6 +284,10 @@ impl Config { pub fn get_map<'a>(&'a self, key: &str) -> Option<&'a HashMap<String, Value>> { self.get(key).and_then(Value::as_map) } + + pub fn get_slice<'a>(&'a self, key: &str) -> Option<&'a [Value]> { + self.get(key).and_then(Value::as_slice) + } } #[cfg(test)] @@ -440,9 +444,41 @@ mod test { assert_eq!(c.get_bool("key_11"), None); } - // Deep merge of tables #[test] - fn test_merge() { + fn test_slice() { + let mut c = Config::new(); + + c.set("values", vec![ + Value::Integer(10), + Value::Integer(325), + Value::Integer(12), + ]); + + let values = c.get_slice("values").unwrap(); + + assert_eq!(values.len(), 3); + assert_eq!(values[1].as_int(), Some(325)); + } + + #[test] + fn test_slice_into() { + let mut c = Config::new(); + + c.set("values", vec![ + 10, + 325, + 12, + ]); + + let values = c.get_slice("values").unwrap(); + + assert_eq!(values.len(), 3); + assert_eq!(values[1].as_int(), Some(325)); + + } + + #[test] + fn test_map() { let mut c = Config::new(); { @@ -477,7 +513,6 @@ mod test { } } - // Path expression #[test] fn test_path() { use file::{File, FileFormat}; @@ -497,4 +532,24 @@ mod test { assert_eq!(c.get_str("databases[0].name").unwrap(), "test_db"); assert_eq!(c.get_str("databases[0].options.trace").unwrap(), "true"); } + + #[test] + fn test_map_into() { + let mut c = Config::new(); + + { + let mut m = HashMap::new(); + m.insert("port".into(), 6379); + m.insert("db".into(), 2); + + c.set("redis", m).unwrap(); + } + + { + let m = c.get_map("redis").unwrap(); + + assert_eq!(m.get("port").unwrap().as_int().unwrap(), 6379); + assert_eq!(m.get("db").unwrap().as_int().unwrap(), 2); + } + } } diff --git a/src/value.rs b/src/value.rs index 4451400..1630d85 100644 --- a/src/value.rs +++ b/src/value.rs @@ -80,6 +80,13 @@ impl Value { _ => None, } } + /// Gets the underlying type as a slice; only works if the type is actually a slice. + pub fn as_slice(&self) -> Option<&[Value]> { + match *self { + Value::Array(ref value) => Some(value), + _ => None + } + } } // Generalized construction from type into variant is needed @@ -115,8 +122,32 @@ impl From<bool> for Value { } } -impl From<HashMap<String, Value>> for Value { - fn from(value: HashMap<String, Value>) -> Value { - Value::Table(value) +// impl From<HashMap<String, Value>> for Value { +// fn from(value: HashMap<String, Value>) -> Value { +// Value::Table(value) +// } +// } + +impl<T> From<HashMap<String, T>> for Value where T: Into<Value> { + fn from(values: HashMap<String, T>) -> Value { + let mut r = HashMap::new(); + + for (k, v) in values { + r.insert(k.clone(), v.into()); + } + + Value::Table(r) + } +} + +impl<T> From<Vec<T>> for Value where T: Into<Value> { + fn from(values: Vec<T>) -> Value { + let mut l = Vec::new(); + + for v in values { + l.push(v.into()); + } + + Value::Array(l) } } |