Skip to content

Commit

Permalink
excel: more robust error-handling
Browse files Browse the repository at this point in the history
- remove unwraps
- simplified last sheet calculation
  • Loading branch information
jqnatividad committed Jun 11, 2022
1 parent 93009b0 commit 413c693
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/cmd/excel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
// i.e -1 is the last sheet; -2 = 2nd to last sheet
sheet_names[cmp::max(
0,
cmp::min(
num_sheets,
num_sheets.abs_diff(sheet_index.unsigned_abs().try_into().unwrap()),
),
cmp::min(num_sheets, num_sheets.abs_diff(sheet_index.abs() as usize)),
)]
.to_string()
}
Expand All @@ -89,7 +86,11 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
first_sheet
}
};
let range = workbook.worksheet_range(&sheet).unwrap().unwrap();
let range = if let Ok(range) = workbook.worksheet_range(&sheet).unwrap() {
range
} else {
return fail!("Cannot get worksheet data from {sheet}");
};

let mut wtr = Config::new(&args.flag_output)
.flexible(args.flag_flexible)
Expand Down Expand Up @@ -121,9 +122,9 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
trimmed_record.push_field(field);
}
});
wtr.write_record(&trimmed_record).unwrap();
wtr.write_record(&trimmed_record)?;
} else {
wtr.write_record(&record).unwrap();
wtr.write_record(&record)?;
}
count += 1;
}
Expand Down

0 comments on commit 413c693

Please sign in to comment.