diff --git a/benches/basic.rs b/benches/basic.rs index 15117186..c8e08bb9 100644 --- a/benches/basic.rs +++ b/benches/basic.rs @@ -11,7 +11,7 @@ fn count>>(path: &str) -> usize { let path = format!("{}/{}", env!("CARGO_MANIFEST_DIR"), path); let mut excel: R = open_workbook(&path).expect("cannot open excel file"); - let sheets = excel.sheet_names().to_owned(); + let sheets = excel.sheet_names(); let mut count = 0; for s in sheets { count += excel @@ -50,11 +50,11 @@ fn bench_xlsx_cells_reader(b: &mut Bencher) { let path = format!("{}/{}", env!("CARGO_MANIFEST_DIR"), path); let mut excel: Xlsx<_> = open_workbook(&path).expect("cannot open excel file"); - let sheets = excel.sheet_names().to_owned(); + let sheets = excel.sheet_names(); let mut count = 0; for s in sheets { let mut cells_reader = excel.worksheet_cells_reader(&s).unwrap(); - while let Some(_) = cells_reader.next_cell().unwrap() { + while cells_reader.next_cell().unwrap().is_some() { count += 1; } } @@ -69,11 +69,11 @@ fn bench_xlsb_cells_reader(b: &mut Bencher) { let path = format!("{}/{}", env!("CARGO_MANIFEST_DIR"), path); let mut excel: Xlsb<_> = open_workbook(&path).expect("cannot open excel file"); - let sheets = excel.sheet_names().to_owned(); + let sheets = excel.sheet_names(); let mut count = 0; for s in sheets { let mut cells_reader = excel.worksheet_cells_reader(&s).unwrap(); - while let Some(_) = cells_reader.next_cell().unwrap() { + while cells_reader.next_cell().unwrap().is_some() { count += 1; } } diff --git a/examples/excel_to_csv.rs b/examples/excel_to_csv.rs index 8e6d6e85..fbc2f115 100644 --- a/examples/excel_to_csv.rs +++ b/examples/excel_to_csv.rs @@ -36,13 +36,13 @@ fn write_range(dest: &mut W, range: &Range) -> std::io::Result<( match *c { Data::Empty => Ok(()), Data::String(ref s) | Data::DateTimeIso(ref s) | Data::DurationIso(ref s) => { - write!(dest, "{}", s) + write!(dest, "{s}") } - Data::Float(ref f) => write!(dest, "{}", f), + Data::Float(ref f) => write!(dest, "{f}"), Data::DateTime(ref d) => write!(dest, "{}", d.as_f64()), - Data::Int(ref i) => write!(dest, "{}", i), - Data::Error(ref e) => write!(dest, "{:?}", e), - Data::Bool(ref b) => write!(dest, "{}", b), + Data::Int(ref i) => write!(dest, "{i}"), + Data::Error(ref e) => write!(dest, "{e:?}"), + Data::Bool(ref b) => write!(dest, "{b}"), }?; if i != n { write!(dest, ";")?; diff --git a/examples/search_errors.rs b/examples/search_errors.rs index 7d10d142..52a184b2 100644 --- a/examples/search_errors.rs +++ b/examples/search_errors.rs @@ -18,7 +18,7 @@ fn main() { // Search recursively for all excel files matching argument pattern // Output statistics: nb broken references, nb broken cells etc... let folder = env::args().nth(1).unwrap_or_else(|| ".".to_string()); - let pattern = format!("{}/**/*.xl*", folder); + let pattern = format!("{folder}/**/*.xl*"); let mut filecount = 0; let mut output = pattern @@ -40,14 +40,14 @@ fn main() { filecount += 1; match run(f) { Ok((f, missing, cell_errors)) => { - writeln!(output, "{:?}~{:?}~{}", f, missing, cell_errors) + writeln!(output, "{f:?}~{missing:?}~{cell_errors}") } - Err(e) => writeln!(output, "{:?}", e), + Err(e) => writeln!(output, "{e:?}"), } - .unwrap_or_else(|e| println!("{:?}", e)) + .unwrap_or_else(|e| println!("{e:?}")) } - println!("Found {} excel files", filecount); + println!("Found {filecount} excel files"); } fn run(f: GlobResult) -> Result<(PathBuf, Option, usize), FileStatus> { diff --git a/src/lib.rs b/src/lib.rs index 4f5ad4d4..a6c970f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -333,7 +333,10 @@ where /// /// This is implemented only for [`calamine::Xlsx`](crate::Xlsx) and [`calamine::Xlsb`](crate::Xlsb), as Xls and Ods formats /// do not support lazy iteration. - fn worksheet_range_at_ref(&mut self, n: usize) -> Option, Self::Error>> { + fn worksheet_range_at_ref( + &mut self, + n: usize, + ) -> Option>, Self::Error>> { let name = self.sheet_names().get(n)?.to_string(); Some(self.worksheet_range_ref(&name)) } @@ -720,41 +723,36 @@ impl Range { /// ``` /// pub fn from_sparse(cells: Vec>) -> Range { - if cells.is_empty() { - Range::empty() - } else { - // search bounds - let row_start = cells.first().unwrap().pos.0; - let row_end = cells.last().unwrap().pos.0; - let mut col_start = u32::MAX; - let mut col_end = 0; - for c in cells.iter().map(|c| c.pos.1) { - if c < col_start { - col_start = c; - } - if c > col_end { - col_end = c; - } - } - let cols = (col_end - col_start + 1) as usize; - let rows = (row_end - row_start + 1) as usize; - let len = cols.saturating_mul(rows); - let mut v = vec![T::default(); len]; - v.shrink_to_fit(); - for c in cells { - let row = (c.pos.0 - row_start) as usize; - let col = (c.pos.1 - col_start) as usize; - let idx = row.saturating_mul(cols) + col; - if let Some(v) = v.get_mut(idx) { - *v = c.val; - } - } - Range { - start: (row_start, col_start), - end: (row_end, col_end), - inner: v, + let (row_start, row_end) = match &cells[..] { + [] => return Range::empty(), + [first] => (first.pos.0, first.pos.0), + [first, .., last] => (first.pos.0, last.pos.0), + }; + // search bounds + let mut col_start = u32::MAX; + let mut col_end = 0; + for c in cells.iter().map(|c| c.pos.1) { + col_start = min(c, col_start); + col_end = max(c, col_end); + } + let cols = (col_end - col_start + 1) as usize; + let rows = (row_end - row_start + 1) as usize; + let len = cols.saturating_mul(rows); + let mut v = vec![T::default(); len]; + v.shrink_to_fit(); + for c in cells { + let row = (c.pos.0 - row_start) as usize; + let col = (c.pos.1 - col_start) as usize; + let idx = row.saturating_mul(cols) + col; + if let Some(v) = v.get_mut(idx) { + *v = c.val; } } + Range { + start: (row_start, col_start), + end: (row_end, col_end), + inner: v, + } } /// Set a value at an absolute position in a `Range`. diff --git a/src/ods.rs b/src/ods.rs index 4778fc27..d1e5939e 100644 --- a/src/ods.rs +++ b/src/ods.rs @@ -775,7 +775,7 @@ fn read_pictures( let zname = zfile.name(); // no Thumbnails if zname.starts_with("Pictures") { - if let Some(ext) = zname.split('.').last() { + if let Some(ext) = zname.split('.').next_back() { if [ "emf", "wmf", "pict", "jpeg", "jpg", "png", "dib", "gif", "tiff", "eps", "bmp", "wpg", diff --git a/src/xls.rs b/src/xls.rs index 7a538323..a58a9d6d 100644 --- a/src/xls.rs +++ b/src/xls.rs @@ -450,8 +450,8 @@ impl Xls { } //0x0201 => cells.push(parse_blank(r.data)?), // 513: Blank 0x0203 => cells.push(parse_number(r.data, &self.formats, self.is_1904)?), // 515: Number - 0x0204 => cells.extend(parse_label(r.data, &encoding, biff)?), // 516: Label [MS-XLS 2.4.148] - 0x0205 => cells.push(parse_bool_err(r.data)?), // 517: BoolErr + 0x0204 => cells.push(parse_label(r.data, &encoding, biff)?), // 516: Label [MS-XLS 2.4.148] + 0x0205 => cells.push(parse_bool_err(r.data)?), // 517: BoolErr 0x0207 => { // 519 String (formula value) let val = Data::String(parse_string(r.data, &encoding, biff)?); @@ -778,7 +778,7 @@ fn parse_short_string( } let mut s = String::with_capacity(cch); - let _ = encoding.decode_to(r.data, cch, &mut s, high_byte); + encoding.decode_to(r.data, cch, &mut s, high_byte); Ok(s) } @@ -799,15 +799,11 @@ fn parse_string(r: &[u8], encoding: &XlsEncoding, biff: Biff) -> Result Result>, XlsError> { +fn parse_label(r: &[u8], encoding: &XlsEncoding, biff: Biff) -> Result, XlsError> { if r.len() < 6 { return Err(XlsError::Len { typ: "label", @@ -818,10 +814,10 @@ fn parse_label( let row = read_u16(r); let col = read_u16(&r[2..]); let _ixfe = read_u16(&r[4..]); - Ok(Some(Cell::new( + Ok(Cell::new( (row as u32, col as u32), Data::String(parse_string(&r[6..], encoding, biff)?), - ))) + )) } fn parse_label_sst(r: &[u8], strings: &[String]) -> Result>, XlsError> { diff --git a/src/xlsb/mod.rs b/src/xlsb/mod.rs index eb5592df..839f6c01 100644 --- a/src/xlsb/mod.rs +++ b/src/xlsb/mod.rs @@ -431,7 +431,7 @@ impl Xlsb { let mut zfile = self.zip.by_index(i)?; let zname = zfile.name(); if zname.starts_with("xl/media") { - if let Some(ext) = zname.split('.').last() { + if let Some(ext) = zname.split('.').next_back() { if [ "emf", "wmf", "pict", "jpeg", "jpg", "png", "dib", "gif", "tiff", "eps", "bmp", "wpg", diff --git a/src/xlsx/mod.rs b/src/xlsx/mod.rs index 97395f06..0b933a7f 100644 --- a/src/xlsx/mod.rs +++ b/src/xlsx/mod.rs @@ -645,7 +645,7 @@ impl Xlsx { let mut zfile = self.zip.by_index(i)?; let zname = zfile.name(); if zname.starts_with("xl/media") { - if let Some(ext) = zname.split('.').last() { + if let Some(ext) = zname.split('.').next_back() { if [ "emf", "wmf", "pict", "jpeg", "jpg", "png", "dib", "gif", "tiff", "eps", "bmp", "wpg", @@ -804,7 +804,7 @@ impl Xlsx { } /// Get the table by name (ref) - pub fn table_by_name_ref(&mut self, table_name: &str) -> Result, XlsxError> { + pub fn table_by_name_ref(&mut self, table_name: &str) -> Result>, XlsxError> { let TableMetadata { name, sheet_name, diff --git a/tests/test.rs b/tests/test.rs index bad9c3cd..5861d778 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -520,13 +520,12 @@ fn issue_127() { .collect(); for ext in &["ods", "xls", "xlsx", "xlsb"] { - let p = format!("{}/tests/issue127.{}", root, ext); + let p = format!("{root}/tests/issue127.{ext}"); let workbook = open_workbook_auto(&p).expect(&p); assert_eq!( workbook.sheet_names(), &ordered_names[..], - "{} sheets should be ordered", - ext + "{ext} sheets should be ordered" ); } } @@ -714,7 +713,7 @@ fn date_xls() { #[cfg(feature = "dates")] { - let date = chrono::NaiveDate::from_ymd_opt(2021, 01, 01).unwrap(); + let date = chrono::NaiveDate::from_ymd_opt(2021, 1, 1).unwrap(); assert_eq!(range.get_value((0, 0)).unwrap().as_date(), Some(date)); let duration = chrono::Duration::seconds(255 * 60 * 60 + 10 * 60 + 10); @@ -749,7 +748,7 @@ fn date_xls_1904() { #[cfg(feature = "dates")] { - let date = chrono::NaiveDate::from_ymd_opt(2021, 01, 01).unwrap(); + let date = chrono::NaiveDate::from_ymd_opt(2021, 1, 1).unwrap(); assert_eq!(range.get_value((0, 0)).unwrap().as_date(), Some(date)); let duration = chrono::Duration::seconds(255 * 60 * 60 + 10 * 60 + 10); @@ -784,7 +783,7 @@ fn date_xlsx() { #[cfg(feature = "dates")] { - let date = chrono::NaiveDate::from_ymd_opt(2021, 01, 01).unwrap(); + let date = chrono::NaiveDate::from_ymd_opt(2021, 1, 1).unwrap(); assert_eq!(range.get_value((0, 0)).unwrap().as_date(), Some(date)); let duration = chrono::Duration::seconds(255 * 60 * 60 + 10 * 60 + 10); @@ -819,7 +818,7 @@ fn date_xlsx_1904() { #[cfg(feature = "dates")] { - let date = chrono::NaiveDate::from_ymd_opt(2021, 01, 01).unwrap(); + let date = chrono::NaiveDate::from_ymd_opt(2021, 1, 1).unwrap(); assert_eq!(range.get_value((0, 0)).unwrap().as_date(), Some(date)); let duration = chrono::Duration::seconds(255 * 60 * 60 + 10 * 60 + 10); @@ -850,7 +849,7 @@ fn date_xlsx_iso() { #[cfg(feature = "dates")] { - let date = chrono::NaiveDate::from_ymd_opt(2021, 01, 01).unwrap(); + let date = chrono::NaiveDate::from_ymd_opt(2021, 1, 1).unwrap(); assert_eq!(range.get_value((0, 0)).unwrap().as_date(), Some(date)); assert_eq!(range.get_value((0, 0)).unwrap().as_time(), None); assert_eq!(range.get_value((0, 0)).unwrap().as_datetime(), None); @@ -894,7 +893,7 @@ fn date_ods() { #[cfg(feature = "dates")] { - let date = chrono::NaiveDate::from_ymd_opt(2021, 01, 01).unwrap(); + let date = chrono::NaiveDate::from_ymd_opt(2021, 1, 1).unwrap(); assert_eq!(range.get_value((0, 0)).unwrap().as_date(), Some(date)); let time = chrono::NaiveTime::from_hms_opt(10, 10, 10).unwrap(); @@ -942,7 +941,7 @@ fn date_xlsb() { #[cfg(feature = "dates")] { - let date = chrono::NaiveDate::from_ymd_opt(2021, 01, 01).unwrap(); + let date = chrono::NaiveDate::from_ymd_opt(2021, 1, 1).unwrap(); assert_eq!(range.get_value((0, 0)).unwrap().as_date(), Some(date)); let duration = chrono::Duration::seconds(255 * 60 * 60 + 10 * 60 + 10); @@ -977,7 +976,7 @@ fn date_xlsb_1904() { #[cfg(feature = "dates")] { - let date = chrono::NaiveDate::from_ymd_opt(2021, 01, 01).unwrap(); + let date = chrono::NaiveDate::from_ymd_opt(2021, 1, 1).unwrap(); assert_eq!(range.get_value((0, 0)).unwrap().as_date(), Some(date)); let duration = chrono::Duration::seconds(255 * 60 * 60 + 10 * 60 + 10);