summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Leckey <leckey.ryan@gmail.com>2017-02-02 23:16:53 -0800
committerRyan Leckey <leckey.ryan@gmail.com>2017-02-02 23:16:53 -0800
commit2107b3f0b58a111f42ce1a2d6c7d318155912f05 (patch)
tree43f9c894f4816def6d1e72f165ea04944fcd2bc1
parent115fe07e2c11aa72e91a5ce9b028ed1c1ff7d806 (diff)
Add slice support and auto conversion of hash/slice sets
-rw-r--r--src/config.rs60
-rw-r--r--src/value.rs37
2 files changed, 92 insertions, 5 deletions
diff --git a/src/config.rs b/src/config.rs
index 126d31b..e5369ae 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -236,6 +236,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)]
@@ -392,9 +396,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();
{
@@ -428,4 +464,24 @@ mod test {
assert_eq!(m.get("db").unwrap().as_str().unwrap(), "1");
}
}
+
+ #[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 5228d91..528e1a9 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)
}
}