Skip to content

Commit

Permalink
Merge pull request #448 from PrettyWood/fix/clippy
Browse files Browse the repository at this point in the history
chore: make clippy happy
  • Loading branch information
tafia authored Jul 2, 2024
2 parents 6b41309 + 1bd8b42 commit ed305da
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 197 deletions.
11 changes: 2 additions & 9 deletions examples/search_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use calamine::{open_workbook_auto, Data, Error, Reader};
use glob::{glob, GlobError, GlobResult};

#[derive(Debug)]
#[allow(dead_code)]
enum FileStatus {
VbaError(Error),
RangeError(Error),
Expand Down Expand Up @@ -77,15 +78,7 @@ fn run(f: GlobResult) -> Result<(PathBuf, Option<usize>, usize), FileStatus> {
let range = xl.worksheet_range(&s).map_err(FileStatus::RangeError)?;
cell_errors += range
.rows()
.flat_map(|r| {
r.iter().filter(|c| {
if let Data::Error(_) = **c {
true
} else {
false
}
})
})
.flat_map(|r| r.iter().filter(|c| matches!(**c, Data::Error(_))))
.count();
}

Expand Down
12 changes: 6 additions & 6 deletions src/auto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ where
RS: std::io::Read + std::io::Seek + Clone,
{
if let Ok(ret) = open_workbook_from_rs::<Xls<RS>, RS>(data.clone()) {
return Ok(Sheets::Xls(ret));
Ok(Sheets::Xls(ret))
} else if let Ok(ret) = open_workbook_from_rs::<Xlsx<RS>, RS>(data.clone()) {
return Ok(Sheets::Xlsx(ret));
Ok(Sheets::Xlsx(ret))
} else if let Ok(ret) = open_workbook_from_rs::<Xlsb<RS>, RS>(data.clone()) {
return Ok(Sheets::Xlsb(ret));
Ok(Sheets::Xlsb(ret))
} else if let Ok(ret) = open_workbook_from_rs::<Ods<RS>, RS>(data) {
return Ok(Sheets::Ods(ret));
Ok(Sheets::Ods(ret))
} else {
return Err(Error::Msg("Cannot detect file format"));
};
Err(Error::Msg("Cannot detect file format"))
}
}

