docx_reader/documents/elements/
table_cell.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
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;

use super::*;
use crate::types::*;

#[derive(Serialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TableCell {
	pub children: Vec<TableCellContent>,
	pub property: TableCellProperty,
	pub has_numbering: bool,
}

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

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

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

	pub fn add_paragraph(mut self, p: Paragraph) -> TableCell {
		if p.has_numbering {
			self.has_numbering = true
		}
		self.children.push(TableCellContent::Paragraph(p));
		self
	}

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

	pub fn add_structured_data_tag(mut self, t: StructuredDataTag) -> Self {
		self.children
			.push(TableCellContent::StructuredDataTag(Box::new(t)));
		self
	}

	pub fn add_table(mut self, t: Table) -> TableCell {
		if t.has_numbering {
			self.has_numbering = true
		}
		self.children.push(TableCellContent::Table(t));
		self
	}

	pub fn vertical_merge(mut self, t: VMergeType) -> TableCell {
		self.property = self.property.vertical_merge(t);
		self
	}

	pub fn shading(mut self, s: Shading) -> TableCell {
		self.property = self.property.shading(s);
		self
	}

	pub fn vertical_align(mut self, t: VAlignType) -> TableCell {
		self.property = self.property.vertical_align(t);
		self
	}

	pub fn text_direction(mut self, t: TextDirectionType) -> TableCell {
		self.property = self.property.text_direction(t);
		self
	}

	pub fn grid_span(mut self, v: usize) -> TableCell {
		self.property = self.property.grid_span(v);
		self
	}

	pub fn width(mut self, v: usize, t: WidthType) -> TableCell {
		self.property = self.property.width(v, t);
		self
	}

	pub fn set_border(mut self, border: TableCellBorder) -> Self {
		self.property = self.property.set_border(border);
		self
	}

	pub fn set_borders(mut self, borders: TableCellBorders) -> Self {
		self.property = self.property.set_borders(borders);
		self
	}

	pub fn clear_border(mut self, position: TableCellBorderPosition) -> Self {
		self.property = self.property.clear_border(position);
		self
	}

	pub fn clear_all_border(mut self) -> Self {
		self.property = self.property.clear_all_border();
		self
	}
}

impl Default for TableCell {
	fn default() -> Self {
		let property = TableCellProperty::new();
		let children = vec![];
		Self {
			property,
			children,
			has_numbering: false,
		}
	}
}