Skip to content

Commit

Permalink
Add better parsing error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwang401 committed Mar 29, 2024
1 parent 0233fb0 commit e9e31e7
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions src-tauri/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,27 @@ fn extract_headers(rdr: &mut Reader<Cursor<Vec<u8>>>) -> Result<(Vec<Day>, Vec<M
non_empty_row_count += 1;

if non_empty_row_count == 1 {
days = record
.iter()
.skip(3)
.map(|col| col.to_string().parse::<Day>().unwrap())
.collect::<Vec<_>>();
for col in record.iter().skip(3) {
let day: Day = col.to_string().parse().map_err(|_| "Failed to parse day")?;
days.push(day);
}
} else if non_empty_row_count == 2 {
for col in record.iter().skip(3) {
let mouse: Mouse = col.to_string().parse().map_err(|_| "Failed to parse mouse")?;
mice.push(mouse);
}
} else if non_empty_row_count == 3 {
for col in record.iter().skip(3) {
let label: Label = col.to_string().parse().map_err(|_| "Failed to parse label")?;
labels.push(label);
}
}

if non_empty_row_count == 2 {
mice = record
.iter()
.skip(3)
.map(|col| col.to_string().parse::<Mouse>().unwrap())
.collect::<Vec<_>>();
}

if non_empty_row_count == 3 {
labels = record
.iter()
.skip(3)
.map(|col| col.to_string().parse::<Label>().unwrap())
.collect::<Vec<_>>();
}

if non_empty_row_count == 4 {
if non_empty_row_count >= 4 {
break;
}
}
}

Ok((days, mice, labels))
}
}

0 comments on commit e9e31e7

Please sign in to comment.