Simplify test_de

This commit is contained in:
Erick Tryzelaar 2015-03-03 21:35:49 -08:00
parent 37bc3e9df3
commit 4db8b732f1

View File

@ -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<D>(deserializer: &mut D) -> Result<NamedSeq, D::Error>
where D: Deserializer,
{
deserializer.visit(NamedSeqVisitor)
}
}
struct NamedSeqVisitor;
impl Visitor for NamedSeqVisitor {
type Value = NamedSeq;
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<NamedSeq, V::Error>
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<V>(&mut self, name: &str, visitor: V) -> Result<NamedSeq, V::Error>
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<D>(deserializer: &mut D) -> Result<NamedMap, D::Error>
where D: Deserializer,
{
deserializer.visit(NamedMapVisitor)
}
}
struct NamedMapVisitor;
impl Visitor for NamedMapVisitor {
type Value = NamedMap;
fn visit_map<V>(&mut self, mut visitor: V) -> Result<NamedMap, V::Error>
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<V>(&mut self, name: &str, visitor: V) -> Result<NamedMap, V::Error>
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<D>(deserializer: &mut D) -> Result<NamedMapField, D::Error>
where D: Deserializer,
{
deserializer.visit(NamedMapFieldVisitor)
}
}
struct NamedMapFieldVisitor;
impl Visitor for NamedMapFieldVisitor {
type Value = NamedMapField;
fn visit_str<E>(&mut self, value: &str) -> Result<NamedMapField, E>
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<D>(deserializer: &mut D) -> Result<Enum, D::Error>
where D: Deserializer,
{
deserializer.visit(EnumVisitor)
}
}
struct EnumVisitor;
impl Visitor for EnumVisitor {
type Value = Enum;
fn visit_enum<V>(&mut self, name: &str, variant: &str, visitor: V) -> Result<Enum, V::Error>
where V: de::EnumVisitor,
{
if name == "Enum" {
self.visit_variant(variant, visitor)
} else {
Err(de::Error::syntax_error())
}
}
fn visit_variant<V>(&mut self, name: &str, mut visitor: V) -> Result<Enum, V::Error>
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<V>(&mut self, mut visitor: V) -> Result<Enum, V::Error>
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<V>(&mut self, mut visitor: V) -> Result<Enum, V::Error>
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<D>(deserializer: &mut D) -> Result<EnumMapField, D::Error>
where D: Deserializer,
{
deserializer.visit(EnumMapFieldVisitor)
}
}
struct EnumMapFieldVisitor;
impl Visitor for EnumMapFieldVisitor {
type Value = EnumMapField;
fn visit_str<E>(&mut self, value: &str) -> Result<EnumMapField, E>
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 {