docx_reader/documents/
document.rs1use serde::ser::{SerializeStruct, Serializer};
2use serde::Serialize;
3
4use super::*;
5
6#[derive(Debug, Clone, PartialEq, Serialize)]
7#[serde(rename_all = "camelCase")]
8pub struct Document {
9 pub children: Vec<DocumentChild>,
10 pub section_property: SectionProperty,
11 pub has_numbering: bool,
12}
13
14#[derive(Debug, Clone, PartialEq)]
15pub enum DocumentChild {
16 Paragraph(Box<Paragraph>),
17 Table(Box<Table>),
18 StructuredDataTag(Box<StructuredDataTag>),
19 TableOfContents(Box<TableOfContents>),
20}
21
22impl Serialize for DocumentChild {
23 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
24 where
25 S: Serializer,
26 {
27 match *self {
28 DocumentChild::Paragraph(ref p) => {
29 let mut t = serializer.serialize_struct("Paragraph", 2)?;
30 t.serialize_field("type", "paragraph")?;
31 t.serialize_field("data", p)?;
32 t.end()
33 }
34 DocumentChild::Table(ref c) => {
35 let mut t = serializer.serialize_struct("Table", 2)?;
36 t.serialize_field("type", "table")?;
37 t.serialize_field("data", c)?;
38 t.end()
39 }
40 DocumentChild::StructuredDataTag(ref r) => {
41 let mut t = serializer.serialize_struct("StructuredDataTag", 2)?;
42 t.serialize_field("type", "structuredDataTag")?;
43 t.serialize_field("data", r)?;
44 t.end()
45 }
46 DocumentChild::TableOfContents(ref r) => {
47 let mut t = serializer.serialize_struct("TableOfContents", 2)?;
48 t.serialize_field("type", "tableOfContents")?;
49 t.serialize_field("data", r)?;
50 t.end()
51 }
52 }
53 }
54}
55
56impl Default for Document {
57 fn default() -> Self {
58 Self {
59 children: Vec::new(),
60 section_property: SectionProperty::new(),
61 has_numbering: false,
62 }
63 }
64}
65
66impl Document {
67 pub fn new() -> Document {
68 Default::default()
69 }
70
71 pub fn add_paragraph(mut self, p: Paragraph) -> Self {
72 if p.has_numbering {
73 self.has_numbering = true
74 }
75 self.children.push(DocumentChild::Paragraph(Box::new(p)));
76 self
77 }
78
79 pub fn add_table(mut self, t: Table) -> Self {
80 if t.has_numbering {
81 self.has_numbering = true
82 }
83 self.children.push(DocumentChild::Table(Box::new(t)));
84 self
85 }
86
87 pub fn page_size(mut self, size: PageSize) -> Self {
88 self.section_property = self.section_property.page_size(size);
89 self
90 }
91
92 pub fn page_margin(mut self, margin: crate::types::PageMargin) -> Self {
93 self.section_property = self.section_property.page_margin(margin);
94 self
95 }
96
97 pub fn page_orient(mut self, o: crate::types::PageOrientationType) -> Self {
98 self.section_property = self.section_property.page_orient(o);
99 self
100 }
101
102 pub fn doc_grid(mut self, doc_grid: DocGrid) -> Self {
103 self.section_property = self.section_property.doc_grid(doc_grid);
104 self
105 }
106
107 pub fn default_section_property(mut self, property: SectionProperty) -> Self {
108 self.section_property = property;
109 self
110 }
111
112 pub fn header(mut self, h: Header, rid: &str) -> Self {
113 self.section_property = self.section_property.header(h, rid);
114 self
115 }
116
117 pub fn first_header(mut self, h: Header, rid: &str) -> Self {
118 self.section_property = self.section_property.first_header(h, rid);
119 self
120 }
121
122 pub fn even_header(mut self, h: Header, rid: &str) -> Self {
123 self.section_property = self.section_property.even_header(h, rid);
124 self
125 }
126
127 pub fn footer(mut self, h: Footer, rid: &str) -> Self {
128 self.section_property = self.section_property.footer(h, rid);
129 self
130 }
131
132 pub fn first_footer(mut self, h: Footer, rid: &str) -> Self {
133 self.section_property = self.section_property.first_footer(h, rid);
134 self
135 }
136
137 pub fn even_footer(mut self, h: Footer, rid: &str) -> Self {
138 self.section_property = self.section_property.even_footer(h, rid);
139 self
140 }
141
142 pub fn add_structured_data_tag(mut self, t: StructuredDataTag) -> Self {
143 if t.has_numbering {
144 self.has_numbering = true
145 }
146 self.children
147 .push(DocumentChild::StructuredDataTag(Box::new(t)));
148 self
149 }
150
151 pub fn add_table_of_contents(mut self, t: TableOfContents) -> Self {
152 self.children
153 .push(DocumentChild::TableOfContents(Box::new(t)));
154 self
155 }
156
157 pub fn columns(mut self, col: usize) -> Self {
158 self.section_property.columns = col;
159 self
160 }
161
162 pub fn text_direction(mut self, direction: String) -> Self {
163 self.section_property.text_direction = direction;
164 self
165 }
166}