1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
mod build_xml;
mod comments;
mod content_types;
mod doc_props;
mod document;
mod document_rels;
mod elements;
mod font_table;
mod history_id;
mod numberings;
mod rels;
mod settings;
mod styles;
mod xml_docx;
pub(crate) use build_xml::BuildXML;
pub(crate) use history_id::HistoryId;
pub use comments::*;
pub use content_types::*;
pub use doc_props::*;
pub use document::*;
pub use document_rels::*;
pub use elements::*;
pub use font_table::*;
pub use numberings::*;
pub use rels::*;
pub use settings::*;
pub use styles::*;
pub use xml_docx::*;
use serde::Serialize;
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Docx {
pub content_type: ContentTypes,
pub rels: Rels,
pub document_rels: DocumentRels,
pub doc_props: DocProps,
pub styles: Styles,
pub document: Document,
pub comments: Comments,
pub numberings: Numberings,
pub settings: Settings,
pub font_table: FontTable,
}
impl Default for Docx {
fn default() -> Self {
let content_type = ContentTypes::new().set_default();
let rels = Rels::new().set_default();
let doc_props = DocProps::new(CorePropsConfig::new());
let styles = Styles::new();
let document = Document::new();
let document_rels = DocumentRels::new();
let settings = Settings::new();
let font_table = FontTable::new();
let comments = Comments::new();
let numberings = Numberings::new();
Docx {
content_type,
rels,
doc_props,
styles,
document,
comments,
document_rels,
settings,
font_table,
numberings,
}
}
}
impl Docx {
pub fn new() -> Docx {
Default::default()
}
pub fn document(mut self, d: Document) -> Docx {
for child in &self.document.children {
match child {
DocumentChild::Paragraph(paragraph) => {
if paragraph.has_numbering {
self.document_rels.has_numberings = true;
}
}
DocumentChild::Table(table) => {
if table.has_numbering {
self.document_rels.has_numberings = true;
}
}
}
}
self.document = d;
self
}
pub fn styles(mut self, s: Styles) -> Self {
self.styles = s;
self
}
pub fn numberings(mut self, n: Numberings) -> Self {
self.numberings = n;
self
}
pub fn add_paragraph(mut self, p: Paragraph) -> Docx {
if p.has_numbering {
self.document_rels.has_numberings = true;
}
self.document = self.document.add_paragraph(p);
self
}
pub fn add_table(mut self, t: Table) -> Docx {
if t.has_numbering {
self.document_rels.has_numberings = true;
}
self.document = self.document.add_table(t);
self
}
pub fn add_abstract_numbering(mut self, num: AbstractNumbering) -> Docx {
self.numberings = self.numberings.add_abstract_numbering(num);
self
}
pub fn add_numbering(mut self, num: Numbering) -> Docx {
self.numberings = self.numberings.add_numbering(num);
self
}
pub fn created_at(mut self, date: &str) -> Self {
self.doc_props = self.doc_props.created_at(date);
self
}
pub fn updated_at(mut self, date: &str) -> Self {
self.doc_props = self.doc_props.updated_at(date);
self
}
pub fn build(&mut self) -> XMLDocx {
self.update_comments();
XMLDocx {
content_type: self.content_type.build(),
rels: self.rels.build(),
doc_props: self.doc_props.build(),
styles: self.styles.build(),
document: self.document.build(),
comments: self.comments.build(),
document_rels: self.document_rels.build(),
settings: self.settings.build(),
font_table: self.font_table.build(),
numberings: self.numberings.build(),
}
}
pub fn json(&mut self) -> String {
self.update_comments();
serde_json::to_string_pretty(&self).unwrap()
}
fn update_comments(&mut self) {
let mut comments: Vec<Comment> = vec![];
for child in &self.document.children {
match child {
DocumentChild::Paragraph(paragraph) => {
for child in ¶graph.children {
if let ParagraphChild::CommentStart(c) = child {
comments.push(c.comment());
}
}
}
DocumentChild::Table(table) => {
for row in &table.rows {
for cell in &row.cells {
for content in &cell.children {
match content {
TableCellContent::Paragraph(paragraph) => {
for child in ¶graph.children {
if let ParagraphChild::CommentStart(c) = child {
comments.push(c.comment());
}
}
}
}
}
}
}
}
}
}
if !comments.is_empty() {
self.document_rels.has_comments = true;
}
self.comments.add_comments(comments);
}
}