docx_reader/documents/elements/
table.rs

1use serde::ser::{SerializeStruct, Serializer};
2use serde::Serialize;
3
4use super::*;
5use crate::types::*;
6
7#[derive(Debug, Clone, PartialEq, Serialize)]
8#[serde(rename_all = "camelCase")]
9pub struct Table {
10	pub rows: Vec<TableChild>,
11	pub grid: Vec<usize>,
12	pub has_numbering: bool,
13	pub property: TableProperty,
14}
15
16#[derive(Debug, Clone, PartialEq)]
17pub enum TableChild {
18	TableRow(TableRow),
19}
20
21impl Table {
22	pub fn new(rows: Vec<TableRow>) -> Table {
23		let property = TableProperty::new();
24		let has_numbering = rows.iter().any(|c| c.has_numbering);
25		let grid = vec![];
26		let rows = rows.into_iter().map(TableChild::TableRow).collect();
27		Self {
28			property,
29			rows,
30			grid,
31			has_numbering,
32		}
33	}
34
35	pub fn without_borders(rows: Vec<TableRow>) -> Table {
36		let property = TableProperty::without_borders();
37		let has_numbering = rows.iter().any(|c| c.has_numbering);
38		let grid = vec![];
39		let rows = rows.into_iter().map(TableChild::TableRow).collect();
40		Self {
41			property,
42			rows,
43			grid,
44			has_numbering,
45		}
46	}
47
48	pub fn add_row(mut self, row: TableRow) -> Table {
49		self.rows.push(TableChild::TableRow(row));
50		self
51	}
52
53	pub fn set_grid(mut self, grid: Vec<usize>) -> Table {
54		self.grid = grid;
55		self
56	}
57
58	pub fn indent(mut self, v: i32) -> Table {
59		self.property = self.property.indent(v);
60		self
61	}
62
63	pub fn align(mut self, v: TableAlignmentType) -> Table {
64		self.property = self.property.align(v);
65		self
66	}
67
68	pub fn style(mut self, s: impl Into<String>) -> Table {
69		self.property = self.property.style(s);
70		self
71	}
72
73	pub fn layout(mut self, t: TableLayoutType) -> Table {
74		self.property = self.property.layout(t);
75		self
76	}
77
78	pub fn width(mut self, w: usize, t: WidthType) -> Table {
79		self.property = self.property.width(w, t);
80		self
81	}
82
83	pub fn margins(mut self, margins: TableCellMargins) -> Self {
84		self.property = self.property.set_margins(margins);
85		self
86	}
87
88	pub fn set_borders(mut self, borders: TableBorders) -> Self {
89		self.property = self.property.set_borders(borders);
90		self
91	}
92
93	pub fn set_border(mut self, border: TableBorder) -> Self {
94		self.property = self.property.set_border(border);
95		self
96	}
97
98	pub fn clear_border(mut self, position: TableBorderPosition) -> Self {
99		self.property = self.property.clear_border(position);
100		self
101	}
102
103	pub fn clear_all_border(mut self) -> Self {
104		self.property = self.property.clear_all_border();
105		self
106	}
107}
108
109impl Serialize for TableChild {
110	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
111	where
112		S: Serializer,
113	{
114		match *self {
115			TableChild::TableRow(ref r) => {
116				let mut t = serializer.serialize_struct("TableRow", 2)?;
117				t.serialize_field("type", "tableRow")?;
118				t.serialize_field("data", r)?;
119				t.end()
120			}
121		}
122	}
123}