From d9625b2ad3ab364f9f8c0e305ea6625dfe6d020d Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Wed, 13 Dec 2023 00:00:28 -0500 Subject: [PATCH] `excel`: simplify password-protected Excel file error handling Just propagate calamine errors --- src/cmd/excel.rs | 22 ++++++++-------------- tests/test_excel.rs | 5 +---- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/cmd/excel.rs b/src/cmd/excel.rs index 5267c3389..2d05dfd76 100644 --- a/src/cmd/excel.rs +++ b/src/cmd/excel.rs @@ -105,7 +105,7 @@ use serde::{Deserialize, Serialize}; use crate::{ config::{Config, Delimiter}, - util, CliResult, + util, CliError, CliResult, }; // number of rows to process in each core/thread @@ -150,6 +150,12 @@ struct SheetMetadata { duplicate_headers_count: usize, } +impl From for CliError { + fn from(e: calamine::Error) -> Self { + CliError::Other(format!("{e}")) + } +} + #[derive(Serialize, Deserialize)] struct MetadataStruct { filename: String, @@ -239,19 +245,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> { let requested_range = args.flag_range.to_lowercase(); - let mut workbook = match open_workbook_auto(path) { - Ok(workbook) => workbook, - Err(e) => { - let es = e.to_string(); - // password protected errors come in different flavors for Excel - if es.starts_with("Xls error: Cfb error") - || es.starts_with("Xlsx error: Zip error: invalid Zip archive") - { - return fail_clierror!("{path} may be a password-protected workbook: {e}."); - } - return fail_clierror!("Cannot open workbook: {e}."); - }, - }; + let mut workbook = open_workbook_auto(path)?; let sheet_names = workbook.sheet_names(); if sheet_names.is_empty() { diff --git a/tests/test_excel.rs b/tests/test_excel.rs index f6e72edbc..0c3c24e41 100644 --- a/tests/test_excel.rs +++ b/tests/test_excel.rs @@ -48,10 +48,7 @@ fn excel_open_xlsx_readpassword() { cmd.arg(xlsx_file); let got = wrk.output_stderr(&mut cmd); - assert!(got - .matches("Cannot open workbook: Xlsx error: Workbook is password protected.") - .min() - .is_some()); + assert_eq!(got, "Xlsx error: Workbook is password protected\n"); wrk.assert_err(&mut cmd); }