summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Leckey <leckey.ryan@gmail.com>2017-07-30 13:46:04 -0700
committerRyan Leckey <leckey.ryan@gmail.com>2017-07-30 13:46:04 -0700
commit011b809e104242f7fe9f6c00a54804b1cf0e125c (patch)
tree6b8753a73d950e9f517f5a20109026f66bafa849
parent14224be23dc2f253a240b85214927d97e1160669 (diff)
Impl Config for Source to allow merging whole configs; closes #28
-rw-r--r--src/config.rs10
-rw-r--r--tests/merge.rs20
2 files changed, 30 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs
index 0ff2601..f890461 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -190,3 +190,13 @@ impl Config {
self.get(key).and_then(Value::into_array)
}
}
+
+impl Source for Config {
+ fn clone_into_box(&self) -> Box<Source + Send + Sync> {
+ Box::new((*self).clone())
+ }
+
+ fn collect(&self) -> Result<HashMap<String, Value>> {
+ self.cache.clone().into_table()
+ }
+}
diff --git a/tests/merge.rs b/tests/merge.rs
index f83eef8..d6eb9e0 100644
--- a/tests/merge.rs
+++ b/tests/merge.rs
@@ -25,3 +25,23 @@ fn test_merge() {
);
assert_eq!(c.get("place.rating").ok(), Some(4.9));
}
+
+#[test]
+fn test_merge_whole_config() {
+ let mut c1 = Config::default();
+ let mut c2 = Config::default();
+
+ c1.set("x", 10).unwrap();
+ c2.set("y", 25).unwrap();
+
+ assert_eq!(c1.get("x").ok(), Some(10));
+ assert_eq!(c2.get::<()>("x").ok(), None);
+
+ assert_eq!(c2.get("y").ok(), Some(25));
+ assert_eq!(c1.get::<()>("y").ok(), None);
+
+ c1.merge(c2).unwrap();
+
+ assert_eq!(c1.get("x").ok(), Some(10));
+ assert_eq!(c1.get("y").ok(), Some(25));
+}