docx_reader/documents/elements/
table_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
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 TableBorder {
	pub border_type: BorderType,
	pub size: usize,
	pub color: String,
	position: TableBorderPosition,
	space: usize,
}

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

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

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

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

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

impl Default for TableBorders {
	fn default() -> TableBorders {
		TableBorders {
			top: Some(TableBorder::new(TableBorderPosition::Top)),
			left: Some(TableBorder::new(TableBorderPosition::Left)),
			bottom: Some(TableBorder::new(TableBorderPosition::Bottom)),
			right: Some(TableBorder::new(TableBorderPosition::Right)),
			inside_h: Some(TableBorder::new(TableBorderPosition::InsideH)),
			inside_v: Some(TableBorder::new(TableBorderPosition::InsideV)),
		}
	}
}

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

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

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

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

	pub fn clear_all(mut self) -> Self {
		self.top = Some(TableBorder::new(TableBorderPosition::Top).border_type(BorderType::Nil));
		self.left = Some(TableBorder::new(TableBorderPosition::Left).border_type(BorderType::Nil));
		self.bottom =
			Some(TableBorder::new(TableBorderPosition::Bottom).border_type(BorderType::Nil));
		self.right =
			Some(TableBorder::new(TableBorderPosition::Right).border_type(BorderType::Nil));
		self.inside_h =
			Some(TableBorder::new(TableBorderPosition::InsideH).border_type(BorderType::Nil));
		self.inside_v =
			Some(TableBorder::new(TableBorderPosition::InsideV).border_type(BorderType::Nil));
		self
	}
}