From 8c33064c2c5290c41f3f27b528224cad22b1c9de Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 13 Aug 2015 08:24:14 -0700 Subject: [PATCH] Override the numeric hints to not deserialize ints into floats, and vice versa --- src/decoder/serde.rs | 89 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 81 insertions(+), 8 deletions(-) diff --git a/src/decoder/serde.rs b/src/decoder/serde.rs index 2f69eb9..be4e3bc 100644 --- a/src/decoder/serde.rs +++ b/src/decoder/serde.rs @@ -3,13 +3,6 @@ use Value; use super::{Decoder, DecodeError, DecodeErrorKind}; use std::collections::BTreeMap; -struct MapVisitor<'a, I> { - iter: I, - toml: &'a mut Option, - key: Option, - value: Option, -} - fn se2toml(err: de::value::Error, ty: &'static str) -> DecodeError { match err { de::value::Error::SyntaxError => de::Error::syntax(ty), @@ -59,7 +52,7 @@ impl de::Deserializer for Decoder { Some(Value::Table(t)) => { visitor.visit_map(MapVisitor { iter: t.into_iter(), - toml: &mut self.toml, + de: self, key: None, value: None, }) @@ -68,6 +61,86 @@ impl de::Deserializer for Decoder { } } + fn visit_isize(&mut self, visitor: V) -> Result + where V: de::Visitor + { + self.visit_i64(visitor) + } + + fn visit_i8(&mut self, visitor: V) -> Result + where V: de::Visitor + { + self.visit_i64(visitor) + } + fn visit_i16(&mut self, visitor: V) -> Result + where V: de::Visitor + { + self.visit_i64(visitor) + } + + fn visit_i32(&mut self, visitor: V) -> Result + where V: de::Visitor + { + self.visit_i64(visitor) + } + + fn visit_i64(&mut self, mut visitor: V) -> Result + where V: de::Visitor + { + match self.toml.take() { + Some(Value::Integer(f)) => { + visitor.visit_i64(f).map_err(|e| se2toml(e, "integer")) + } + ref found => Err(self.mismatch("integer", found)), + } + } + + fn visit_usize(&mut self, visitor: V) -> Result + where V: de::Visitor + { + self.visit_i64(visitor) + } + + fn visit_u8(&mut self, visitor: V) -> Result + where V: de::Visitor + { + self.visit_i64(visitor) + } + fn visit_u16(&mut self, visitor: V) -> Result + where V: de::Visitor + { + self.visit_i64(visitor) + } + + fn visit_u32(&mut self, visitor: V) -> Result + where V: de::Visitor + { + self.visit_i64(visitor) + } + + fn visit_u64(&mut self, visitor: V) -> Result + where V: de::Visitor + { + self.visit_i64(visitor) + } + + fn visit_f32(&mut self, visitor: V) -> Result + where V: de::Visitor + { + self.visit_f64(visitor) + } + + fn visit_f64(&mut self, mut visitor: V) -> Result + where V: de::Visitor + { + match self.toml.take() { + Some(Value::Float(f)) => { + visitor.visit_f64(f).map_err(|e| se2toml(e, "float")) + } + ref found => Err(self.mismatch("float", found)), + } + } + fn visit_option(&mut self, mut visitor: V) -> Result where V: de::Visitor {