Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ quick-xml = { version = "0.31.0", features = ["serialize"] }
csv = "1.3.1"
serde = { version = "1", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }
tracing-subscriber = { version = "0.3.22", features = ["env-filter"] }
tracing = "0.1.44"

[dev-dependencies]
tempfile = "3"
Expand Down
21 changes: 20 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use quick_xml::reader::Reader;
use std::collections::BTreeMap;
use std::io::BufRead;
use std::path::Path;
use tracing::{debug, instrument};

#[derive(Debug, Clone)]
pub struct SheetInfo {
Expand All @@ -20,6 +21,7 @@ pub struct StyleInfo {
pub is_date: bool,
}

#[instrument(level = "debug", skip(reader))]
pub fn parse_styles<R: BufRead>(reader: R) -> Result<Vec<StyleInfo>> {
let mut xml = Reader::from_reader(reader);
let mut buf = Vec::new();
Expand Down Expand Up @@ -130,6 +132,7 @@ pub fn parse_styles<R: BufRead>(reader: R) -> Result<Vec<StyleInfo>> {
}
buf.clear();
}
debug!(style_count = styles.len(), "Parsed styles");
Ok(styles)
}

Expand All @@ -140,6 +143,7 @@ fn tag_eq_ignore_case(actual: &[u8], expect: &str) -> bool {
|| actual.ends_with(expect.to_ascii_uppercase().as_bytes())
}

#[instrument(level = "debug", skip(reader))]
pub fn parse_workbook_rels<R: BufRead>(reader: R) -> Result<BTreeMap<String, String>> {
// Map r:Id -> full path inside zip (xl/worksheets/sheet1.xml)
let mut xml = Reader::from_reader(reader);
Expand Down Expand Up @@ -172,9 +176,11 @@ pub fn parse_workbook_rels<R: BufRead>(reader: R) -> Result<BTreeMap<String, Str
}
buf.clear();
}
debug!(rel_count = map.len(), "Parsed workbook relationships");
Ok(map)
}

#[instrument(level = "debug", skip(reader, rels))]
pub fn parse_workbook<R: BufRead>(
reader: R,
rels: &BTreeMap<String, String>,
Expand Down Expand Up @@ -225,9 +231,15 @@ pub fn parse_workbook<R: BufRead>(
}
buf.clear();
}
debug!(
sheet_count = sheets.len(),
is_1904 = is_1904,
"Parsed workbook"
);
Ok((sheets, is_1904))
}

#[instrument(level = "debug", skip(reader))]
pub fn read_shared_strings<R: BufRead>(reader: R) -> Result<Vec<String>> {
let mut xml = Reader::from_reader(reader);
// xml.config_mut().trim_text(true);
Expand Down Expand Up @@ -264,6 +276,7 @@ pub fn read_shared_strings<R: BufRead>(reader: R) -> Result<Vec<String>> {
}
buf.clear();
}
debug!(string_count = strings.len(), "Parsed shared strings");
Ok(strings)
}

Expand Down Expand Up @@ -320,7 +333,11 @@ pub fn to_lowercase_filename(name: &str) -> String {
})
.collect();

if s.is_empty() { "sheet".to_string() } else { s }
if s.is_empty() {
"sheet".to_string()
} else {
s
}
}

// Excel date/time utilities
Expand Down Expand Up @@ -353,6 +370,7 @@ pub fn excel_serial_to_iso_date(serial: f64, is_1904: bool) -> Option<String> {
Some(datetime.format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string())
}

#[instrument(level = "debug", skip(reader, shared_strings, styles), fields(out_path = %out_path.display()))]
pub fn export_sheet_xml_to_csv<R: BufRead>(
reader: R,
shared_strings: &[String],
Expand Down Expand Up @@ -509,6 +527,7 @@ pub fn export_sheet_xml_to_csv<R: BufRead>(
wtr.write_record(row_vals.iter())?;
}
wtr.flush()?;
debug!(total_rows = current_row_idx, "Exported sheet to CSV");
Ok(())
}

Expand Down
Loading
Loading