Unnamed=>Seq, Named=>Map

This commit is contained in:
Erick Tryzelaar 2015-01-09 23:22:11 -08:00
parent 75d1de0ced
commit 3bcfbea1bc
2 changed files with 44 additions and 44 deletions

View File

@ -1130,8 +1130,8 @@ mod tests {
#[derive(PartialEq, Show)] #[derive(PartialEq, Show)]
enum Enum { enum Enum {
Unit, Unit,
Unnamed(i32, i32, i32), Seq(i32, i32, i32),
Named { a: i32, b: i32, c: i32 } Map { a: i32, b: i32, c: i32 }
} }
impl Deserialize for Enum { impl Deserialize for Enum {
@ -1159,16 +1159,16 @@ mod tests {
try!(visitor.visit_unit()); try!(visitor.visit_unit());
Ok(Enum::Unit) Ok(Enum::Unit)
} }
"Unnamed" => visitor.visit_seq(&mut EnumUnnamedVisitor), "Seq" => visitor.visit_seq(&mut EnumSeqVisitor),
"Named" => visitor.visit_map(&mut EnumNamedVisitor), "Map" => visitor.visit_map(&mut EnumMapVisitor),
_ => Err(super::Error::syntax_error()), _ => Err(super::Error::syntax_error()),
} }
} }
} }
struct EnumUnnamedVisitor; struct EnumSeqVisitor;
impl super::EnumSeqVisitor for EnumUnnamedVisitor { impl super::EnumSeqVisitor for EnumSeqVisitor {
type Value = Enum; type Value = Enum;
fn visit< fn visit<
@ -1191,13 +1191,13 @@ mod tests {
try!(visitor.end()); try!(visitor.end());
Ok(Enum::Unnamed(a, b, c)) Ok(Enum::Seq(a, b, c))
} }
} }
struct EnumNamedVisitor; struct EnumMapVisitor;
impl super::EnumMapVisitor for EnumNamedVisitor { impl super::EnumMapVisitor for EnumMapVisitor {
type Value = Enum; type Value = Enum;
fn visit< fn visit<
@ -1209,45 +1209,45 @@ mod tests {
while let Some(key) = try!(visitor.visit_key()) { while let Some(key) = try!(visitor.visit_key()) {
match key { match key {
EnumNamedField::A => { a = Some(try!(visitor.visit_value())); } EnumMapField::A => { a = Some(try!(visitor.visit_value())); }
EnumNamedField::B => { b = Some(try!(visitor.visit_value())); } EnumMapField::B => { b = Some(try!(visitor.visit_value())); }
EnumNamedField::C => { c = Some(try!(visitor.visit_value())); } EnumMapField::C => { c = Some(try!(visitor.visit_value())); }
} }
} }
match (a, b, c) { match (a, b, c) {
(Some(a), Some(b), Some(c)) => Ok(Enum::Named { a: a, b: b, c: c }), (Some(a), Some(b), Some(c)) => Ok(Enum::Map { a: a, b: b, c: c }),
_ => Err(super::Error::syntax_error()), _ => Err(super::Error::syntax_error()),
} }
} }
} }
enum EnumNamedField { enum EnumMapField {
A, A,
B, B,
C, C,
} }
impl Deserialize for EnumNamedField { impl Deserialize for EnumMapField {
fn deserialize< fn deserialize<
S: Deserializer, S: Deserializer,
>(state: &mut S) -> Result<EnumNamedField, S::Error> { >(state: &mut S) -> Result<EnumMapField, S::Error> {
state.visit(&mut EnumNamedFieldVisitor) state.visit(&mut EnumMapFieldVisitor)
} }
} }
struct EnumNamedFieldVisitor; struct EnumMapFieldVisitor;
impl Visitor for EnumNamedFieldVisitor { impl Visitor for EnumMapFieldVisitor {
type Value = EnumNamedField; type Value = EnumMapField;
fn visit_str< fn visit_str<
E: super::Error, E: super::Error,
>(&mut self, value: &str) -> Result<EnumNamedField, E> { >(&mut self, value: &str) -> Result<EnumMapField, E> {
match value { match value {
"a" => Ok(EnumNamedField::A), "a" => Ok(EnumMapField::A),
"b" => Ok(EnumNamedField::B), "b" => Ok(EnumMapField::B),
"c" => Ok(EnumNamedField::C), "c" => Ok(EnumMapField::C),
_ => Err(super::Error::syntax_error()), _ => Err(super::Error::syntax_error()),
} }
} }
@ -1510,9 +1510,9 @@ mod tests {
Token::EnumEnd, Token::EnumEnd,
], ],
} }
test_enum_unnamed { test_enum_seq {
Enum::Unnamed(1, 2, 3) => vec![ Enum::Seq(1, 2, 3) => vec![
Token::EnumStart("Enum", "Unnamed"), Token::EnumStart("Enum", "Seq"),
Token::SeqStart(3), Token::SeqStart(3),
Token::SeqSep(true), Token::SeqSep(true),
Token::I32(1), Token::I32(1),
@ -1526,9 +1526,9 @@ mod tests {
Token::EnumEnd, Token::EnumEnd,
], ],
} }
test_enum_named { test_enum_map {
Enum::Named { a: 1, b: 2, c: 3 } => vec![ Enum::Map { a: 1, b: 2, c: 3 } => vec![
Token::EnumStart("Enum", "Named"), Token::EnumStart("Enum", "Map"),
Token::MapStart(3), Token::MapStart(3),
Token::MapSep(true), Token::MapSep(true),
Token::Str("a"), Token::Str("a"),

View File

@ -957,8 +957,8 @@ mod tests {
enum Enum { enum Enum {
Unit, Unit,
Unnamed(i32, i32), Seq(i32, i32),
Named { a: i32, b: i32 }, Map { a: i32, b: i32 },
} }
impl Serialize for Enum { impl Serialize for Enum {
@ -969,15 +969,15 @@ mod tests {
Enum::Unit => { Enum::Unit => {
visitor.visit_enum_unit("Enum", "Unit") visitor.visit_enum_unit("Enum", "Unit")
} }
Enum::Unnamed(ref a, ref b) => { Enum::Seq(ref a, ref b) => {
visitor.visit_enum_seq("Enum", "Unnamed", UnnamedVisitor { visitor.visit_enum_seq("Enum", "Seq", EnumSeqVisitor {
a: a, a: a,
b: b, b: b,
state: 0, state: 0,
}) })
} }
Enum::Named { ref a, ref b } => { Enum::Map { ref a, ref b } => {
visitor.visit_enum_map("Enum", "Named", NamedVisitor { visitor.visit_enum_map("Enum", "Map", EnumMapVisitor {
a: a, a: a,
b: b, b: b,
state: 0, state: 0,
@ -987,13 +987,13 @@ mod tests {
} }
} }
struct UnnamedVisitor<'a> { struct EnumSeqVisitor<'a> {
a: &'a i32, a: &'a i32,
b: &'a i32, b: &'a i32,
state: u8, state: u8,
} }
impl<'a> SeqVisitor for UnnamedVisitor<'a> { impl<'a> SeqVisitor for EnumSeqVisitor<'a> {
fn visit< fn visit<
V: Visitor, V: Visitor,
>(&mut self, visitor: &mut V) -> Result<Option<V::Value>, V::Error> { >(&mut self, visitor: &mut V) -> Result<Option<V::Value>, V::Error> {
@ -1017,13 +1017,13 @@ mod tests {
} }
} }
struct NamedVisitor<'a> { struct EnumMapVisitor<'a> {
a: &'a i32, a: &'a i32,
b: &'a i32, b: &'a i32,
state: u8, state: u8,
} }
impl<'a> MapVisitor for NamedVisitor<'a> { impl<'a> MapVisitor for EnumMapVisitor<'a> {
fn visit< fn visit<
V: Visitor, V: Visitor,
>(&mut self, visitor: &mut V) -> Result<Option<V::Value>, V::Error> { >(&mut self, visitor: &mut V) -> Result<Option<V::Value>, V::Error> {
@ -1249,8 +1249,8 @@ mod tests {
} }
test_enum { test_enum {
Enum::Unit => vec![Token::EnumUnit("Enum", "Unit")], Enum::Unit => vec![Token::EnumUnit("Enum", "Unit")],
Enum::Unnamed(1, 2) => vec![ Enum::Seq(1, 2) => vec![
Token::EnumSeqStart("Enum", "Unnamed", 2), Token::EnumSeqStart("Enum", "Seq", 2),
Token::SeqSep(true), Token::SeqSep(true),
Token::I32(1), Token::I32(1),
@ -1258,8 +1258,8 @@ mod tests {
Token::I32(2), Token::I32(2),
Token::SeqEnd, Token::SeqEnd,
], ],
Enum::Named { a: 1, b: 2 } => vec![ Enum::Map { a: 1, b: 2 } => vec![
Token::EnumMapStart("Enum", "Named", 2), Token::EnumMapStart("Enum", "Map", 2),
Token::MapSep(true), Token::MapSep(true),
Token::Str("a"), Token::Str("a"),
Token::I32(1), Token::I32(1),