docx_rs/documents/elements/
table_property.rs1use serde::Serialize;
2use std::io::Write;
3#[cfg(feature = "wasm")]
4use wasm_bindgen::prelude::*;
5
6use super::*;
7use crate::documents::BuildXML;
8use crate::types::*;
9use crate::xml_builder::*;
10
11#[cfg_attr(feature = "wasm", wasm_bindgen)]
12#[derive(Debug, Clone, PartialEq, Serialize)]
13#[serde(rename_all = "camelCase")]
14pub struct TableProperty {
15 width: TableWidth,
16 justification: Justification,
17 borders: TableBorders,
18 #[serde(skip_serializing_if = "Option::is_none")]
19 margins: Option<TableCellMargins>,
20 #[serde(skip_serializing_if = "Option::is_none")]
21 indent: Option<TableIndent>,
22 #[serde(skip_serializing_if = "Option::is_none")]
23 style: Option<TableStyle>,
24 #[serde(skip_serializing_if = "Option::is_none")]
25 layout: Option<TableLayout>,
26 #[serde(skip_serializing_if = "Option::is_none")]
27 position: Option<TablePositionProperty>,
28}
29
30impl Default for TableProperty {
31 fn default() -> Self {
32 TableProperty {
33 width: TableWidth::new(0, WidthType::Auto),
34 justification: Justification::new("left"),
35 borders: TableBorders::new(),
36 margins: None,
37 indent: None,
38 style: None,
39 layout: None,
40 position: None,
41 }
42 }
43}
44
45impl TableProperty {
46 pub fn new() -> TableProperty {
47 Default::default()
48 }
49
50 pub fn without_borders() -> TableProperty {
51 TableProperty {
52 borders: TableBorders::with_empty(),
53 ..Default::default()
54 }
55 }
56
57 pub fn indent(mut self, v: i32) -> TableProperty {
58 self.indent = Some(TableIndent::new(v, WidthType::Dxa));
59 self
60 }
61
62 pub fn width(mut self, v: usize, t: WidthType) -> TableProperty {
63 self.width = TableWidth::new(v, t);
64 self
65 }
66
67 pub fn align(mut self, v: TableAlignmentType) -> TableProperty {
68 self.justification = Justification::new(v.to_string());
69 self
70 }
71
72 pub fn set_margins(mut self, margins: TableCellMargins) -> Self {
73 self.margins = Some(margins);
74 self
75 }
76
77 pub fn cell_margin_top(mut self, v: usize, t: WidthType) -> Self {
78 if let Some(margins) = self.margins {
79 self.margins = Some(margins.margin_top(v, t));
80 } else {
81 let margins = TableCellMargins::new();
82 self.margins = Some(margins.margin_top(v, t));
83 }
84 self
85 }
86
87 pub fn cell_margin_right(mut self, v: usize, t: WidthType) -> Self {
88 if let Some(margins) = self.margins {
89 self.margins = Some(margins.margin_right(v, t));
90 } else {
91 let margins = TableCellMargins::new();
92 self.margins = Some(margins.margin_right(v, t));
93 }
94 self
95 }
96
97 pub fn cell_margin_bottom(mut self, v: usize, t: WidthType) -> Self {
98 if let Some(margins) = self.margins {
99 self.margins = Some(margins.margin_bottom(v, t));
100 } else {
101 let margins = TableCellMargins::new();
102 self.margins = Some(margins.margin_bottom(v, t));
103 }
104 self
105 }
106
107 pub fn cell_margin_left(mut self, v: usize, t: WidthType) -> Self {
108 if let Some(margins) = self.margins {
109 self.margins = Some(margins.margin_left(v, t));
110 } else {
111 let margins = TableCellMargins::new();
112 self.margins = Some(margins.margin_left(v, t));
113 }
114 self
115 }
116
117 pub fn set_borders(mut self, borders: TableBorders) -> Self {
118 self.borders = borders;
119 self
120 }
121
122 pub fn set_border(mut self, border: TableBorder) -> Self {
123 self.borders = self.borders.set(border);
124 self
125 }
126
127 pub fn clear_border(mut self, position: TableBorderPosition) -> Self {
128 self.borders = self.borders.clear(position);
129 self
130 }
131
132 pub fn clear_all_border(mut self) -> Self {
133 self.borders = self.borders.clear_all();
134 self
135 }
136
137 pub fn style(mut self, s: impl Into<String>) -> Self {
138 self.style = Some(TableStyle::new(s));
139 self
140 }
141
142 pub fn layout(mut self, t: TableLayoutType) -> Self {
143 self.layout = Some(TableLayout::new(t));
144 self
145 }
146
147 pub fn position(mut self, p: TablePositionProperty) -> Self {
148 self.position = Some(p);
149 self
150 }
151}
152
153impl BuildXML for TableProperty {
154 fn build_to<W: Write>(
155 &self,
156 stream: xml::writer::EventWriter<W>,
157 ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
158 XMLBuilder::from(stream)
159 .open_table_property()?
160 .add_child(&self.width)?
161 .add_child(&self.justification)?
162 .add_child(&self.borders)?
163 .add_optional_child(&self.margins)?
164 .add_optional_child(&self.indent)?
165 .add_optional_child(&self.style)?
166 .add_optional_child(&self.layout)?
167 .add_optional_child(&self.position)?
168 .close()?
169 .into_inner()
170 }
171}
172
173#[cfg(test)]
174mod tests {
175
176 use super::*;
177 #[cfg(test)]
178 use pretty_assertions::assert_eq;
179 use std::str;
180
181 #[test]
182 fn test_default() {
183 let c = TableProperty::new();
184 let b = c.build();
185 assert_eq!(
186 str::from_utf8(&b).unwrap(),
187 r#"<w:tblPr><w:tblW w:w="0" w:type="auto" /><w:jc w:val="left" /><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></w:tblPr>"#
188 );
189 }
190
191 #[test]
192 fn test_table_property_json() {
193 let p = TableProperty::new().indent(100);
194 assert_eq!(
195 serde_json::to_string(&p).unwrap(),
196 r#"{"width":{"width":0,"widthType":"auto"},"justification":"left","borders":{"top":{"borderType":"single","size":2,"color":"000000","position":"top","space":0},"left":{"borderType":"single","size":2,"color":"000000","position":"left","space":0},"bottom":{"borderType":"single","size":2,"color":"000000","position":"bottom","space":0},"right":{"borderType":"single","size":2,"color":"000000","position":"right","space":0},"insideH":{"borderType":"single","size":2,"color":"000000","position":"insideH","space":0},"insideV":{"borderType":"single","size":2,"color":"000000","position":"insideV","space":0}},"indent":{"width":100,"widthType":"dxa"}}"#
197 );
198 }
199}