Override the numeric hints to not deserialize ints into floats, and vice versa

This commit is contained in:
Erick Tryzelaar 2015-08-13 08:24:14 -07:00
parent e5efc8801d
commit 8c33064c2c

View file

@ -3,13 +3,6 @@ use Value;
use super::{Decoder, DecodeError, DecodeErrorKind}; use super::{Decoder, DecodeError, DecodeErrorKind};
use std::collections::BTreeMap; use std::collections::BTreeMap;
struct MapVisitor<'a, I> {
iter: I,
toml: &'a mut Option<Value>,
key: Option<String>,
value: Option<Value>,
}
fn se2toml(err: de::value::Error, ty: &'static str) -> DecodeError { fn se2toml(err: de::value::Error, ty: &'static str) -> DecodeError {
match err { match err {
de::value::Error::SyntaxError => de::Error::syntax(ty), de::value::Error::SyntaxError => de::Error::syntax(ty),
@ -59,7 +52,7 @@ impl de::Deserializer for Decoder {
Some(Value::Table(t)) => { Some(Value::Table(t)) => {
visitor.visit_map(MapVisitor { visitor.visit_map(MapVisitor {
iter: t.into_iter(), iter: t.into_iter(),
toml: &mut self.toml, de: self,
key: None, key: None,
value: None, value: None,
}) })
@ -68,6 +61,86 @@ impl de::Deserializer for Decoder {
} }
} }
fn visit_isize<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor
{
self.visit_i64(visitor)
}
fn visit_i8<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor
{
self.visit_i64(visitor)
}
fn visit_i16<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor
{
self.visit_i64(visitor)
}
fn visit_i32<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor
{
self.visit_i64(visitor)
}
fn visit_i64<V>(&mut self, mut visitor: V) -> Result<V::Value, DecodeError>
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<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor
{
self.visit_i64(visitor)
}
fn visit_u8<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor
{
self.visit_i64(visitor)
}
fn visit_u16<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor
{
self.visit_i64(visitor)
}
fn visit_u32<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor
{
self.visit_i64(visitor)
}
fn visit_u64<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor
{
self.visit_i64(visitor)
}
fn visit_f32<V>(&mut self, visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor
{
self.visit_f64(visitor)
}
fn visit_f64<V>(&mut self, mut visitor: V) -> Result<V::Value, DecodeError>
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<V>(&mut self, mut visitor: V) -> Result<V::Value, DecodeError> fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value, DecodeError>
where V: de::Visitor where V: de::Visitor
{ {