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

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

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Table {
	pub rows: Vec<TableChild>,
	pub grid: Vec<usize>,
	pub has_numbering: bool,
	pub property: TableProperty,
}

#[derive(Debug, Clone, PartialEq)]
pub enum TableChild {
	TableRow(TableRow),
}

impl Table {
	pub fn new(rows: Vec<TableRow>) -> Table {
		let property = TableProperty::new();
		let has_numbering = rows.iter().any(|c| c.has_numbering);
		let grid = vec![];
		let rows = rows.into_iter().map(TableChild::TableRow).collect();
		Self {
			property,
			rows,
			grid,
			has_numbering,
		}
	}

	pub fn without_borders(rows: Vec<TableRow>) -> Table {
		let property = TableProperty::without_borders();
		let has_numbering = rows.iter().any(|c| c.has_numbering);
		let grid = vec![];
		let rows = rows.into_iter().map(TableChild::TableRow).collect();
		Self {
			property,
			rows,
			grid,
			has_numbering,
		}
	}

	pub fn add_row(mut self, row: TableRow) -> Table {
		self.rows.push(TableChild::TableRow(row));
		self
	}

	pub fn set_grid(mut self, grid: Vec<usize>) -> Table {
		self.grid = grid;
		self
	}

	pub fn indent(mut self, v: i32) -> Table {
		self.property = self.property.indent(v);
		self
	}

	pub fn align(mut self, v: TableAlignmentType) -> Table {
		self.property = self.property.align(v);
		self
	}

	pub fn style(mut self, s: impl Into<String>) -> Table {
		self.property = self.property.style(s);
		self
	}

	pub fn layout(mut self, t: TableLayoutType) -> Table {
		self.property = self.property.layout(t);
		self
	}

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

	pub fn margins(mut self, margins: TableCellMargins) -> Self {
		self.property = self.property.set_margins(margins);
		self
	}

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

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

	pub fn clear_border(mut self, position: TableBorderPosition) -> 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 Serialize for TableChild {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: Serializer,
	{
		match *self {
			TableChild::TableRow(ref r) => {
				let mut t = serializer.serialize_struct("TableRow", 2)?;
				t.serialize_field("type", "tableRow")?;
				t.serialize_field("data", r)?;
				t.end()
			}
		}
	}
}