Skip to content

Commit 8da5d6c

Browse files
authored
Reformat heavily nested conditionals (not-fl3#143)
1 parent d886298 commit 8da5d6c

File tree

1 file changed

+50
-66
lines changed

1 file changed

+50
-66
lines changed

src/toml.rs

Lines changed: 50 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,14 @@ impl TomlParser {
416416
}
417417

418418
fn next_tok(&mut self, i: &mut Chars) -> Result<TomlTok, TomlErr> {
419-
while self.cur == '\n' || self.cur == '\r' || self.cur == '\t' || self.cur == ' ' {
419+
while matches!(self.cur, '\n' | '\r' | '\t' | ' ') {
420420
self.next(i);
421421
}
422-
loop {
423-
if self.cur == '\0' {
424-
return Ok(TomlTok::Eof);
425-
}
426422

423+
loop {
427424
#[allow(unreachable_patterns)]
428425
match self.cur {
426+
'\0' => return Ok(TomlTok::Eof),
429427
',' => {
430428
self.next(i);
431429
return Ok(TomlTok::Comma);
@@ -447,11 +445,7 @@ impl TomlParser {
447445
self.next(i);
448446
}
449447

450-
while self.cur == '\n'
451-
|| self.cur == '\r'
452-
|| self.cur == '\t'
453-
|| self.cur == ' '
454-
{
448+
while matches!(self.cur, '\n' | '\r' | '\t' | ' ') {
455449
self.next(i);
456450
}
457451
}
@@ -466,27 +460,26 @@ impl TomlParser {
466460
}
467461
let escaped_string = braces == 3;
468462
loop {
469-
if self.cur == '"' && !escaped_string {
470-
break;
471-
}
472-
if self.cur == '"' && escaped_string {
473-
let mut tmp = String::new();
474-
let mut braces = 0;
475-
while self.cur == '"' {
476-
tmp.push('"');
477-
braces += 1;
478-
self.next(i);
463+
match self.cur {
464+
'"' if !escaped_string => break,
465+
'"' if escaped_string => {
466+
let mut tmp = String::new();
467+
let mut braces = 0;
468+
while self.cur == '"' {
469+
tmp.push('"');
470+
braces += 1;
471+
self.next(i);
472+
}
473+
if braces == 3 {
474+
break;
475+
}
476+
val.push_str(&tmp);
479477
}
480-
if braces == 3 {
481-
break;
478+
'\\' => {
479+
self.next(i);
482480
}
483-
val.push_str(&tmp);
484-
}
485-
if self.cur == '\\' {
486-
self.next(i);
487-
}
488-
if self.cur == '\0' {
489-
return Err(self.err_parse("string"));
481+
'\0' => return Err(self.err_parse("string")),
482+
_ => {}
490483
}
491484
val.push(self.cur);
492485
self.next(i);
@@ -539,7 +532,7 @@ impl TomlParser {
539532
self.next(i);
540533
}
541534

542-
if self.cur == 'n' {
535+
if self.cur == 'n' { // check if is "nan"
543536
num.push(self.cur);
544537
self.next(i);
545538
if self.cur == 'a' {
@@ -553,7 +546,7 @@ impl TomlParser {
553546
}
554547
}
555548
}
556-
} else if self.cur == 'i' {
549+
} else if self.cur == 'i' { // check if is "inf"
557550
num.push(self.cur);
558551
self.next(i);
559552
if self.cur == 'n' {
@@ -576,49 +569,40 @@ impl TomlParser {
576569
self.next(i);
577570
}
578571

579-
if self.cur == '.' {
580-
num.push(self.cur);
581-
self.next(i);
582-
while matches!(self.cur, '0'..='9' | '_') {
583-
if self.cur != '_' {
584-
num.push(self.cur);
585-
}
586-
self.next(i);
587-
}
588-
if let Ok(num) = num.parse() {
589-
return Ok(TomlTok::F64(num));
590-
} else {
591-
return Err(self.err_parse("number"));
592-
}
593-
} else if self.cur == '-' {
594-
// lets assume its a date. whatever. i don't feel like more parsing today
595-
num.push(self.cur);
596-
self.next(i);
597-
while matches!(self.cur, '0'..='9' | ':' | '-' | 'T') {
572+
match self.cur {
573+
'.' => {
598574
num.push(self.cur);
599575
self.next(i);
600-
}
601-
return Ok(TomlTok::Date(num));
602-
// TODO rework this
603-
}
604-
605-
if matches!(self.cur, ident_chars!()) {
606-
return self.parse_ident(i, num);
607-
}
608-
609-
match negative {
610-
true => {
576+
while matches!(self.cur, '0'..='9' | '_') {
577+
if self.cur != '_' {
578+
num.push(self.cur);
579+
}
580+
self.next(i);
581+
}
611582
if let Ok(num) = num.parse() {
612-
return Ok(TomlTok::I64(num));
583+
return Ok(TomlTok::F64(num));
613584
}
585+
return Err(self.err_parse("number"));
614586
}
615-
false => {
616-
if let Ok(num) = num.parse() {
617-
return Ok(TomlTok::U64(num));
587+
'-' => {
588+
// lets assume its a date. whatever. i don't feel like more parsing today
589+
num.push(self.cur);
590+
self.next(i);
591+
while matches!(self.cur, '0'..='9' | ':' | '-' | 'T') {
592+
num.push(self.cur);
593+
self.next(i);
618594
}
595+
return Ok(TomlTok::Date(num));
596+
// TODO rework this
619597
}
598+
ident_chars!() => return self.parse_ident(i, num),
599+
_ => {}
620600
}
621601

622-
Err(self.err_parse("tokenizer"))
602+
match (negative, num.parse()) {
603+
(true, Ok(n)) => Ok(TomlTok::I64(n)),
604+
(false, Ok(n)) => Ok(TomlTok::U64(n as u64)),
605+
_ => Err(self.err_parse("tokenizer")),
606+
}
623607
}
624608
}

0 commit comments

Comments
 (0)