diff options
author | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2015-08-13 08:24:14 -0700 |
---|---|---|
committer | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2015-08-13 08:25:56 -0700 |
commit | 8c33064c2c5290c41f3f27b528224cad22b1c9de (patch) | |
tree | 6e7b19b7068689743a91936b5e161f6ffd57a39f /src/decoder | |
parent | e5efc8801d737033d7a766107c4a84ac2bb33b36 (diff) | |
download | milf-rs-8c33064c2c5290c41f3f27b528224cad22b1c9de.tar.gz milf-rs-8c33064c2c5290c41f3f27b528224cad22b1c9de.zip |
Override the numeric hints to not deserialize ints into floats, and vice versa
Diffstat (limited to 'src/decoder')
-rw-r--r-- | src/decoder/serde.rs | 89 |
1 files 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<Value>, - key: Option<String>, - value: Option<Value>, -} - 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<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> where V: de::Visitor { |