1use serde::ser::{SerializeStruct, Serializer};
2use serde::Serialize;
3use std::io::Write;
4
5use super::*;
6use crate::documents::BuildXML;
7use crate::xml_builder::*;
8
9#[derive(Debug, Clone, PartialEq, Serialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Document {
12 pub children: Vec<DocumentChild>,
13 pub section_property: SectionProperty,
14 pub has_numbering: bool,
15}
16
17#[derive(Debug, Clone, PartialEq)]
18pub enum DocumentChild {
19 Paragraph(Box<Paragraph>),
20 Table(Box<Table>),
21 BookmarkStart(BookmarkStart),
22 BookmarkEnd(BookmarkEnd),
23 CommentStart(Box<CommentRangeStart>),
24 CommentEnd(CommentRangeEnd),
25 StructuredDataTag(Box<StructuredDataTag>),
26 TableOfContents(Box<TableOfContents>),
27 Section(Box<Section>),
28}
29
30impl Serialize for DocumentChild {
31 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
32 where
33 S: Serializer,
34 {
35 match *self {
36 DocumentChild::Paragraph(ref p) => {
37 let mut t = serializer.serialize_struct("Paragraph", 2)?;
38 t.serialize_field("type", "paragraph")?;
39 t.serialize_field("data", p)?;
40 t.end()
41 }
42 DocumentChild::Table(ref c) => {
43 let mut t = serializer.serialize_struct("Table", 2)?;
44 t.serialize_field("type", "table")?;
45 t.serialize_field("data", c)?;
46 t.end()
47 }
48 DocumentChild::BookmarkStart(ref c) => {
49 let mut t = serializer.serialize_struct("BookmarkStart", 2)?;
50 t.serialize_field("type", "bookmarkStart")?;
51 t.serialize_field("data", c)?;
52 t.end()
53 }
54 DocumentChild::BookmarkEnd(ref c) => {
55 let mut t = serializer.serialize_struct("BookmarkEnd", 2)?;
56 t.serialize_field("type", "bookmarkEnd")?;
57 t.serialize_field("data", c)?;
58 t.end()
59 }
60 DocumentChild::CommentStart(ref r) => {
61 let mut t = serializer.serialize_struct("CommentRangeStart", 2)?;
62 t.serialize_field("type", "commentRangeStart")?;
63 t.serialize_field("data", r)?;
64 t.end()
65 }
66 DocumentChild::CommentEnd(ref r) => {
67 let mut t = serializer.serialize_struct("CommentRangeEnd", 2)?;
68 t.serialize_field("type", "commentRangeEnd")?;
69 t.serialize_field("data", r)?;
70 t.end()
71 }
72 DocumentChild::StructuredDataTag(ref r) => {
73 let mut t = serializer.serialize_struct("StructuredDataTag", 2)?;
74 t.serialize_field("type", "structuredDataTag")?;
75 t.serialize_field("data", r)?;
76 t.end()
77 }
78 DocumentChild::TableOfContents(ref r) => {
79 let mut t = serializer.serialize_struct("TableOfContents", 2)?;
80 t.serialize_field("type", "tableOfContents")?;
81 t.serialize_field("data", r)?;
82 t.end()
83 }
84 DocumentChild::Section(ref r) => {
85 let mut t = serializer.serialize_struct("Section", 2)?;
86 t.serialize_field("type", "section")?;
87 t.serialize_field("data", r)?;
88 t.end()
89 }
90 }
91 }
92}
93
94impl Default for Document {
95 fn default() -> Self {
96 Self {
97 children: Vec::new(),
98 section_property: SectionProperty::new(),
99 has_numbering: false,
100 }
101 }
102}
103
104impl Document {
105 pub fn new() -> Document {
106 Default::default()
107 }
108
109 pub fn add_paragraph(mut self, p: Paragraph) -> Self {
110 if p.has_numbering {
111 self.has_numbering = true
112 }
113 self.children.push(DocumentChild::Paragraph(Box::new(p)));
114 self
115 }
116
117 pub fn add_table(mut self, t: Table) -> Self {
118 if t.has_numbering {
119 self.has_numbering = true
120 }
121 self.children.push(DocumentChild::Table(Box::new(t)));
122 self
123 }
124
125 pub fn add_bookmark_start(mut self, id: usize, name: impl Into<String>) -> Self {
126 self.children
127 .push(DocumentChild::BookmarkStart(BookmarkStart::new(id, name)));
128 self
129 }
130
131 pub fn add_bookmark_end(mut self, id: usize) -> Self {
132 self.children
133 .push(DocumentChild::BookmarkEnd(BookmarkEnd::new(id)));
134 self
135 }
136
137 pub fn add_comment_start(mut self, comment: Comment) -> Self {
138 self.children.push(DocumentChild::CommentStart(Box::new(
139 CommentRangeStart::new(comment),
140 )));
141 self
142 }
143
144 pub fn add_comment_end(mut self, id: usize) -> Self {
145 self.children
146 .push(DocumentChild::CommentEnd(CommentRangeEnd::new(id)));
147 self
148 }
149
150 pub fn add_section(mut self, sec: Section) -> Self {
151 self.children.push(DocumentChild::Section(Box::new(sec)));
152 self
153 }
154
155 pub fn title_pg(mut self) -> Self {
156 self.section_property = self.section_property.title_pg();
157 self
158 }
159
160 pub fn page_size(mut self, size: PageSize) -> Self {
161 self.section_property = self.section_property.page_size(size);
162 self
163 }
164
165 pub fn page_margin(mut self, margin: crate::types::PageMargin) -> Self {
166 self.section_property = self.section_property.page_margin(margin);
167 self
168 }
169
170 pub fn page_orient(mut self, o: crate::types::PageOrientationType) -> Self {
171 self.section_property = self.section_property.page_orient(o);
172 self
173 }
174
175 pub fn doc_grid(mut self, doc_grid: DocGrid) -> Self {
176 self.section_property = self.section_property.doc_grid(doc_grid);
177 self
178 }
179
180 pub fn default_section_property(mut self, property: SectionProperty) -> Self {
181 self.section_property = property;
182 self
183 }
184
185 pub fn header(mut self, h: Header, rid: &str) -> Self {
186 self.section_property = self.section_property.header(h, rid);
187 self
188 }
189
190 pub fn first_header(mut self, h: Header, rid: &str) -> Self {
191 self.section_property = self.section_property.first_header(h, rid);
192 self
193 }
194
195 pub(crate) fn first_header_without_title_pg(mut self, h: Header, rid: &str) -> Self {
196 self.section_property = self.section_property.first_header_without_title_pg(h, rid);
197 self
198 }
199
200 pub fn even_header(mut self, h: Header, rid: &str) -> Self {
201 self.section_property = self.section_property.even_header(h, rid);
202 self
203 }
204
205 pub fn footer(mut self, h: Footer, rid: &str) -> Self {
206 self.section_property = self.section_property.footer(h, rid);
207 self
208 }
209
210 pub fn first_footer(mut self, h: Footer, rid: &str) -> Self {
211 self.section_property = self.section_property.first_footer(h, rid);
212 self
213 }
214
215 pub(crate) fn first_footer_without_title_pg(mut self, h: Footer, rid: &str) -> Self {
216 self.section_property = self.section_property.first_footer_without_title_pg(h, rid);
217 self
218 }
219
220 pub fn even_footer(mut self, h: Footer, rid: &str) -> Self {
221 self.section_property = self.section_property.even_footer(h, rid);
222 self
223 }
224
225 pub fn add_structured_data_tag(mut self, t: StructuredDataTag) -> Self {
226 if t.has_numbering {
227 self.has_numbering = true
228 }
229 self.children
230 .push(DocumentChild::StructuredDataTag(Box::new(t)));
231 self
232 }
233
234 pub fn add_table_of_contents(mut self, t: TableOfContents) -> Self {
235 self.children
236 .push(DocumentChild::TableOfContents(Box::new(t)));
237 self
238 }
239
240 pub fn columns(mut self, col: usize) -> Self {
241 self.section_property.columns = col;
242 self
243 }
244
245 pub fn text_direction(mut self, direction: String) -> Self {
246 self.section_property.text_direction = direction;
247 self
248 }
249
250 pub fn page_num_type(mut self, p: PageNumType) -> Self {
251 self.section_property = self.section_property.page_num_type(p);
252 self
253 }
254}
255
256impl BuildXML for DocumentChild {
257 fn build_to<W: Write>(
258 &self,
259 stream: xml::writer::EventWriter<W>,
260 ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
261 match self {
262 DocumentChild::Paragraph(v) => v.build_to(stream),
263 DocumentChild::Table(v) => v.build_to(stream),
264 DocumentChild::BookmarkStart(v) => v.build_to(stream),
265 DocumentChild::BookmarkEnd(v) => v.build_to(stream),
266 DocumentChild::CommentStart(v) => v.build_to(stream),
267 DocumentChild::CommentEnd(v) => v.build_to(stream),
268 DocumentChild::StructuredDataTag(v) => v.build_to(stream),
269 DocumentChild::TableOfContents(v) => v.build_to(stream),
270 DocumentChild::Section(v) => v.build_to(stream),
271 }
272 }
273}
274
275impl BuildXML for Document {
276 fn build_to<W: Write>(
277 &self,
278 stream: xml::writer::EventWriter<W>,
279 ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
280 XMLBuilder::from(stream)
281 .declaration(Some(true))?
282 .open_document()?
283 .open_body()?
284 .add_children(&self.children)?
285 .add_child(&self.section_property)?
286 .close()?
287 .close()?
288 .into_inner()
289 }
290}
291
292#[cfg(test)]
293mod tests {
294
295 use super::super::Run;
296 use super::*;
297 #[cfg(test)]
298 use pretty_assertions::assert_eq;
299 use std::str;
300
301 #[test]
302 fn test_document() {
303 let b = Document::new()
304 .add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello")))
305 .build();
306 assert_eq!(
307 str::from_utf8(&b).unwrap(),
308 r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 wp14"><w:body><w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r></w:p><w:sectPr><w:pgSz w:w="11906" w:h="16838" /><w:pgMar w:top="1985" w:right="1701" w:bottom="1701" w:left="1701" w:header="851" w:footer="992" w:gutter="0" /><w:cols w:space="425" w:num="1" /></w:sectPr></w:body></w:document>"#
309 );
310 }
311
312 #[test]
313 fn test_document_with_toc() {
314 let toc = TableOfContents::new().heading_styles_range(1, 3);
315 let b = Document::new().add_table_of_contents(toc).build();
316 assert_eq!(
317 str::from_utf8(&b).unwrap(),
318 r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 wp14"><w:body><w:sdt><w:sdtPr><w:rPr /></w:sdtPr><w:sdtContent><w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType="begin" w:dirty="true" /><w:instrText>TOC \o "1-3"</w:instrText><w:fldChar w:fldCharType="separate" w:dirty="false" /></w:r></w:p><w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:fldChar w:fldCharType="end" w:dirty="false" /></w:r></w:p></w:sdtContent></w:sdt><w:sectPr><w:pgSz w:w="11906" w:h="16838" /><w:pgMar w:top="1985" w:right="1701" w:bottom="1701" w:left="1701" w:header="851" w:footer="992" w:gutter="0" /><w:cols w:space="425" w:num="1" /></w:sectPr></w:body></w:document>"#
319 );
320 }
321
322 #[test]
323 fn test_document_cols() {
324 let b = Document::new()
325 .columns(2)
326 .add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello")))
327 .build();
328 assert_eq!(
329 str::from_utf8(&b).unwrap(),
330 r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 wp14"><w:body><w:p w14:paraId="12345678"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r></w:p><w:sectPr><w:pgSz w:w="11906" w:h="16838" /><w:pgMar w:top="1985" w:right="1701" w:bottom="1701" w:left="1701" w:header="851" w:footer="992" w:gutter="0" /><w:cols w:space="425" w:num="2" /></w:sectPr></w:body></w:document>"#
331 );
332 }
333}