easyofd_core/doc/
document.rs1use crate::basic_type::ST_Loc;
7
8#[derive(Debug, Clone, Default, PartialEq)]
14pub struct Document {
15 pub common_data: Option<String>,
17 pub pages: Vec<PageRef>,
19 pub outlines: Option<String>,
21 pub permissions: Option<String>,
23 pub actions: Option<String>,
25 pub v_preferences: Option<String>,
27 pub bookmarks: Option<String>,
29 pub annotations: Option<ST_Loc>,
31 pub custom_tags: Option<ST_Loc>,
33 pub attachments: Option<ST_Loc>,
35 pub extensions: Option<ST_Loc>,
37}
38
39#[derive(Debug, Clone, PartialEq)]
41pub struct PageRef {
42 pub id: u32,
44 pub base_loc: ST_Loc,
46}
47
48impl PageRef {
49 #[must_use]
51 pub fn new(id: u32, base_loc: ST_Loc) -> Self {
52 Self { id, base_loc }
53 }
54}
55
56impl Document {
57 #[must_use]
59 pub fn new() -> Self {
60 Self::default()
61 }
62
63 pub fn add_page(&mut self, page: PageRef) {
65 self.pages.push(page);
66 }
67
68 #[must_use]
70 pub fn page_count(&self) -> usize {
71 self.pages.len()
72 }
73
74 #[must_use]
76 pub fn annotations(mut self, path: ST_Loc) -> Self {
77 self.annotations = Some(path);
78 self
79 }
80
81 #[must_use]
83 pub fn attachments(mut self, path: ST_Loc) -> Self {
84 self.attachments = Some(path);
85 self
86 }
87
88 #[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}