From 4357840e95f3646494ddeea4aae12425dfab2db8 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Thu, 1 Jun 2017 22:06:50 -0700 Subject: Handle Option in deserializer --- lib/src/config.rs | 21 +++++++++------------ lib/src/de.rs | 28 ++++++++++++++++++++++------ 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(&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::::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(self, visitor: V) -> Result - 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(self, visitor: V) -> Result + 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) -> 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(&mut self, seed: K) -> Result> - 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(&mut self, seed: V) -> Result - where V: de::DeserializeSeed, + where V: de::DeserializeSeed { de::DeserializeSeed::deserialize(seed, self.elements.remove(0).1) } -- cgit v1.2.3