From d56c7f9a67088123323217a75c70adf6a67f27f6 Mon Sep 17 00:00:00 2001 From: Eric Jolibois Date: Sun, 6 Oct 2024 22:01:43 +0200 Subject: [PATCH] clean --- README.md | 17 +++++++++-------- src/auto.rs | 34 ++-------------------------------- src/lib.rs | 24 +++--------------------- src/ods.rs | 19 +------------------ src/xls.rs | 29 +---------------------------- src/xlsb/mod.rs | 18 +----------------- src/xlsx/mod.rs | 18 ++---------------- 7 files changed, 19 insertions(+), 140 deletions(-) diff --git a/README.md b/README.md index 29a9aed..530eed5 100644 --- a/README.md +++ b/README.md @@ -103,24 +103,25 @@ if let Some(Ok(r)) = excel.worksheet_range("Sheet1") { } ``` -### Reader: With options +### Reader: With header row ```rs -use calamine::{Reader, Xlsx, XlsxOptions, open_workbook}; +use calamine::{Reader, Xlsx, open_workbook}; let mut excel: Xlsx<_> = open_workbook("file.xlsx").unwrap(); let sheet1 = excel - .with_options(XlsxOptions::default().with_header_row(3)) + .with_header_row(Some(3)) .worksheet_range("Sheet1") .unwrap(); ``` -Keep in mind that `xlsx` and `xlsb` files support lazy loading, -meaning the specified options are applied immediately when reading a sheet range. -However, for `xls` and `ods` files, all sheets are loaded at once when -opening the workbook with default settings, so the options are only applied -afterward, offering no performance advantages. +Note that `xlsx` and `xlsb` files support lazy loading, so specifying a +header row takes effect immediately when reading a sheet range. +In contrast, for `xls` and `ods` files, all sheets are loaded at once when +opening the workbook with default settings. +As a result, setting the header row only applies afterward and does not +provide any performance benefits. ### Reader: More complex diff --git a/src/auto.rs b/src/auto.rs index 576a596..b1d28fd 100644 --- a/src/auto.rs +++ b/src/auto.rs @@ -2,10 +2,9 @@ use crate::errors::Error; use crate::vba::VbaProject; -use crate::xlsb::XlsbOptions; use crate::{ - open_workbook, open_workbook_from_rs, Data, DataRef, Metadata, Ods, OdsOptions, Range, Reader, - ReaderOptions, ReaderRef, Xls, XlsOptions, Xlsb, Xlsx, XlsxOptions, + open_workbook, open_workbook_from_rs, Data, DataRef, Metadata, Ods, Range, Reader, ReaderRef, + Xls, Xlsb, Xlsx, }; use std::borrow::Cow; use std::fs::File; @@ -75,46 +74,17 @@ where } } -pub enum AutoReaderOptions { - Xls(XlsOptions), - Xlsx(XlsxOptions), - Xlsb(XlsbOptions), - Ods(OdsOptions), -} - -impl ReaderOptions for AutoReaderOptions { - fn with_header_row(self, header_row: u32) -> Self { - match self { - AutoReaderOptions::Xls(e) => AutoReaderOptions::Xls(e.with_header_row(header_row)), - AutoReaderOptions::Xlsx(e) => AutoReaderOptions::Xlsx(e.with_header_row(header_row)), - AutoReaderOptions::Xlsb(e) => AutoReaderOptions::Xlsb(e.with_header_row(header_row)), - AutoReaderOptions::Ods(e) => AutoReaderOptions::Ods(e.with_header_row(header_row)), - } - } -} - impl Reader for Sheets where RS: std::io::Read + std::io::Seek, { type Error = Error; - type Options = AutoReaderOptions; /// Creates a new instance. fn new(_reader: RS) -> Result { Err(Error::Msg("Sheets must be created from a Path")) } - fn set_options(&mut self, options: Self::Options) { - match (self, options) { - (Sheets::Xls(ref mut e), AutoReaderOptions::Xls(opts)) => e.set_options(opts), - (Sheets::Xlsx(ref mut e), AutoReaderOptions::Xlsx(opts)) => e.set_options(opts), - (Sheets::Xlsb(ref mut e), AutoReaderOptions::Xlsb(opts)) => e.set_options(opts), - (Sheets::Ods(ref mut e), AutoReaderOptions::Ods(opts)) => e.set_options(opts), - _ => unreachable!(), - } - } - fn with_header_row(&mut self, header_row: Option) -> &mut Self { match self { Sheets::Xls(ref mut e) => { diff --git a/src/lib.rs b/src/lib.rs index 039fe00..be971ff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,10 +86,10 @@ pub use crate::auto::{open_workbook_auto, open_workbook_auto_from_rs, Sheets}; pub use crate::datatype::{Data, DataRef, DataType, ExcelDateTime, ExcelDateTimeType}; pub use crate::de::{DeError, RangeDeserializer, RangeDeserializerBuilder, ToCellDeserializer}; pub use crate::errors::Error; -pub use crate::ods::{Ods, OdsError, OdsOptions}; +pub use crate::ods::{Ods, OdsError}; pub use crate::xls::{Xls, XlsError, XlsOptions}; -pub use crate::xlsb::{Xlsb, XlsbError, XlsbOptions}; -pub use crate::xlsx::{Xlsx, XlsxError, XlsxOptions}; +pub use crate::xlsb::{Xlsb, XlsbError}; +pub use crate::xlsx::{Xlsx, XlsxError}; use crate::vba::VbaProject; @@ -215,12 +215,6 @@ pub struct Sheet { pub visible: SheetVisible, } -/// A trait to share reader options across different `FileType`s -pub trait ReaderOptions: Sized { - /// Set the header row - fn with_header_row(self, _header_row: u32) -> Self; -} - // FIXME `Reader` must only be seek `Seek` for `Xls::xls`. Because of the present API this limits // the kinds of readers (other) data in formats can be read from. /// A trait to share spreadsheets reader functions across different `FileType`s @@ -231,21 +225,9 @@ where /// Error specific to file type type Error: std::fmt::Debug + From; - /// Options specific to file type - type Options: ReaderOptions; - /// Creates a new instance. fn new(reader: RS) -> Result; - /// Set options - fn set_options(&mut self, options: Self::Options); - - /// Set options and return the reader - fn with_options(&mut self, options: Self::Options) -> &mut Self { - self.set_options(options); - self - } - /// Set current header row fn with_header_row(&mut self, header_row: Option) -> &mut Self; diff --git a/src/ods.rs b/src/ods.rs index 10ef581..003bcef 100644 --- a/src/ods.rs +++ b/src/ods.rs @@ -16,9 +16,7 @@ use zip::read::{ZipArchive, ZipFile}; use zip::result::ZipError; use crate::vba::VbaProject; -use crate::{ - Data, DataType, Metadata, Range, Reader, ReaderOptions, Sheet, SheetType, SheetVisible, -}; +use crate::{Data, DataType, Metadata, Range, Reader, Sheet, SheetType, SheetVisible}; use std::marker::PhantomData; const MIMETYPE: &[u8] = b"application/vnd.oasis.opendocument.spreadsheet"; @@ -72,15 +70,6 @@ pub struct OdsOptions { pub header_row: Option, } -impl ReaderOptions for OdsOptions { - /// Set the header row index - fn with_header_row(self, header_row: u32) -> Self { - Self { - header_row: Some(header_row), - } - } -} - from_err!(std::io::Error, OdsError, Io); from_err!(zip::result::ZipError, OdsError, Zip); from_err!(quick_xml::Error, OdsError, Xml); @@ -144,7 +133,6 @@ where RS: Read + Seek, { type Error = OdsError; - type Options = OdsOptions; fn new(reader: RS) -> Result { let mut zip = ZipArchive::new(reader)?; @@ -187,11 +175,6 @@ where }) } - /// Set options - fn set_options(&mut self, options: Self::Options) { - self.options = options; - } - fn with_header_row(&mut self, header_row: Option) -> &mut Self { self.options.header_row = header_row; self diff --git a/src/xls.rs b/src/xls.rs index 49f9a6c..761b6cc 100644 --- a/src/xls.rs +++ b/src/xls.rs @@ -17,8 +17,7 @@ use crate::utils::read_usize; use crate::utils::{push_column, read_f64, read_i16, read_i32, read_u16, read_u32}; use crate::vba::VbaProject; use crate::{ - Cell, CellErrorType, Data, Dimensions, Metadata, Range, Reader, ReaderOptions, Sheet, - SheetType, SheetVisible, + Cell, CellErrorType, Data, Dimensions, Metadata, Range, Reader, Sheet, SheetType, SheetVisible, }; #[derive(Debug)] @@ -142,27 +141,6 @@ pub struct XlsOptions { pub header_row: Option, } -impl XlsOptions { - #[allow(dead_code)] - /// Set the code page - fn with_codepage(self, codepage: u16) -> Self { - Self { - force_codepage: Some(codepage), - ..self - } - } -} - -impl ReaderOptions for XlsOptions { - /// Set the header row index - fn with_header_row(self, header_row: u32) -> Self { - Self { - header_row: Some(header_row), - ..self - } - } -} - struct SheetData { range: Range, formula: Range, @@ -251,16 +229,11 @@ impl Xls { impl Reader for Xls { type Error = XlsError; - type Options = XlsOptions; fn new(reader: RS) -> Result { Self::new_with_options(reader, XlsOptions::default()) } - fn set_options(&mut self, options: Self::Options) { - self.options = options; - } - fn with_header_row(&mut self, header_row: Option) -> &mut Self { self.options.header_row = header_row; self diff --git a/src/xlsb/mod.rs b/src/xlsb/mod.rs index d79e4b0..9bf3347 100644 --- a/src/xlsb/mod.rs +++ b/src/xlsb/mod.rs @@ -20,9 +20,7 @@ use crate::datatype::DataRef; use crate::formats::{builtin_format_by_code, detect_custom_number_format, CellFormat}; use crate::utils::{push_column, read_f64, read_i32, read_u16, read_u32, read_usize}; use crate::vba::VbaProject; -use crate::{ - Cell, Data, Metadata, Range, Reader, ReaderOptions, ReaderRef, Sheet, SheetType, SheetVisible, -}; +use crate::{Cell, Data, Metadata, Range, Reader, ReaderRef, Sheet, SheetType, SheetVisible}; /// A Xlsb specific error #[derive(Debug)] @@ -138,15 +136,6 @@ pub struct XlsbOptions { pub header_row: Option, } -impl ReaderOptions for XlsbOptions { - /// Set the header row index - fn with_header_row(self, header_row: u32) -> Self { - Self { - header_row: Some(header_row), - } - } -} - /// A Xlsb reader pub struct Xlsb { zip: ZipArchive, @@ -455,7 +444,6 @@ impl Xlsb { impl Reader for Xlsb { type Error = XlsbError; - type Options = XlsbOptions; fn new(mut reader: RS) -> Result { check_for_password_protected(&mut reader)?; @@ -482,10 +470,6 @@ impl Reader for Xlsb { Ok(xlsb) } - fn set_options(&mut self, options: Self::Options) { - self.options = options; - } - fn with_header_row(&mut self, header_row: Option) -> &mut Self { self.options.header_row = header_row; self diff --git a/src/xlsx/mod.rs b/src/xlsx/mod.rs index 6b5c6e9..1e5436c 100644 --- a/src/xlsx/mod.rs +++ b/src/xlsx/mod.rs @@ -18,8 +18,8 @@ use crate::datatype::DataRef; use crate::formats::{builtin_format_by_id, detect_custom_number_format, CellFormat}; use crate::vba::VbaProject; use crate::{ - Cell, CellErrorType, Data, Dimensions, Metadata, Range, Reader, ReaderOptions, ReaderRef, - Sheet, SheetType, SheetVisible, Table, + Cell, CellErrorType, Data, Dimensions, Metadata, Range, Reader, ReaderRef, Sheet, SheetType, + SheetVisible, Table, }; pub use cells_reader::XlsxCellReader; @@ -209,15 +209,6 @@ pub struct XlsxOptions { pub header_row: Option, } -impl ReaderOptions for XlsxOptions { - /// Set the header row index - fn with_header_row(self, header_row: u32) -> Self { - Self { - header_row: Some(header_row), - } - } -} - impl Xlsx { fn read_shared_strings(&mut self) -> Result<(), XlsxError> { let mut xml = match xml_reader(&mut self.zip, "xl/sharedStrings.xml") { @@ -868,7 +859,6 @@ impl Xlsx { impl Reader for Xlsx { type Error = XlsxError; - type Options = XlsxOptions; fn new(mut reader: RS) -> Result { check_for_password_protected(&mut reader)?; @@ -896,10 +886,6 @@ impl Reader for Xlsx { Ok(xlsx) } - fn set_options(&mut self, options: Self::Options) { - self.options = options; - } - fn with_header_row(&mut self, header_row: Option) -> &mut Self { self.options.header_row = header_row; self