Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Oct 6, 2024
1 parent 23f4c26 commit a7e41ef
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 126 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 2 additions & 32 deletions src/auto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<RS> Reader<RS> for Sheets<RS>
where
RS: std::io::Read + std::io::Seek,
{
type Error = Error;
type Options = AutoReaderOptions;

/// Creates a new instance.
fn new(_reader: RS) -> Result<Self, Self::Error> {
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<u32>) -> &mut Self {
match self {
Sheets::Xls(ref mut e) => {
Expand Down
18 changes: 0 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -231,21 +225,9 @@ where
/// Error specific to file type
type Error: std::fmt::Debug + From<std::io::Error>;

/// Options specific to file type
type Options: ReaderOptions;

/// Creates a new instance.
fn new(reader: RS) -> Result<Self, Self::Error>;

/// 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<u32>) -> &mut Self;

Expand Down
19 changes: 1 addition & 18 deletions src/ods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -72,15 +70,6 @@ pub struct OdsOptions {
pub header_row: Option<u32>,
}

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);
Expand Down Expand Up @@ -144,7 +133,6 @@ where
RS: Read + Seek,
{
type Error = OdsError;
type Options = OdsOptions;

fn new(reader: RS) -> Result<Self, OdsError> {
let mut zip = ZipArchive::new(reader)?;
Expand Down Expand Up @@ -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<u32>) -> &mut Self {
self.options.header_row = header_row;
self
Expand Down
18 changes: 1 addition & 17 deletions src/xls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -153,16 +152,6 @@ impl XlsOptions {
}
}

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<Data>,
formula: Range<String>,
Expand Down Expand Up @@ -251,16 +240,11 @@ impl<RS: Read + Seek> Xls<RS> {

impl<RS: Read + Seek> Reader<RS> for Xls<RS> {
type Error = XlsError;
type Options = XlsOptions;

fn new(reader: RS) -> Result<Self, XlsError> {
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<u32>) -> &mut Self {
self.options.header_row = header_row;
self
Expand Down
18 changes: 1 addition & 17 deletions src/xlsb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -138,15 +136,6 @@ pub struct XlsbOptions {
pub header_row: Option<u32>,
}

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<RS> {
zip: ZipArchive<RS>,
Expand Down Expand Up @@ -455,7 +444,6 @@ impl<RS: Read + Seek> Xlsb<RS> {

impl<RS: Read + Seek> Reader<RS> for Xlsb<RS> {
type Error = XlsbError;
type Options = XlsbOptions;

fn new(mut reader: RS) -> Result<Self, XlsbError> {
check_for_password_protected(&mut reader)?;
Expand All @@ -482,10 +470,6 @@ impl<RS: Read + Seek> Reader<RS> for Xlsb<RS> {
Ok(xlsb)
}

fn set_options(&mut self, options: Self::Options) {
self.options = options;
}

fn with_header_row(&mut self, header_row: Option<u32>) -> &mut Self {
self.options.header_row = header_row;
self
Expand Down
18 changes: 2 additions & 16 deletions src/xlsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -209,15 +209,6 @@ pub struct XlsxOptions {
pub header_row: Option<u32>,
}

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<RS: Read + Seek> Xlsx<RS> {
fn read_shared_strings(&mut self) -> Result<(), XlsxError> {
let mut xml = match xml_reader(&mut self.zip, "xl/sharedStrings.xml") {
Expand Down Expand Up @@ -868,7 +859,6 @@ impl<RS: Read + Seek> Xlsx<RS> {

impl<RS: Read + Seek> Reader<RS> for Xlsx<RS> {
type Error = XlsxError;
type Options = XlsxOptions;

fn new(mut reader: RS) -> Result<Self, XlsxError> {
check_for_password_protected(&mut reader)?;
Expand Down Expand Up @@ -896,10 +886,6 @@ impl<RS: Read + Seek> Reader<RS> for Xlsx<RS> {
Ok(xlsx)
}

fn set_options(&mut self, options: Self::Options) {
self.options = options;
}

fn with_header_row(&mut self, header_row: Option<u32>) -> &mut Self {
self.options.header_row = header_row;
self
Expand Down

0 comments on commit a7e41ef

Please sign in to comment.