Skip to main content

memvid_core/reader/
docx.rs

1use std::io::{Cursor, Read};
2
3use quick_xml::Reader as XmlReader;
4use quick_xml::events::Event;
5use zip::ZipArchive;
6
7use crate::{
8    DocumentFormat, DocumentReader, PassthroughReader, ReaderDiagnostics, ReaderHint, ReaderOutput,
9    Result,
10};
11
12const DOC_XML_PATH: &str = "word/document.xml";
13
14pub struct DocxReader;
15
16impl DocxReader {
17    fn extract_text(bytes: &[u8]) -> Result<String> {
18        let cursor = Cursor::new(bytes);
19        let mut archive =
20            ZipArchive::new(cursor).map_err(|err| crate::MemvidError::ExtractionFailed {
21                reason: format!("failed to open docx archive: {err}").into(),
22            })?;
23
24        let mut file =
25            archive
26                .by_name(DOC_XML_PATH)
27                .map_err(|err| crate::MemvidError::ExtractionFailed {
28                    reason: format!("docx missing document.xml: {err}").into(),
29                })?;
30        let mut xml = String::new();
31        file.read_to_string(&mut xml)
32            .map_err(|err| crate::MemvidError::ExtractionFailed {
33                reason: format!("failed to read document.xml: {err}").into(),
34            })?;
35
36        Ok(extract_plain_text(&xml, b"w:p"))
37    }
38}
39
40impl DocumentReader for DocxReader {
41    fn name(&self) -> &'static str {
42        "docx"
43    }
44
45    fn supports(&self, hint: &ReaderHint<'_>) -> bool {
46        matches!(hint.format, Some(DocumentFormat::Docx))
47            || hint.mime.is_some_and(|mime| {
48                mime.eq_ignore_ascii_case(
49                    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
50                )
51            })
52    }
53
54    fn extract(&self, bytes: &[u8], hint: &ReaderHint<'_>) -> Result<ReaderOutput> {
55        match Self::extract_text(bytes) {
56            Ok(text) => {
57                if text.trim().is_empty() {
58                    // quick-xml returned empty - try extractous as fallback
59                    let mut output = PassthroughReader.extract(bytes, hint)?;
60                    output.reader_name = self.name().to_string();
61                    output.diagnostics.mark_fallback();
62                    output.diagnostics.record_warning(
63                        "docx reader produced empty text; falling back to default extractor",
64                    );
65                    Ok(output)
66                } else {
67                    // quick-xml succeeded - build output directly WITHOUT calling extractous
68                    let mut document = crate::ExtractedDocument::empty();
69                    document.text = Some(text);
70                    document.mime_type = Some(
71                        "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
72                            .to_string(),
73                    );
74                    Ok(ReaderOutput::new(document, self.name())
75                        .with_diagnostics(ReaderDiagnostics::default()))
76                }
77            }
78            Err(err) => {
79                // quick-xml failed - try extractous as fallback
80                let mut fallback = PassthroughReader.extract(bytes, hint)?;
81                fallback.reader_name = self.name().to_string();
82                fallback.diagnostics.mark_fallback();
83                fallback
84                    .diagnostics
85                    .record_warning(format!("docx reader error: {err}"));
86                Ok(fallback)
87            }
88        }
89    }
90}
91
92fn extract_plain_text(xml: &str, block_tag: &[u8]) -> String {
93    let mut reader = XmlReader::from_str(xml);
94    reader.trim_text(true);
95    let mut buf = Vec::new();
96    let mut text = String::new();
97    let mut first_block = true;
98
99    loop {
100        match reader.read_event_into(&mut buf) {
101            Ok(Event::Start(e)) => {
102                if e.name().as_ref().ends_with(block_tag) {
103                    if !first_block {
104                        text.push('\n');
105                    }
106                    first_block = false;
107                }
108            }
109            Ok(Event::Text(t)) => {
110                if let Ok(content) = t.unescape() {
111                    if !content.trim().is_empty() {
112                        text.push_str(content.trim());
113                        text.push(' ');
114                    }
115                }
116            }
117            Ok(Event::Eof) => break,
118            Err(_) => break,
119            _ => (),
120        }
121        buf.clear();
122    }
123
124    text.trim().to_string()
125}