Skip to content

Commit e9e31e7

Browse files
committed
Add better parsing error handling
1 parent 0233fb0 commit e9e31e7

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

src-tauri/src/parser.rs

+16-23
Original file line numberDiff line numberDiff line change
@@ -75,34 +75,27 @@ fn extract_headers(rdr: &mut Reader<Cursor<Vec<u8>>>) -> Result<(Vec<Day>, Vec<M
7575
non_empty_row_count += 1;
7676

7777
if non_empty_row_count == 1 {
78-
days = record
79-
.iter()
80-
.skip(3)
81-
.map(|col| col.to_string().parse::<Day>().unwrap())
82-
.collect::<Vec<_>>();
78+
for col in record.iter().skip(3) {
79+
let day: Day = col.to_string().parse().map_err(|_| "Failed to parse day")?;
80+
days.push(day);
81+
}
82+
} else if non_empty_row_count == 2 {
83+
for col in record.iter().skip(3) {
84+
let mouse: Mouse = col.to_string().parse().map_err(|_| "Failed to parse mouse")?;
85+
mice.push(mouse);
86+
}
87+
} else if non_empty_row_count == 3 {
88+
for col in record.iter().skip(3) {
89+
let label: Label = col.to_string().parse().map_err(|_| "Failed to parse label")?;
90+
labels.push(label);
91+
}
8392
}
8493

85-
if non_empty_row_count == 2 {
86-
mice = record
87-
.iter()
88-
.skip(3)
89-
.map(|col| col.to_string().parse::<Mouse>().unwrap())
90-
.collect::<Vec<_>>();
91-
}
92-
93-
if non_empty_row_count == 3 {
94-
labels = record
95-
.iter()
96-
.skip(3)
97-
.map(|col| col.to_string().parse::<Label>().unwrap())
98-
.collect::<Vec<_>>();
99-
}
100-
101-
if non_empty_row_count == 4 {
94+
if non_empty_row_count >= 4 {
10295
break;
10396
}
10497
}
10598
}
10699

107100
Ok((days, mice, labels))
108-
}
101+
}

0 commit comments

Comments
 (0)