summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Leckey <ryan@launchbadge.com>2017-06-01 22:06:50 -0700
committerRyan Leckey <ryan@launchbadge.com>2017-06-01 22:06:50 -0700
commit4357840e95f3646494ddeea4aae12425dfab2db8 (patch)
tree81b6b1312a906e5be1693e34c9675a5b9b7f5d6b
parent6e71949a7f7895bba255d26d71777f2e5f5d8789 (diff)
Handle Option<T> in deserializer
-rw-r--r--lib/src/config.rs21
-rw-r--r--lib/src/de.rs28
2 files changed, 31 insertions, 18 deletions
diff --git a/lib/src/config.rs b/lib/src/config.rs
index 0767f87..93d7fc0 100644
--- a/lib/src/config.rs
+++ b/lib/src/config.rs
@@ -43,7 +43,8 @@ pub struct Config {
impl Config {
/// Merge in a configuration property source.
pub fn merge<T>(&mut self, source: T) -> Result<()>
- where T: 'static, T: Source + Send + Sync
+ where T: 'static,
+ T: Source + Send + Sync
{
match self.kind {
ConfigKind::Mutable { ref mut sources, .. } => {
@@ -65,14 +66,12 @@ impl Config {
/// operation (`set`, `merge`, `set_default`, etc.).
pub fn refresh(&mut self) -> Result<()> {
self.cache = match self.kind {
- ConfigKind::Mutable { ref overrides, ref sources, ref defaults } => {
- let mut cache = Value::new(None, HashMap::<String, Value>::new());
-
- // HACK!
- cache = sources[0].collect()?;
-
- cache
- }
+ // TODO: We need to actually merge in all the stuff
+ ConfigKind::Mutable {
+ ref overrides,
+ ref sources,
+ ref defaults,
+ } => sources[0].collect()?,
ConfigKind::Frozen => {
return Err(ConfigError::Frozen);
@@ -95,9 +94,7 @@ impl Config {
T::deserialize(value)
}
- None => {
- Err(ConfigError::NotFound(key.into()))
- }
+ None => Err(ConfigError::NotFound(key.into())),
}
}
}
diff --git a/lib/src/de.rs b/lib/src/de.rs
index 369d6de..89a3bcf 100644
--- a/lib/src/de.rs
+++ b/lib/src/de.rs
@@ -11,7 +11,7 @@ impl de::Deserializer for Value {
#[inline]
fn deserialize<V>(self, visitor: V) -> Result<V::Value>
- where V: de::Visitor,
+ where V: de::Visitor
{
// Deserialize based on the underlying type
match self.kind {
@@ -21,14 +21,27 @@ impl de::Deserializer for Value {
ValueKind::String(s) => visitor.visit_string(s),
ValueKind::Array(values) => unimplemented!(),
ValueKind::Table(map) => visitor.visit_map(MapVisitor::new(map)),
- _ => { unimplemented!(); }
+ _ => {
+ unimplemented!();
+ }
+ }
+ }
+
+ #[inline]
+ fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
+ where V: de::Visitor
+ {
+ // Match an explicit nil as None and everything else as Some
+ match self.kind {
+ ValueKind::Nil => visitor.visit_none(),
+ _ => visitor.visit_some(self),
}
}
forward_to_deserialize! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
seq_fixed_size bytes byte_buf map struct unit enum newtype_struct
- struct_field ignored_any unit_struct tuple_struct tuple option
+ struct_field ignored_any unit_struct tuple_struct tuple
}
}
@@ -62,7 +75,10 @@ struct MapVisitor {
impl MapVisitor {
fn new(mut table: HashMap<String, Value>) -> Self {
- MapVisitor { elements: table.drain().collect(), index: 0 }
+ MapVisitor {
+ elements: table.drain().collect(),
+ index: 0,
+ }
}
}
@@ -70,7 +86,7 @@ impl de::MapVisitor for MapVisitor {
type Error = ConfigError;
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
- where K: de::DeserializeSeed,
+ where K: de::DeserializeSeed
{
if self.index >= self.elements.len() {
return Ok(None);
@@ -84,7 +100,7 @@ impl de::MapVisitor for MapVisitor {
}
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
- where V: de::DeserializeSeed,
+ where V: de::DeserializeSeed
{
de::DeserializeSeed::deserialize(seed, self.elements.remove(0).1)
}