From 4db8b732f1e316baf5aa8219e1cfd951031a662d Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Tue, 3 Mar 2015 21:35:49 -0800 Subject: [PATCH] Simplify test_de --- serde2/tests/test_de.rs | 257 +--------------------------------------- 1 file changed, 3 insertions(+), 254 deletions(-) diff --git a/serde2/tests/test_de.rs b/serde2/tests/test_de.rs index 6a2987fe..63efdcf2 100644 --- a/serde2/tests/test_de.rs +++ b/serde2/tests/test_de.rs @@ -327,277 +327,26 @@ impl<'a, 'b> de::EnumVisitor for TokenDeserializerEnumVisitor<'a, 'b> { #[derive_deserialize] struct NamedUnit; -////////////////////////////////////////////////////////////////////////// - #[derive(PartialEq, Debug)] +#[derive_deserialize] struct NamedSeq(i32, i32, i32); -impl Deserialize for NamedSeq { - fn deserialize(deserializer: &mut D) -> Result - where D: Deserializer, - { - deserializer.visit(NamedSeqVisitor) - } -} - -struct NamedSeqVisitor; - -impl Visitor for NamedSeqVisitor { - type Value = NamedSeq; - - fn visit_seq(&mut self, mut visitor: V) -> Result - where V: de::SeqVisitor, - { - let a = match try!(visitor.visit()) { - Some(value) => value, - None => { return Err(de::Error::end_of_stream_error()); } - }; - - let b = match try!(visitor.visit()) { - Some(value) => value, - None => { return Err(de::Error::end_of_stream_error()); } - }; - - let c = match try!(visitor.visit()) { - Some(value) => value, - None => { return Err(de::Error::end_of_stream_error()); } - }; - - try!(visitor.end()); - - Ok(NamedSeq(a, b, c)) - } - - fn visit_named_seq(&mut self, name: &str, visitor: V) -> Result - where V: de::SeqVisitor, - { - if name == "NamedSeq" { - self.visit_seq(visitor) - } else { - Err(de::Error::syntax_error()) - } - } -} - -////////////////////////////////////////////////////////////////////////// - #[derive(PartialEq, Debug)] +#[derive_deserialize] struct NamedMap { a: i32, b: i32, c: i32, } -impl Deserialize for NamedMap { - fn deserialize(deserializer: &mut D) -> Result - where D: Deserializer, - { - deserializer.visit(NamedMapVisitor) - } -} - -struct NamedMapVisitor; - -impl Visitor for NamedMapVisitor { - type Value = NamedMap; - - fn visit_map(&mut self, mut visitor: V) -> Result - where V: de::MapVisitor, - { - let mut a = None; - let mut b = None; - let mut c = None; - - while let Some(key) = try!(visitor.visit_key()) { - match key { - NamedMapField::A => { a = Some(try!(visitor.visit_value())); } - NamedMapField::B => { b = Some(try!(visitor.visit_value())); } - NamedMapField::C => { c = Some(try!(visitor.visit_value())); } - } - } - - match (a, b, c) { - (Some(a), Some(b), Some(c)) => Ok(NamedMap { a: a, b: b, c: c }), - _ => Err(de::Error::syntax_error()), - } - } - - fn visit_named_map(&mut self, name: &str, visitor: V) -> Result - where V: de::MapVisitor, - { - if name == "NamedMap" { - self.visit_map(visitor) - } else { - Err(de::Error::syntax_error()) - } - } -} - -enum NamedMapField { - A, - B, - C, -} - -impl Deserialize for NamedMapField { - fn deserialize(deserializer: &mut D) -> Result - where D: Deserializer, -{ - deserializer.visit(NamedMapFieldVisitor) - } -} - -struct NamedMapFieldVisitor; - -impl Visitor for NamedMapFieldVisitor { - type Value = NamedMapField; - - fn visit_str(&mut self, value: &str) -> Result - where E: de::Error, - { - match value { - "a" => Ok(NamedMapField::A), - "b" => Ok(NamedMapField::B), - "c" => Ok(NamedMapField::C), - _ => Err(de::Error::syntax_error()), - } - } -} - -////////////////////////////////////////////////////////////////////////// - #[derive(PartialEq, Debug)] +#[derive_deserialize] enum Enum { Unit, Seq(i32, i32, i32), Map { a: i32, b: i32, c: i32 } } -impl Deserialize for Enum { - fn deserialize(deserializer: &mut D) -> Result - where D: Deserializer, - { - deserializer.visit(EnumVisitor) - } -} - -struct EnumVisitor; - -impl Visitor for EnumVisitor { - type Value = Enum; - - fn visit_enum(&mut self, name: &str, variant: &str, visitor: V) -> Result - where V: de::EnumVisitor, - { - if name == "Enum" { - self.visit_variant(variant, visitor) - } else { - Err(de::Error::syntax_error()) - } - } - - fn visit_variant(&mut self, name: &str, mut visitor: V) -> Result - where V: de::EnumVisitor, - { - match name { - "Unit" => { - try!(visitor.visit_unit()); - Ok(Enum::Unit) - } - "Seq" => visitor.visit_seq(EnumSeqVisitor), - "Map" => visitor.visit_map(EnumMapVisitor), - _ => Err(de::Error::syntax_error()), - } - } -} - -struct EnumSeqVisitor; - -impl de::EnumSeqVisitor for EnumSeqVisitor { - type Value = Enum; - - fn visit(&mut self, mut visitor: V) -> Result - where V: de::SeqVisitor, - { - let a = match try!(visitor.visit()) { - Some(value) => value, - None => { return Err(de::Error::end_of_stream_error()); } - }; - - let b = match try!(visitor.visit()) { - Some(value) => value, - None => { return Err(de::Error::end_of_stream_error()); } - }; - - let c = match try!(visitor.visit()) { - Some(value) => value, - None => { return Err(de::Error::end_of_stream_error()); } - }; - - try!(visitor.end()); - - Ok(Enum::Seq(a, b, c)) - } -} - -struct EnumMapVisitor; - -impl de::EnumMapVisitor for EnumMapVisitor { - type Value = Enum; - - fn visit(&mut self, mut visitor: V) -> Result - where V: de::MapVisitor, - { - let mut a = None; - let mut b = None; - let mut c = None; - - while let Some(key) = try!(visitor.visit_key()) { - match key { - EnumMapField::A => { a = Some(try!(visitor.visit_value())); } - EnumMapField::B => { b = Some(try!(visitor.visit_value())); } - EnumMapField::C => { c = Some(try!(visitor.visit_value())); } - } - } - - match (a, b, c) { - (Some(a), Some(b), Some(c)) => Ok(Enum::Map { a: a, b: b, c: c }), - _ => Err(de::Error::syntax_error()), - } - } -} - -enum EnumMapField { - A, - B, - C, -} - -impl Deserialize for EnumMapField { - fn deserialize(deserializer: &mut D) -> Result - where D: Deserializer, - { - deserializer.visit(EnumMapFieldVisitor) - } -} - -struct EnumMapFieldVisitor; - -impl Visitor for EnumMapFieldVisitor { - type Value = EnumMapField; - - fn visit_str(&mut self, value: &str) -> Result - where E: de::Error, - { - match value { - "a" => Ok(EnumMapField::A), - "b" => Ok(EnumMapField::B), - "c" => Ok(EnumMapField::C), - _ => Err(de::Error::syntax_error()), - } - } -} - ////////////////////////////////////////////////////////////////////////// macro_rules! btreemap {