Skip to main content

easyofd_core/doc/
document.rs

1//! 文档根节点。
2//!
3//! 对应 Java: org.ofdrw.core.basicStructure.doc.Document
4//! Document.xml 文档根节点,对应 GB/T 33190-2016 图 5
5
6use crate::basic_type::ST_Loc;
7
8/// 文档根节点。
9///
10/// Document.xml 文档根节点,定义了文档的页面、资源、权限等信息。
11///
12/// 对应 Java: org.ofdrw.core.basicStructure.doc.Document
13#[derive(Debug, Clone, Default, PartialEq)]
14pub struct Document {
15    /// 文档公共数据引用。
16    pub common_data: Option<String>,
17    /// 页树引用。
18    pub pages: Vec<PageRef>,
19    /// 大纲引用。
20    pub outlines: Option<String>,
21    /// 权限声明。
22    pub permissions: Option<String>,
23    /// 动作序列。
24    pub actions: Option<String>,
25    /// 视图首选项。
26    pub v_preferences: Option<String>,
27    /// 书签集。
28    pub bookmarks: Option<String>,
29    /// 注释列表路径。
30    pub annotations: Option<ST_Loc>,
31    /// 自定义标引列表路径。
32    pub custom_tags: Option<ST_Loc>,
33    /// 附件列表路径。
34    pub attachments: Option<ST_Loc>,
35    /// 扩展列表路径。
36    pub extensions: Option<ST_Loc>,
37}
38
39/// 页面引用。
40#[derive(Debug, Clone, PartialEq)]
41pub struct PageRef {
42    /// 页面 ID。
43    pub id: u32,
44    /// 页面文件路径。
45    pub base_loc: ST_Loc,
46}
47
48impl PageRef {
49    /// 创建页面引用。
50    #[must_use]
51    pub fn new(id: u32, base_loc: ST_Loc) -> Self {
52        Self { id, base_loc }
53    }
54}
55
56impl Document {
57    /// 创建新的文档根节点。
58    #[must_use]
59    pub fn new() -> Self {
60        Self::default()
61    }
62
63    /// 添加页面引用。
64    pub fn add_page(&mut self, page: PageRef) {
65        self.pages.push(page);
66    }
67
68    /// 获取页面数量。
69    #[must_use]
70    pub fn page_count(&self) -> usize {
71        self.pages.len()
72    }
73
74    /// 设置注释路径。
75    #[must_use]
76    pub fn annotations(mut self, path: ST_Loc) -> Self {
77        self.annotations = Some(path);
78        self
79    }
80
81    /// 设置附件路径。
82    #[must_use]
83    pub fn attachments(mut self, path: ST_Loc) -> Self {
84        self.attachments = Some(path);
85        self
86    }
87
88    /// 设置扩展路径。
89    #[must_use]
90    pub fn extensions(mut self, path: ST_Loc) -> Self {
91        self.extensions = Some(path);
92        self
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[test]
101    fn document_new() {
102        let doc = Document::new();
103        assert!(doc.pages.is_empty());
104        assert!(doc.annotations.is_none());
105    }
106
107    #[test]
108    fn document_add_page() {
109        let mut doc = Document::new();
110        doc.add_page(PageRef::new(1, ST_Loc::new("Pages/Page_0.xml")));
111        doc.add_page(PageRef::new(2, ST_Loc::new("Pages/Page_1.xml")));
112        assert_eq!(doc.page_count(), 2);
113    }
114
115    #[test]
116    fn page_ref_new() {
117        let pr = PageRef::new(1, ST_Loc::new("Pages/Page_0.xml"));
118        assert_eq!(pr.id, 1);
119        assert_eq!(pr.base_loc.loc(), "Pages/Page_0.xml");
120    }
121
122    #[test]
123    fn document_builder() {
124        let doc = Document::new()
125            .annotations(ST_Loc::new("Annotations.xml"))
126            .attachments(ST_Loc::new("Attachments.xml"));
127        assert!(doc.annotations.is_some());
128        assert!(doc.attachments.is_some());
129    }
130
131    #[test]
132    fn document_clone_debug() {
133        let doc = Document::new();
134        let doc2 = doc.clone();
135        assert_eq!(doc2.page_count(), 0);
136        assert!(format!("{doc:?}").contains("Document"));
137    }
138}