From d02e62233088df15fd43b86aa97e0695b375a46b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 7 Mar 2016 23:48:01 -0800 Subject: WIP --- src/encoder/mod.rs | 3 +++ src/encoder/serde.rs | 62 ++++++++++++++++++++++++++++------------------------ 2 files changed, 37 insertions(+), 28 deletions(-) (limited to 'src/encoder') diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs index cdfe8e2..fb628fa 100644 --- a/src/encoder/mod.rs +++ b/src/encoder/mod.rs @@ -61,6 +61,8 @@ pub enum Error { /// Indicates that a type other than a string was attempted to be used as a /// map key type. InvalidMapKeyType, + /// A custom error type was generated + Custom(String), } #[derive(PartialEq)] @@ -202,6 +204,7 @@ impl fmt::Display for Error { at this location"), Error::InvalidMapKeyType => write!(f, "only strings can be used as \ key types"), + Error::Custom(ref s) => write!(f, "custom error: {}", s), } } } diff --git a/src/encoder/serde.rs b/src/encoder/serde.rs index 3b50181..d5bbb77 100644 --- a/src/encoder/serde.rs +++ b/src/encoder/serde.rs @@ -5,33 +5,33 @@ use super::{Encoder, Error}; impl ser::Serializer for Encoder { type Error = Error; - fn visit_bool(&mut self, v: bool) -> Result<(), Error> { + fn serialize_bool(&mut self, v: bool) -> Result<(), Error> { self.emit_value(Value::Boolean(v)) } - fn visit_i64(&mut self, v: i64) -> Result<(), Error> { + fn serialize_i64(&mut self, v: i64) -> Result<(), Error> { self.emit_value(Value::Integer(v)) } - fn visit_u64(&mut self, v: u64) -> Result<(), Error> { - self.visit_i64(v as i64) + fn serialize_u64(&mut self, v: u64) -> Result<(), Error> { + self.serialize_i64(v as i64) } - fn visit_f64(&mut self, v: f64) -> Result<(), Error> { + fn serialize_f64(&mut self, v: f64) -> Result<(), Error> { self.emit_value(Value::Float(v)) } - fn visit_str(&mut self, value: &str) -> Result<(), Error> { + fn serialize_str(&mut self, value: &str) -> Result<(), Error> { self.emit_value(Value::String(value.to_string())) } - fn visit_unit(&mut self) -> Result<(), Error> { + fn serialize_unit(&mut self) -> Result<(), Error> { Ok(()) } - fn visit_none(&mut self) -> Result<(), Error> { + fn serialize_none(&mut self) -> Result<(), Error> { self.emit_none() } - fn visit_some(&mut self, value: V) -> Result<(), Error> + fn serialize_some(&mut self, value: V) -> Result<(), Error> where V: ser::Serialize { value.serialize(self) } - fn visit_seq(&mut self, mut visitor: V) -> Result<(), Error> + fn serialize_seq(&mut self, mut visitor: V) -> Result<(), Error> where V: ser::SeqVisitor { self.seq(|me| { @@ -39,12 +39,12 @@ impl ser::Serializer for Encoder { Ok(()) }) } - fn visit_seq_elt(&mut self, value: T) -> Result<(), Error> + fn serialize_seq_elt(&mut self, value: T) -> Result<(), Error> where T: ser::Serialize { value.serialize(self) } - fn visit_map(&mut self, mut visitor: V) -> Result<(), Error> + fn serialize_map(&mut self, mut visitor: V) -> Result<(), Error> where V: ser::MapVisitor { self.table(|me| { @@ -52,26 +52,26 @@ impl ser::Serializer for Encoder { Ok(()) }) } - fn visit_map_elt(&mut self, key: K, value: V) -> Result<(), Error> + fn serialize_map_elt(&mut self, key: K, value: V) -> Result<(), Error> where K: ser::Serialize, V: ser::Serialize { try!(self.table_key(|me| key.serialize(me))); try!(value.serialize(self)); Ok(()) } - fn visit_newtype_struct(&mut self, - _name: &'static str, - value: T) -> Result<(), Self::Error> + fn serialize_newtype_struct(&mut self, + _name: &'static str, + value: T) -> Result<(), Self::Error> where T: ser::Serialize, { // Don't serialize the newtype struct in a tuple. value.serialize(self) } - fn visit_newtype_variant(&mut self, - _name: &'static str, - _variant_index: usize, - _variant: &'static str, - value: T) -> Result<(), Self::Error> + fn serialize_newtype_variant(&mut self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + value: T) -> Result<(), Self::Error> where T: ser::Serialize, { // Don't serialize the newtype struct variant in a tuple. @@ -84,19 +84,25 @@ impl ser::Serialize for Value { where E: ser::Serializer { match *self { - Value::String(ref s) => e.visit_str(s), - Value::Integer(i) => e.visit_i64(i), - Value::Float(f) => e.visit_f64(f), - Value::Boolean(b) => e.visit_bool(b), - Value::Datetime(ref s) => e.visit_str(s), + Value::String(ref s) => e.serialize_str(s), + Value::Integer(i) => e.serialize_i64(i), + Value::Float(f) => e.serialize_f64(f), + Value::Boolean(b) => e.serialize_bool(b), + Value::Datetime(ref s) => e.serialize_str(s), Value::Array(ref a) => { - e.visit_seq(ser::impls::SeqIteratorVisitor::new(a.iter(), + e.serialize_seq(ser::impls::SeqIteratorVisitor::new(a.iter(), Some(a.len()))) } Value::Table(ref t) => { - e.visit_map(ser::impls::MapIteratorVisitor::new(t.iter(), + e.serialize_map(ser::impls::MapIteratorVisitor::new(t.iter(), Some(t.len()))) } } } } + +impl ser::Error for Error { + fn custom>(msg: T) -> Error { + Error::Custom(msg.into()) + } +} -- cgit v1.2.3