Clean up some logic/formatting in JSON module

This commit is contained in:
Corey Farwell 2014-11-23 12:08:11 -05:00
parent 22513fed35
commit 02355b8726

View File

@ -1166,7 +1166,7 @@ impl Stack {
/// at the top. /// at the top.
pub fn get<'l>(&'l self, idx: uint) -> StackElement<'l> { pub fn get<'l>(&'l self, idx: uint) -> StackElement<'l> {
match self.stack[idx] { match self.stack[idx] {
InternalIndex(i) => { Index(i) } InternalIndex(i) => Index(i),
InternalKey(start, size) => { InternalKey(start, size) => {
Key(str::from_utf8( Key(str::from_utf8(
self.str_buffer[start as uint .. start as uint + size as uint]).unwrap()) self.str_buffer[start as uint .. start as uint + size as uint]).unwrap())
@ -1643,10 +1643,10 @@ impl<T: Iterator<char>> Parser<T> {
fn parse_start(&mut self) -> JsonEvent { fn parse_start(&mut self) -> JsonEvent {
let val = self.parse_value(); let val = self.parse_value();
self.state = match val { self.state = match val {
Error(_) => { ParseFinished } Error(_) => ParseFinished,
ArrayStart => { ParseArray(true) } ArrayStart => ParseArray(true),
ObjectStart => { ParseObject(true) } ObjectStart => ParseObject(true),
_ => { ParseBeforeFinish } _ => ParseBeforeFinish,
}; };
return val; return val;
} }
@ -1654,33 +1654,31 @@ impl<T: Iterator<char>> Parser<T> {
fn parse_array(&mut self, first: bool) -> JsonEvent { fn parse_array(&mut self, first: bool) -> JsonEvent {
if self.ch_is(']') { if self.ch_is(']') {
if !first { if !first {
return self.error_event(InvalidSyntax); self.error_event(InvalidSyntax)
}
if self.stack.is_empty() {
self.state = ParseBeforeFinish;
} else { } else {
self.state = if self.stack.last_is_index() { self.state = if self.stack.is_empty() {
ParseBeforeFinish
} else if self.stack.last_is_index() {
ParseArrayComma ParseArrayComma
} else { } else {
ParseObjectComma ParseObjectComma
} };
}
self.bump(); self.bump();
return ArrayEnd; ArrayEnd
} }
} else {
if first { if first {
self.stack.push_index(0); self.stack.push_index(0);
} }
let val = self.parse_value(); let val = self.parse_value();
self.state = match val { self.state = match val {
Error(_) => { ParseFinished } Error(_) => ParseFinished,
ArrayStart => { ParseArray(true) } ArrayStart => ParseArray(true),
ObjectStart => { ParseObject(true) } ObjectStart => ParseObject(true),
_ => { ParseArrayComma } _ => ParseArrayComma,
}; };
return val; val
}
} }
fn parse_array_comma_or_end(&mut self) -> Option<JsonEvent> { fn parse_array_comma_or_end(&mut self) -> Option<JsonEvent> {
@ -1688,24 +1686,22 @@ impl<T: Iterator<char>> Parser<T> {
self.stack.bump_index(); self.stack.bump_index();
self.state = ParseArray(false); self.state = ParseArray(false);
self.bump(); self.bump();
return None; None
} else if self.ch_is(']') { } else if self.ch_is(']') {
self.stack.pop(); self.stack.pop();
if self.stack.is_empty() { self.state = if self.stack.is_empty() {
self.state = ParseBeforeFinish; ParseBeforeFinish
} else { } else if self.stack.last_is_index() {
self.state = if self.stack.last_is_index() {
ParseArrayComma ParseArrayComma
} else { } else {
ParseObjectComma ParseObjectComma
} };
}
self.bump(); self.bump();
return Some(ArrayEnd); Some(ArrayEnd)
} else if self.eof() { } else if self.eof() {
return Some(self.error_event(EOFWhileParsingArray)); Some(self.error_event(EOFWhileParsingArray))
} else { } else {
return Some(self.error_event(InvalidSyntax)); Some(self.error_event(InvalidSyntax))
} }
} }
@ -1718,15 +1714,13 @@ impl<T: Iterator<char>> Parser<T> {
self.stack.pop(); self.stack.pop();
} }
} }
if self.stack.is_empty() { self.state = if self.stack.is_empty() {
self.state = ParseBeforeFinish; ParseBeforeFinish
} else { } else if self.stack.last_is_index() {
self.state = if self.stack.last_is_index() {
ParseArrayComma ParseArrayComma
} else { } else {
ParseObjectComma ParseObjectComma
} };
}
self.bump(); self.bump();
return ObjectEnd; return ObjectEnd;
} }
@ -1737,7 +1731,7 @@ impl<T: Iterator<char>> Parser<T> {
return self.error_event(KeyMustBeAString); return self.error_event(KeyMustBeAString);
} }
let s = match self.parse_str() { let s = match self.parse_str() {
Ok(s) => { s } Ok(s) => s,
Err(e) => { Err(e) => {
self.state = ParseFinished; self.state = ParseFinished;
return Error(e); return Error(e);
@ -1756,25 +1750,23 @@ impl<T: Iterator<char>> Parser<T> {
let val = self.parse_value(); let val = self.parse_value();
self.state = match val { self.state = match val {
Error(_) => { ParseFinished } Error(_) => ParseFinished,
ArrayStart => { ParseArray(true) } ArrayStart => ParseArray(true),
ObjectStart => { ParseObject(true) } ObjectStart => ParseObject(true),
_ => { ParseObjectComma } _ => ParseObjectComma,
}; };
return val; return val;
} }
fn parse_object_end(&mut self) -> JsonEvent { fn parse_object_end(&mut self) -> JsonEvent {
if self.ch_is('}') { if self.ch_is('}') {
if self.stack.is_empty() { self.state = if self.stack.is_empty() {
self.state = ParseBeforeFinish; ParseBeforeFinish
} else { } else if self.stack.last_is_index() {
self.state = if self.stack.last_is_index() {
ParseArrayComma ParseArrayComma
} else { } else {
ParseObjectComma ParseObjectComma
} };
}
self.bump(); self.bump();
ObjectEnd ObjectEnd
} else if self.eof() { } else if self.eof() {
@ -1852,23 +1844,23 @@ impl<T: Iterator<char>> Builder<T> {
} }
fn build_value(&mut self) -> Result<Json, BuilderError> { fn build_value(&mut self) -> Result<Json, BuilderError> {
return match self.token { match self.token {
Some(NullValue) => { Ok(Null) } Some(NullValue) => Ok(Null),
Some(I64Value(n)) => { Ok(I64(n)) } Some(I64Value(n)) => Ok(I64(n)),
Some(U64Value(n)) => { Ok(U64(n)) } Some(U64Value(n)) => Ok(U64(n)),
Some(F64Value(n)) => { Ok(F64(n)) } Some(F64Value(n)) => Ok(F64(n)),
Some(BooleanValue(b)) => { Ok(Boolean(b)) } Some(BooleanValue(b)) => Ok(Boolean(b)),
Some(StringValue(ref mut s)) => { Some(StringValue(ref mut s)) => {
let mut temp = string::String::new(); let mut temp = string::String::new();
swap(s, &mut temp); swap(s, &mut temp);
Ok(String(temp)) Ok(String(temp))
} }
Some(Error(e)) => { Err(e) } Some(Error(e)) => Err(e),
Some(ArrayStart) => { self.build_array() } Some(ArrayStart) => self.build_array(),
Some(ObjectStart) => { self.build_object() } Some(ObjectStart) => self.build_object(),
Some(ObjectEnd) => { self.parser.error(InvalidSyntax) } Some(ObjectEnd) => self.parser.error(InvalidSyntax),
Some(ArrayEnd) => { self.parser.error(InvalidSyntax) } Some(ArrayEnd) => self.parser.error(InvalidSyntax),
None => { self.parser.error(EOFWhileParsingValue) } None => self.parser.error(EOFWhileParsingValue),
} }
} }