docx_reader/documents/elements/
table_cell_borders.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
use serde::Serialize;

use crate::types::*;

/*
	Please see. L.4.3.2.18 Cell Border Properties

	left – left border
	right – right border
	top – top border
	bottom – bottom border
	insideH – inner horizontal borders
	insideV – inner vertical borders
	tl2br – diagonal border from top left corner to bottom right corner
	tr2bl – diagonal border from top right corner to bottom left corner
*/
#[derive(Serialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TableCellBorder {
	pub border_type: BorderType,
	pub size: usize,
	pub color: String,
	position: TableCellBorderPosition,
	space: usize,
}

impl TableCellBorder {
	pub fn new(position: TableCellBorderPosition) -> TableCellBorder {
		TableCellBorder {
			position,
			border_type: BorderType::Single,
			size: 2,
			space: 0,
			color: "000000".to_owned(),
		}
	}

	pub fn color(mut self, color: impl Into<String>) -> TableCellBorder {
		self.color = color.into();
		self
	}

	pub fn size(mut self, size: usize) -> TableCellBorder {
		self.size = size;
		self
	}

	pub fn border_type(mut self, border_type: BorderType) -> TableCellBorder {
		self.border_type = border_type;
		self
	}

	pub fn get_size(&self) -> usize {
		self.size
	}

	pub fn get_color(&self) -> String {
		self.color.clone()
	}

	pub fn get_border_type(&self) -> BorderType {
		self.border_type
	}
}

#[derive(Serialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TableCellBorders {
	top: Option<TableCellBorder>,
	left: Option<TableCellBorder>,
	bottom: Option<TableCellBorder>,
	right: Option<TableCellBorder>,
	inside_h: Option<TableCellBorder>,
	inside_v: Option<TableCellBorder>,
	tr2bl: Option<TableCellBorder>,
	tl2br: Option<TableCellBorder>,
}

impl Default for TableCellBorders {
	fn default() -> TableCellBorders {
		TableCellBorders {
			top: Some(TableCellBorder::new(TableCellBorderPosition::Top)),
			left: Some(TableCellBorder::new(TableCellBorderPosition::Left)),
			bottom: Some(TableCellBorder::new(TableCellBorderPosition::Bottom)),
			right: Some(TableCellBorder::new(TableCellBorderPosition::Right)),
			inside_h: Some(TableCellBorder::new(TableCellBorderPosition::InsideH)),
			inside_v: Some(TableCellBorder::new(TableCellBorderPosition::InsideV)),
			tr2bl: None,
			tl2br: None,
		}
	}
}

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

	pub fn with_empty() -> TableCellBorders {
		TableCellBorders {
			top: None,
			left: None,
			bottom: None,
			right: None,
			inside_h: None,
			inside_v: None,
			tr2bl: None,
			tl2br: None,
		}
	}

	pub fn set(mut self, border: TableCellBorder) -> Self {
		match border.position {
			TableCellBorderPosition::Top => self.top = Some(border),
			TableCellBorderPosition::Left => self.left = Some(border),
			TableCellBorderPosition::Bottom => self.bottom = Some(border),
			TableCellBorderPosition::Right => self.right = Some(border),
			TableCellBorderPosition::InsideH => self.inside_h = Some(border),
			TableCellBorderPosition::InsideV => self.inside_v = Some(border),
			TableCellBorderPosition::Tr2bl => self.tr2bl = Some(border),
			TableCellBorderPosition::Tl2br => self.tl2br = Some(border),
		};
		self
	}

	pub fn clear(mut self, position: TableCellBorderPosition) -> Self {
		let nil = TableCellBorder::new(position.clone()).border_type(BorderType::Nil);
		match position {
			TableCellBorderPosition::Top => self.top = Some(nil),
			TableCellBorderPosition::Left => self.left = Some(nil),
			TableCellBorderPosition::Bottom => self.bottom = Some(nil),
			TableCellBorderPosition::Right => self.right = Some(nil),
			TableCellBorderPosition::InsideH => self.inside_h = Some(nil),
			TableCellBorderPosition::InsideV => self.inside_v = Some(nil),
			TableCellBorderPosition::Tr2bl => self.tr2bl = Some(nil),
			TableCellBorderPosition::Tl2br => self.tl2br = Some(nil),
		};
		self
	}

	pub fn clear_all(mut self) -> Self {
		self.top =
			Some(TableCellBorder::new(TableCellBorderPosition::Top).border_type(BorderType::Nil));
		self.left =
			Some(TableCellBorder::new(TableCellBorderPosition::Left).border_type(BorderType::Nil));
		self.bottom = Some(
			TableCellBorder::new(TableCellBorderPosition::Bottom).border_type(BorderType::Nil),
		);
		self.right =
			Some(TableCellBorder::new(TableCellBorderPosition::Right).border_type(BorderType::Nil));
		self.inside_h = Some(
			TableCellBorder::new(TableCellBorderPosition::InsideH).border_type(BorderType::Nil),
		);
		self.inside_v = Some(
			TableCellBorder::new(TableCellBorderPosition::InsideV).border_type(BorderType::Nil),
		);
		self.tl2br =
			Some(TableCellBorder::new(TableCellBorderPosition::Tl2br).border_type(BorderType::Nil));
		self.tr2bl =
			Some(TableCellBorder::new(TableCellBorderPosition::Tr2bl).border_type(BorderType::Nil));
		self
	}
}