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

use super::{Delete, Insert, TableCell, TableRowProperty};
use crate::HeightRule;

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TableRow {
	pub cells: Vec<TableRowChild>,
	pub has_numbering: bool,
	pub property: TableRowProperty,
}

#[derive(Debug, Clone, PartialEq)]
pub enum TableRowChild {
	TableCell(TableCell),
}

impl TableRow {
	pub fn new(cells: Vec<TableCell>) -> TableRow {
		let property = TableRowProperty::new();
		let has_numbering = cells.iter().any(|c| c.has_numbering);
		let cells = cells.into_iter().map(TableRowChild::TableCell).collect();
		Self {
			cells,
			property,
			has_numbering,
		}
	}

	pub fn grid_after(mut self, grid_after: u32) -> TableRow {
		self.property = self.property.grid_after(grid_after);
		self
	}

	pub fn width_after(mut self, w: f32) -> TableRow {
		self.property = self.property.width_after(w);
		self
	}

	pub fn grid_before(mut self, grid_before: u32) -> TableRow {
		self.property = self.property.grid_before(grid_before);
		self
	}

	pub fn width_before(mut self, w: f32) -> TableRow {
		self.property = self.property.width_before(w);
		self
	}

	pub fn row_height(mut self, h: f32) -> TableRow {
		self.property = self.property.row_height(h);
		self
	}

	pub fn height_rule(mut self, r: HeightRule) -> TableRow {
		self.property = self.property.height_rule(r);
		self
	}

	pub fn delete(mut self, d: Delete) -> TableRow {
		self.property = self.property.delete(d);
		self
	}

	pub fn insert(mut self, i: Insert) -> TableRow {
		self.property = self.property.insert(i);
		self
	}
}

impl Serialize for TableRowChild {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: Serializer,
	{
		match *self {
			TableRowChild::TableCell(ref r) => {
				let mut t = serializer.serialize_struct("TableCell", 2)?;
				t.serialize_field("type", "tableCell")?;
				t.serialize_field("data", r)?;
				t.end()
			}
		}
	}
}