Clean up the enum parsing code.

This commit is contained in:
Erick Tryzelaar 2014-05-30 22:19:22 -07:00
parent 5c0f324f6d
commit a7b224702c

26
json.rs
View File

@ -1867,21 +1867,18 @@ impl<T: Iterator<char>> de::Deserializer<ParserError> for Parser<T> {
// parse, but objects require us to preparse the entire object because
// we can't guarantee the order of the map.
match token {
let variant = match token {
de::String(variant) => {
match variants.iter().position(|v| *v == variant.as_slice()) {
Some(idx) => {
// Make sure to terminate the enum.
self.tokens.push_front(de::End);
Ok(idx)
}
None => self.error(UnknownVariant),
}
variant
}
de::MapStart(len) => {
let mut variant = None;
let mut fields = None;
// Extract all the fields.
loop {
let field = match try!(self.expect_token()) {
de::End => { break; }
@ -1907,6 +1904,9 @@ impl<T: Iterator<char>> de::Deserializer<ParserError> for Parser<T> {
_ => { return self.error(MissingField); }
};
// Add all the field's tokens to our buffer. We need to skip
// over the `SeqStart` because we're pretending we're an
// `EnumStart`.
let mut iter = fields.move_iter();
match iter.next() {
Some(de::SeqStart(_)) => { }
@ -1914,14 +1914,18 @@ impl<T: Iterator<char>> de::Deserializer<ParserError> for Parser<T> {
}
self.tokens.extend(iter);
variant
}
_ => { return self.error(InvalidSyntax); }
};
match variants.iter().position(|v| *v == variant.as_slice()) {
Some(idx) => Ok(idx),
Some(idx) => {
Ok(idx)
}
None => self.error(UnknownVariant),
}
}
_ => self.error(InvalidSyntax),
}
}
}
/*