Skip to main content

easydoc_reader/builder/
read_builder.rs

1//! 流式文档读取器构建器。
2
3use std::path::PathBuf;
4
5use crate::extractor;
6use easydoc_core::{DocxRow, Result};
7
8/// 流式文档读取的 Fluent 构建器。
9///
10/// 通过门面 `EasyDoc::read()` 方法创建。
11///
12/// 对应 Java: `EasyExcel.read(path).head(RowClass.class)`
13pub struct DocReadBuilder {
14    path: PathBuf,
15}
16
17impl DocReadBuilder {
18    /// 创建新的读取器构建器。
19    #[must_use]
20    pub fn new(path: impl Into<PathBuf>) -> Self {
21        Self { path: path.into() }
22    }
23
24    /// 执行同步读取,返回所有表格展平后的 `Vec<T>`。
25    ///
26    /// 使用 `office_oxide` 进行后端解析。
27    ///
28    /// 对应 Java: `EasyExcel.read(path).head(RowClass.class).sheet().doReadSync()`
29    ///
30    /// # Errors
31    ///
32    /// 返回 I/O、格式或转换错误。
33    pub fn do_read<T: DocxRow>(self) -> Result<Vec<T>> {
34        let tables: Vec<Vec<T>> = extractor::table::extract_tables::<T>(&self.path)?;
35        Ok(tables.into_iter().flatten().collect())
36    }
37}