docx_reader/documents/
document.rs

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
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;

use super::*;

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Document {
	pub children: Vec<DocumentChild>,
	pub section_property: SectionProperty,
	pub has_numbering: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub enum DocumentChild {
	Paragraph(Box<Paragraph>),
	Table(Box<Table>),
	StructuredDataTag(Box<StructuredDataTag>),
	TableOfContents(Box<TableOfContents>),
}

impl Serialize for DocumentChild {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: Serializer,
	{
		match *self {
			DocumentChild::Paragraph(ref p) => {
				let mut t = serializer.serialize_struct("Paragraph", 2)?;
				t.serialize_field("type", "paragraph")?;
				t.serialize_field("data", p)?;
				t.end()
			}
			DocumentChild::Table(ref c) => {
				let mut t = serializer.serialize_struct("Table", 2)?;
				t.serialize_field("type", "table")?;
				t.serialize_field("data", c)?;
				t.end()
			}
			DocumentChild::StructuredDataTag(ref r) => {
				let mut t = serializer.serialize_struct("StructuredDataTag", 2)?;
				t.serialize_field("type", "structuredDataTag")?;
				t.serialize_field("data", r)?;
				t.end()
			}
			DocumentChild::TableOfContents(ref r) => {
				let mut t = serializer.serialize_struct("TableOfContents", 2)?;
				t.serialize_field("type", "tableOfContents")?;
				t.serialize_field("data", r)?;
				t.end()
			}
		}
	}
}

impl Default for Document {
	fn default() -> Self {
		Self {
			children: Vec::new(),
			section_property: SectionProperty::new(),
			has_numbering: false,
		}
	}
}

impl Document {
	pub fn new() -> Document {
		Default::default()
	}

	pub fn add_paragraph(mut self, p: Paragraph) -> Self {
		if p.has_numbering {
			self.has_numbering = true
		}
		self.children.push(DocumentChild::Paragraph(Box::new(p)));
		self
	}

	pub fn add_table(mut self, t: Table) -> Self {
		if t.has_numbering {
			self.has_numbering = true
		}
		self.children.push(DocumentChild::Table(Box::new(t)));
		self
	}

	pub fn page_size(mut self, size: PageSize) -> Self {
		self.section_property = self.section_property.page_size(size);
		self
	}

	pub fn page_margin(mut self, margin: crate::types::PageMargin) -> Self {
		self.section_property = self.section_property.page_margin(margin);
		self
	}

	pub fn page_orient(mut self, o: crate::types::PageOrientationType) -> Self {
		self.section_property = self.section_property.page_orient(o);
		self
	}

	pub fn doc_grid(mut self, doc_grid: DocGrid) -> Self {
		self.section_property = self.section_property.doc_grid(doc_grid);
		self
	}

	pub fn default_section_property(mut self, property: SectionProperty) -> Self {
		self.section_property = property;
		self
	}

	pub fn header(mut self, h: Header, rid: &str) -> Self {
		self.section_property = self.section_property.header(h, rid);
		self
	}

	pub fn first_header(mut self, h: Header, rid: &str) -> Self {
		self.section_property = self.section_property.first_header(h, rid);
		self
	}

	pub fn even_header(mut self, h: Header, rid: &str) -> Self {
		self.section_property = self.section_property.even_header(h, rid);
		self
	}

	pub fn footer(mut self, h: Footer, rid: &str) -> Self {
		self.section_property = self.section_property.footer(h, rid);
		self
	}

	pub fn first_footer(mut self, h: Footer, rid: &str) -> Self {
		self.section_property = self.section_property.first_footer(h, rid);
		self
	}

	pub fn even_footer(mut self, h: Footer, rid: &str) -> Self {
		self.section_property = self.section_property.even_footer(h, rid);
		self
	}

	pub fn add_structured_data_tag(mut self, t: StructuredDataTag) -> Self {
		if t.has_numbering {
			self.has_numbering = true
		}
		self.children
			.push(DocumentChild::StructuredDataTag(Box::new(t)));
		self
	}

	pub fn add_table_of_contents(mut self, t: TableOfContents) -> Self {
		self.children
			.push(DocumentChild::TableOfContents(Box::new(t)));
		self
	}

	pub fn columns(mut self, col: usize) -> Self {
		self.section_property.columns = col;
		self
	}

	pub fn text_direction(mut self, direction: String) -> Self {
		self.section_property.text_direction = direction;
		self
	}
}