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/value.rs | |
parent | 6c568726be3680a98bfff24a67e8bc3082a0cc90 (diff) | |
parent | 2107b3f0b58a111f42ce1a2d6c7d318155912f05 (diff) |
Merge branch 'master' of https://github.com/mehcode/config-rs
Diffstat (limited to 'src/value.rs')
-rw-r--r-- | src/value.rs | 37 |
1 files changed, 34 insertions, 3 deletions
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) } } |