Re-structure control flow a bit + modernization

This commit is contained in:
Alex Crichton 2015-06-07 22:38:36 -07:00
parent 8487b63c97
commit 6580b77a20

View file

@ -254,9 +254,8 @@ impl<'a> Parser<'a> {
let mut keys = Vec::new(); let mut keys = Vec::new();
loop { loop {
self.ws(); self.ws();
match self.key_name() { if let Some(s) = self.key_name() {
Some(s) => keys.push(s), keys.push(s);
None => {}
} }
self.ws(); self.ws();
if self.eat(']') { if self.eat(']') {
@ -293,9 +292,7 @@ impl<'a> Parser<'a> {
self.finish_string(start, false) self.finish_string(start, false)
} else { } else {
let mut ret = String::new(); let mut ret = String::new();
loop { while let Some((_, ch)) = self.cur.clone().next() {
match self.cur.clone().next() {
Some((_, ch)) => {
match ch { match ch {
'a' ... 'z' | 'a' ... 'z' |
'A' ... 'Z' | 'A' ... 'Z' |
@ -304,9 +301,6 @@ impl<'a> Parser<'a> {
_ => break, _ => break,
} }
} }
None => break
}
}
Some(ret) Some(ret)
}; };
match key { match key {
@ -423,9 +417,8 @@ impl<'a> Parser<'a> {
return Some(ret) return Some(ret)
} }
Some((pos, '\\')) => { Some((pos, '\\')) => {
match escape(self, pos, multiline) { if let Some(c) = escape(self, pos, multiline) {
Some(c) => ret.push(c), ret.push(c);
None => {}
} }
} }
Some((pos, ch)) if ch < '\u{1f}' => { Some((pos, ch)) if ch < '\u{1f}' => {
@ -470,14 +463,11 @@ impl<'a> Parser<'a> {
} else { } else {
"invalid" "invalid"
}; };
match u32::from_str_radix(num, 16).ok() { if let Some(n) = u32::from_str_radix(num, 16).ok() {
Some(n) => { if let Some(c) = char::from_u32(n) {
match char::from_u32(n) {
Some(c) => {
me.cur.by_ref().skip(len - 1).next(); me.cur.by_ref().skip(len - 1).next();
return Some(c) return Some(c)
} } else {
None => {
me.errors.push(ParserError { me.errors.push(ParserError {
lo: pos + 1, lo: pos + 1,
hi: pos + 5, hi: pos + 5,
@ -486,9 +476,7 @@ impl<'a> Parser<'a> {
codepoint", n), codepoint", n),
}) })
} }
} } else {
}
None => {
me.errors.push(ParserError { me.errors.push(ParserError {
lo: pos, lo: pos,
hi: pos + 1, hi: pos + 1,
@ -496,7 +484,6 @@ impl<'a> Parser<'a> {
after a `{}` escape", len, c), after a `{}` escape", len, c),
}) })
} }
}
None None
} }
Some((pos, ch)) => { Some((pos, ch)) => {
@ -832,10 +819,7 @@ impl<'a> Parser<'a> {
if tmp.0.contains_key(part) { if tmp.0.contains_key(part) {
match *tmp.0.get_mut(part).unwrap() { match *tmp.0.get_mut(part).unwrap() {
Value::Table(ref mut table) => { Value::Table(ref mut table) => cur = table,
cur = table;
continue
}
Value::Array(ref mut array) => { Value::Array(ref mut array) => {
match array.last_mut() { match array.last_mut() {
Some(&mut Value::Table(ref mut table)) => cur = table, Some(&mut Value::Table(ref mut table)) => cur = table,
@ -849,7 +833,6 @@ impl<'a> Parser<'a> {
return None return None
} }
} }
continue
} }
_ => { _ => {
self.errors.push(ParserError { self.errors.push(ParserError {
@ -861,6 +844,7 @@ impl<'a> Parser<'a> {
return None return None
} }
} }
continue
} }
// Initialize an empty table as part of this sub-key // Initialize an empty table as part of this sub-key
@ -922,11 +906,10 @@ impl<'a> Parser<'a> {
Some(pair) => pair, Some(pair) => pair,
None => return, None => return,
}; };
let key = format!("{}", key); if !into.0.contains_key(key) {
if !into.0.contains_key(&key) { into.0.insert(key.to_owned(), Value::Array(Vec::new()));
into.0.insert(key.clone(), Value::Array(Vec::new()));
} }
match *into.0.get_mut(&key).unwrap() { match *into.0.get_mut(key).unwrap() {
Value::Array(ref mut vec) => { Value::Array(ref mut vec) => {
match vec.first() { match vec.first() {
Some(ref v) if !v.same_type(&value) => { Some(ref v) if !v.same_type(&value) => {