memvid_core/reader/
xlsx.rs1use std::io::Cursor;
2
3use calamine::{DataType, Reader as CalamineReader, Xlsx};
4
5use crate::{
6 DocumentFormat, DocumentReader, PassthroughReader, ReaderDiagnostics, ReaderHint, ReaderOutput,
7 Result,
8};
9
10pub struct XlsxReader;
11
12impl XlsxReader {
13 fn extract_text(bytes: &[u8]) -> Result<String> {
14 let cursor = Cursor::new(bytes);
15 let mut workbook =
16 Xlsx::new(cursor).map_err(|err| crate::MemvidError::ExtractionFailed {
17 reason: format!("failed to read xlsx workbook: {err}").into(),
18 })?;
19
20 let mut out = String::new();
21 for sheet_name in workbook.sheet_names().clone() {
22 if let Some(Ok(range)) = workbook.worksheet_range(&sheet_name) {
23 if !out.is_empty() {
24 out.push('\n');
25 }
26 out.push_str(&format!("Sheet: {sheet_name}\n"));
27 for row in range.rows() {
28 let mut first_cell = true;
29 for cell in row {
30 if !first_cell {
31 out.push('\t');
32 }
33 first_cell = false;
34 match cell {
35 DataType::String(s) => out.push_str(s.trim()),
36 DataType::Float(v) => out.push_str(&format!("{v}")),
37 DataType::Int(v) => out.push_str(&format!("{v}")),
38 DataType::Bool(b) => out.push_str(if *b { "true" } else { "false" }),
39 DataType::Error(e) => out.push_str(&format!("#{e:?}")),
40 DataType::Empty => {}
41 DataType::DateTime(v) => out.push_str(&format!("{v}")),
42 DataType::DateTimeIso(s) => out.push_str(s),
43 DataType::Duration(v) => out.push_str(&format!("{v}")),
44 DataType::DurationIso(s) => out.push_str(s),
45 }
46 }
47 out.push('\n');
48 }
49 }
50 }
51
52 Ok(out.trim().to_string())
53 }
54}
55
56impl DocumentReader for XlsxReader {
57 fn name(&self) -> &'static str {
58 "xlsx"
59 }
60
61 fn supports(&self, hint: &ReaderHint<'_>) -> bool {
62 matches!(hint.format, Some(DocumentFormat::Xlsx))
63 || hint.mime.is_some_and(|mime| {
64 mime.eq_ignore_ascii_case(
65 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
66 )
67 })
68 }
69
70 fn extract(&self, bytes: &[u8], hint: &ReaderHint<'_>) -> Result<ReaderOutput> {
71 match Self::extract_text(bytes) {
72 Ok(text) => {
73 if text.trim().is_empty() {
74 let mut fallback = PassthroughReader.extract(bytes, hint)?;
76 fallback.reader_name = self.name().to_string();
77 fallback.diagnostics.mark_fallback();
78 fallback.diagnostics.record_warning(
79 "xlsx reader produced empty text; falling back to default extractor",
80 );
81 Ok(fallback)
82 } else {
83 let mut document = crate::ExtractedDocument::empty();
85 document.text = Some(text);
86 document.mime_type = Some(
87 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
88 .to_string(),
89 );
90 Ok(ReaderOutput::new(document, self.name())
91 .with_diagnostics(ReaderDiagnostics::default()))
92 }
93 }
94 Err(err) => {
95 let mut fallback = PassthroughReader.extract(bytes, hint)?;
97 fallback.reader_name = self.name().to_string();
98 fallback.diagnostics.mark_fallback();
99 fallback
100 .diagnostics
101 .record_warning(format!("xlsx reader error: {err}"));
102 Ok(fallback)
103 }
104 }
105 }
106}