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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use serde::Serialize;
use crate::documents::BuildXML;
use crate::types::*;
use crate::xml_builder::*;
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TableBorder {
position: BorderPosition,
border_type: BorderType,
size: usize,
space: usize,
color: String,
}
impl TableBorder {
pub fn new(position: BorderPosition) -> 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
}
}
impl BuildXML for TableBorder {
fn build(&self) -> Vec<u8> {
let base = XMLBuilder::new();
let base = match self.position {
BorderPosition::Top => {
base.border_top(self.border_type, self.size, self.space, &self.color)
}
BorderPosition::Left => {
base.border_left(self.border_type, self.size, self.space, &self.color)
}
BorderPosition::Bottom => {
base.border_bottom(self.border_type, self.size, self.space, &self.color)
}
BorderPosition::Right => {
base.border_right(self.border_type, self.size, self.space, &self.color)
}
BorderPosition::InsideH => {
base.border_inside_h(self.border_type, self.size, self.space, &self.color)
}
BorderPosition::InsideV => {
base.border_inside_v(self.border_type, self.size, self.space, &self.color)
}
};
base.build()
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[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(BorderPosition::Top)),
left: Some(TableBorder::new(BorderPosition::Left)),
bottom: Some(TableBorder::new(BorderPosition::Bottom)),
right: Some(TableBorder::new(BorderPosition::Right)),
inside_h: Some(TableBorder::new(BorderPosition::InsideH)),
inside_v: Some(TableBorder::new(BorderPosition::InsideV)),
}
}
}
impl TableBorders {
pub fn new() -> TableBorders {
Default::default()
}
pub fn set_border(mut self, border: TableBorder) -> Self {
match border.position {
BorderPosition::Top => self.top = Some(border),
BorderPosition::Left => self.left = Some(border),
BorderPosition::Bottom => self.bottom = Some(border),
BorderPosition::Right => self.right = Some(border),
BorderPosition::InsideH => self.inside_h = Some(border),
BorderPosition::InsideV => self.inside_v = Some(border),
};
self
}
pub fn clear_border(mut self, position: BorderPosition) -> Self {
match position {
BorderPosition::Top => self.top = None,
BorderPosition::Left => self.left = None,
BorderPosition::Bottom => self.bottom = None,
BorderPosition::Right => self.right = None,
BorderPosition::InsideH => self.inside_h = None,
BorderPosition::InsideV => self.inside_v = None,
};
self
}
}
impl BuildXML for TableBorders {
fn build(&self) -> Vec<u8> {
XMLBuilder::new()
.open_table_borders()
.add_optional_child(&self.top)
.add_optional_child(&self.left)
.add_optional_child(&self.bottom)
.add_optional_child(&self.right)
.add_optional_child(&self.inside_h)
.add_optional_child(&self.inside_v)
.close()
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_table_borders() {
let b = TableBorders::new().build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:tblBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:left w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:right w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:insideV w:val="single" w:sz="2" w:space="0" w:color="000000" /></w:tblBorders>"#
);
}
#[test]
fn test_table_borders_set() {
let b = TableBorders::new()
.set_border(TableBorder::new(BorderPosition::Left).color("AAAAAA"))
.clear_border(BorderPosition::Top)
.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:tblBorders><w:left w:val="single" w:sz="2" w:space="0" w:color="AAAAAA" /><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:right w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000" /><w:insideV w:val="single" w:sz="2" w:space="0" w:color="000000" /></w:tblBorders>"#
);
}
}