impl<RS> Reader<RS> for Sheets<RS>
Expand Down
5 changes: 1 addition & 4 deletions src/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ impl DataType for Data {

impl PartialEq<&str> for Data {
fn eq(&self, other: &&str) -> bool {
match *self {
Data::String(ref s) if s == other => true,
_ => false,
}
matches!(*self, Data::String(ref s) if s == other)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn format_excel_i64(value: i64, format: Option<&CellFormat>, is_1904: bool)

// convert f64 to date, if format == Date
#[inline]
pub fn format_excel_f64_ref<'a>(
pub fn format_excel_f64_ref(
value: f64,
format: Option<&CellFormat>,
is_1904: bool,
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub struct Dimensions {
pub end: (u32, u32),
}

#[allow(clippy::len_without_is_empty)]
impl Dimensions {
/// create dimensions info with start position and end position
pub fn new(start: (u32, u32), end: (u32, u32)) -> Self {
Expand Down Expand Up @@ -463,7 +464,9 @@ impl<T: CellType> Range<T> {
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;
v.get_mut(idx).map(|v| *v = c.val);
if let Some(v) = v.get_mut(idx) {
*v = c.val;
}
}
Range {
start: (row_start, col_start),
Expand Down
2 changes: 1 addition & 1 deletion src/xls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ fn parse_merge_cells(r: &[u8], merge_cells: &mut Vec<Dimensions>) -> Result<(),
for i in 0..count {
let offset: usize = (2 + i * 8).into();

let rf = read_u16(&r[offset + 0..]);
let rf = read_u16(&r[offset..]);
let rl = read_u16(&r[offset + 2..]);
let cf = read_u16(&r[offset + 4..]);
let cl = read_u16(&r[offset + 6..]);
Expand Down
12 changes: 6 additions & 6 deletions src/xlsb/cells_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl<'a> XlsbCellsReader<'a> {
let v = (v as f64) / 100.0;
format_excel_f64_ref(
v,
cell_format(&self.formats, &self.buf),
cell_format(self.formats, &self.buf),
self.is_1904,
)
} else {
Expand All @@ -103,7 +103,7 @@ impl<'a> XlsbCellsReader<'a> {
v[4..].copy_from_slice(&self.buf[8..12]);
let v = read_f64(&v);
let v = if d100 { v / 100.0 } else { v };
format_excel_f64_ref(v, cell_format(&self.formats, &self.buf), self.is_1904)
format_excel_f64_ref(v, cell_format(self.formats, &self.buf), self.is_1904)
}
}
0x0003 => {
Expand All @@ -124,7 +124,7 @@ impl<'a> XlsbCellsReader<'a> {
0x0004 | 0x000A => DataRef::Bool(self.buf[8] != 0), // BrtCellBool or BrtFmlaBool
0x0005 | 0x0009 => {
let v = read_f64(&self.buf[8..16]);
format_excel_f64_ref(v, cell_format(&self.formats, &self.buf), self.is_1904)
format_excel_f64_ref(v, cell_format(self.formats, &self.buf), self.is_1904)
} // BrtCellReal or BrtFmlaNum
0x0006 | 0x0008 => DataRef::String(wide_str(&self.buf[8..], &mut 0)?.into_owned()), // BrtCellSt or BrtFmlaString
0x0007 => {
Expand Down Expand Up @@ -162,21 +162,21 @@ impl<'a> XlsbCellsReader<'a> {
let formula = &self.buf[14 + cch * 2..];
let cce = read_u32(formula) as usize;
let rgce = &formula[4..4 + cce];
parse_formula(rgce, &self.extern_sheets, &self.metadata_names)?
parse_formula(rgce, self.extern_sheets, self.metadata_names)?
}
0x0009 => {
// BrtFmlaNum
let formula = &self.buf[18..];
let cce = read_u32(formula) as usize;
let rgce = &formula[4..4 + cce];
parse_formula(rgce, &self.extern_sheets, &self.metadata_names)?
parse_formula(rgce, self.extern_sheets, self.metadata_names)?
}
0x000A | 0x000B => {
// BrtFmlaBool | BrtFmlaError
let formula = &self.buf[11..];
let cce = read_u32(formula) as usize;
let rgce = &formula[4..4 + cce];
parse_formula(rgce, &self.extern_sheets, &self.metadata_names)?
parse_formula(rgce, self.extern_sheets, self.metadata_names)?
}
0x0000 => {
// BrtRowHdr
Expand Down
165 changes: 75 additions & 90 deletions src/xlsx/cells_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use crate::{
Cell, XlsxError,
};

type FormulaMap = HashMap<(u32, u32), (i64, i64)>;

/// An xlsx Cell Iterator
pub struct XlsxCellReader<'a> {
xml: XlReader<'a>,
Expand All @@ -25,7 +27,7 @@ pub struct XlsxCellReader<'a> {
col_index: u32,
buf: Vec<u8>,
cell_buf: Vec<u8>,
formulas: Vec<Option<(String, HashMap<(u32, u32), (i64, i64)>)>>,
formulas: Vec<Option<(String, FormulaMap)>>,
}

impl<'a> XlsxCellReader<'a> {
Expand Down Expand Up @@ -173,98 +175,84 @@ impl<'a> XlsxCellReader<'a> {
if let Some(f) = formula.borrow() {
value = Some(f.clone());
}
match get_attribute(e.attributes(), QName(b"t")) {
Ok(Some(b"shared")) => {
// shared formula
let mut offset_map: HashMap<(u32, u32), (i64, i64)> =
HashMap::new();
// shared index
let shared_index =
match get_attribute(e.attributes(), QName(b"si"))? {
Some(res) => match std::str::from_utf8(res) {
Ok(res) => match usize::from_str_radix(res, 10)
{
Ok(res) => res,
Err(e) => {
return Err(XlsxError::ParseInt(e));
}
},
Err(_) => {
return Err(XlsxError::Unexpected(
"si attribute must be a number",
));
if let Ok(Some(b"shared")) =
get_attribute(e.attributes(), QName(b"t"))
{
// shared formula
let mut offset_map: HashMap<(u32, u32), (i64, i64)> =
HashMap::new();
// shared index
let shared_index =
match get_attribute(e.attributes(), QName(b"si"))? {
Some(res) => match std::str::from_utf8(res) {
Ok(res) => match res.parse::<usize>() {
Ok(res) => res,
Err(e) => {
return Err(XlsxError::ParseInt(e));
}
},
None => {
Err(_) => {
return Err(XlsxError::Unexpected(
"si attribute is mandatory if it is shared",
"si attribute must be a number",
));
}
};
// shared reference
match get_attribute(e.attributes(), QName(b"ref"))? {
Some(res) => {
// orignal reference formula
let reference = get_dimension(res)?;
if reference.start.0 != reference.end.0 {
for i in
0..=(reference.end.0 - reference.start.0)
{
offset_map.insert(
(
reference.start.0 + i,
reference.start.1,
),
(
(reference.start.0 as i64
- pos.0 as i64
+ i as i64),
0,
),
);
}
} else if reference.start.1 != reference.end.1 {
for i in
0..=(reference.end.1 - reference.start.1)
{
offset_map.insert(
(
reference.start.0,
reference.start.1 + i,
),
(
0,
(reference.start.1 as i64
- pos.1 as i64
+ i as i64),
),
);
}
},
None => {
return Err(XlsxError::Unexpected(
"si attribute is mandatory if it is shared",
));
}
};
// shared reference
match get_attribute(e.attributes(), QName(b"ref"))? {
Some(res) => {
// orignal reference formula
let reference = get_dimension(res)?;
if reference.start.0 != reference.end.0 {
for i in 0..=(reference.end.0 - reference.start.0) {
offset_map.insert(
(reference.start.0 + i, reference.start.1),
(
(reference.start.0 as i64
- pos.0 as i64
+ i as i64),
0,
),
);
}
} else if reference.start.1 != reference.end.1 {
for i in 0..=(reference.end.1 - reference.start.1) {
offset_map.insert(
(reference.start.0, reference.start.1 + i),
(
0,
(reference.start.1 as i64
- pos.1 as i64
+ i as i64),
),
);
}
}

if let Some(f) = formula.borrow() {
while self.formulas.len() < shared_index {
self.formulas.push(None);
}
self.formulas
.push(Some((f.clone(), offset_map)));
if let Some(f) = formula.borrow() {
while self.formulas.len() < shared_index {
self.formulas.push(None);
}
value = formula;
self.formulas.push(Some((f.clone(), offset_map)));
}
None => {
// calculated formula
if let Some(Some((f, offset_map))) =
self.formulas.get(shared_index)
{
if let Some(offset) = offset_map.get(&*&pos) {
value =
Some(replace_cell_names(f, *offset)?);
}
value = formula;
}
None => {
// calculated formula
if let Some(Some((f, offset_map))) =
self.formulas.get(shared_index)
{
if let Some(offset) = offset_map.get(&pos) {
value = Some(replace_cell_names(f, *offset)?);
}
}
};
}
_ => {}
}
};
};
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"c" => break,
Expand Down Expand Up @@ -333,10 +321,10 @@ fn read_v<'s>(
) -> Result<DataRef<'s>, XlsxError> {
let cell_format = match get_attribute(c_element.attributes(), QName(b"s")) {
Ok(Some(style)) => {
let id: usize = match std::str::from_utf8(style).unwrap_or("0").parse() {
Ok(parsed_id) => parsed_id,
Err(_) => 0,
};
let id: usize = std::str::from_utf8(style)
.unwrap_or("0")
.parse()
.unwrap_or(0);
formats.get(id)
}
_ => Some(&CellFormat::Other),
Expand Down Expand Up @@ -403,10 +391,7 @@ fn read_v<'s>(
}
}

fn read_formula<'s>(
xml: &mut XlReader<'_>,
e: &BytesStart<'_>,
) -> Result<Option<String>, XlsxError> {
fn read_formula(xml: &mut XlReader, e: &BytesStart) -> Result<Option<String>, XlsxError> {
match e.local_name().as_ref() {
b"is" | b"v" => {
xml.read_to_end_into(e.name(), &mut Vec::new())?;
Expand Down
Loading

0 comments on commit ed305da

Please sign in to comment.