office-rs 0.1.1

A Rust library for reading and writing XML Office files
Documentation
# Office-RS 统一API设计方案


## 设计原则


### 1. 统一的构建器接口


所有Office格式都实现相同的核心接口,确保API的一致性和易学性:

```rust
// 统一的文档构建器特征
pub trait DocumentBuilder<T> {
    fn new() -> Self;
    fn open<P: AsRef<Path>>(self, path: P) -> Result<&mut Self>;  
    fn read<P: AsRef<Path>>(self, path: P) -> Result<&Self>;  
    fn save<P: AsRef<Path>>(self, path: P) -> Result<()>;
    fn build(self) -> Result<T>;
}

// 统一的错误类型
pub type Result<T> = std::result::Result<T, OfficeError>;

#[derive(Debug, thiserror::Error)]

pub enum OfficeError {
    #[error("IO错误: {0}")]
    Io(#[from] std::io::Error),
    
    #[error("文档结构无效: {0}")]
    InvalidStructure(String),
    
    #[error("不支持的功能: {0}")]
    UnsupportedFeature(String),
    
    #[error("格式错误: {0}")]
    FormatError(String),
    
    #[error("解析错误: {0}")]
    ParseError(String),
}
```

### 2. 格式特定实现


每种格式都有自己的构建器,但遵循统一的模式:

```rust
// DOCX 构建器
pub struct DocxBuilder {
    document: Document,
}

impl DocumentBuilder<Document> for DocxBuilder {
    fn new() -> Self { /* ... */ }
    fn save<P: AsRef<Path>>(self, path: P) -> Result<()> { /* ... */ }
    fn build(self) -> Result<Document> { /* ... */ }
}

// XLSX 构建器
pub struct XlsxBuilder {
    document: Document,
}

impl DocumentBuilder<Document> for XlsxBuilder {
    fn new() -> Self { /* ... */ }
    fn save<P: AsRef<Path>>(self, path: P) -> Result<()> { /* ... */ }
    fn build(self) -> Result<Document> { /* ... */ }
}

// PPTX 构建器
pub struct PptxBuilder {
    document: Document,
}

impl DocumentBuilder<Document> for PptxBuilder {
    fn new() -> Self { /* ... */ }
    fn save<P: AsRef<Path>>(self, path: P) -> Result<()> { /* ... */ }
    fn build(self) -> Result<Document> { /* ... */ }
}

// OFD 构建器
pub struct OfdBuilder {
    document: Document,
}

impl DocumentBuilder<Document> for OfdBuilder {
    fn new() -> Self { /* ... */ }
    fn save<P: AsRef<Path>>(self, path: P) -> Result<()> { /* ... */ }
    fn build(self) -> Result<Document> { /* ... */ }
}
```

## 统一的使用模式


### 1. 简洁的文档创建


所有格式都支持链式调用和闭包构建器:

```rust
// Word 文档
DocxBuilder::new()
    .paragraph(|p| p.text("Hello").formatted_text("World", |f| f.bold()))
    .table(|t| t.row(["A", "B"]).row(["1", "2"]))
    .save("document.docx")?;

// Excel 工作簿
XlsxBuilder::new()
    .worksheet("Sheet1", |sheet| sheet
        .cell("A1", "Title").style(Styles::title())
        .row(2, &["Data1", "Data2", "Data3"])
    )
    .save("workbook.xlsx")?;

// PowerPoint 演示文稿
PptxBuilder::new()
    .slide(|slide| slide
        .title("Welcome")
        .text("Content", Position::center(400.0, 200.0))
    )
    .save("presentation.pptx")?;

// OFD 文档
OfdBuilder::new()
    .metadata("Title", "Author")
    .page(|page| page
        .text("Hello OFD", 100.0, 100.0)
        .rectangle(50.0, 150.0, 200.0, 100.0)
    )
    .save("document.ofd")?;
```

### 2. 预定义样式和布局


每种格式都提供常用的预定义样式:

```rust
// Excel 样式
Styles::title()    // 标题样式
Styles::header()   // 表头样式
Styles::number()   // 数字样式

// PowerPoint 布局
Layouts::title_slide()   // 标题页布局
Layouts::content()       // 内容页布局
Layouts::two_content()   // 双栏布局

// OFD 页面尺寸
PageSizes::a4()     // A4 尺寸
PageSizes::a3()     // A3 尺寸
PageSizes::letter() // Letter 尺寸
```

## 关键优势


### 1. 学习成本低

- 统一的API模式,学会一种格式即可快速掌握其他格式
- 一致的命名约定和参数顺序
- 相似的错误处理机制

### 2. 代码复用性高

- 共享的核心特征和错误类型
- 可以编写通用的文档处理函数
- 统一的测试和文档模式

### 3. 类型安全

- 充分利用Rust的类型系统
- 编译时检查确保API使用正确
- 避免运行时错误

### 4. 易于扩展

- 新格式可以轻松集成到现有框架中
- 预留的扩展点支持高级功能
- 模块化设计便于维护

### 5. 性能优化

- 零拷贝设计减少内存分配
- 批量操作支持提高效率
- 延迟计算优化资源使用

## 实现建议


### 1. 开发顺序

1. 实现统一的核心特征和错误类型
2. 从最简单的格式开始(如OFD)
3. 逐步添加更复杂的格式(DOCX, XLSX, PPTX)
4. 完善文档和示例
5. 添加高级功能和优化

### 2. 测试策略

- 为每种格式编写全面的单元测试
- 集成测试验证格式兼容性
- 性能测试确保效率
- 文档测试保证示例代码正确

### 3. 文档要求

- 每个API都有清晰的文档说明
- 提供丰富的使用示例
- 包含最佳实践指南
- 维护更新日志和迁移指南

通过这种统一的API设计,office-rs将成为Rust生态系统中最易用、最强大的Office文档处理库。