docx_rs/documents/elements/
table.rs1use serde::ser::{SerializeStruct, Serializer};
2use serde::Serialize;
3use std::io::Write;
4
5use super::*;
6use crate::documents::BuildXML;
7use crate::types::*;
8use crate::xml_builder::*;
9
10#[derive(Debug, Clone, PartialEq, Serialize)]
11#[serde(rename_all = "camelCase")]
12pub struct Table {
13 pub rows: Vec<TableChild>,
14 pub grid: Vec<usize>,
15 pub has_numbering: bool,
16 pub property: TableProperty,
17}
18
19#[derive(Debug, Clone, PartialEq)]
20pub enum TableChild {
21 TableRow(TableRow),
22}
23
24impl BuildXML for TableChild {
25 fn build_to<W: Write>(
26 &self,
27 stream: xml::writer::EventWriter<W>,
28 ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
29 match self {
30 TableChild::TableRow(v) => v.build_to(stream),
31 }
32 }
33}
34
35impl Table {
36 pub fn new(rows: Vec<TableRow>) -> Table {
37 let property = TableProperty::new();
38 let has_numbering = rows.iter().any(|c| c.has_numbering);
39 let grid = vec![];
40 let rows = rows.into_iter().map(TableChild::TableRow).collect();
41 Self {
42 property,
43 rows,
44 grid,
45 has_numbering,
46 }
47 }
48
49 pub fn without_borders(rows: Vec<TableRow>) -> Table {
50 let property = TableProperty::without_borders();
51 let has_numbering = rows.iter().any(|c| c.has_numbering);
52 let grid = vec![];
53 let rows = rows.into_iter().map(TableChild::TableRow).collect();
54 Self {
55 property,
56 rows,
57 grid,
58 has_numbering,
59 }
60 }
61
62 pub fn add_row(mut self, row: TableRow) -> Table {
63 self.rows.push(TableChild::TableRow(row));
64 self
65 }
66
67 pub fn set_grid(mut self, grid: Vec<usize>) -> Table {
68 self.grid = grid;
69 self
70 }
71
72 pub fn indent(mut self, v: i32) -> Table {
73 self.property = self.property.indent(v);
74 self
75 }
76
77 pub fn align(mut self, v: TableAlignmentType) -> Table {
78 self.property = self.property.align(v);
79 self
80 }
81
82 pub fn style(mut self, s: impl Into<String>) -> Table {
83 self.property = self.property.style(s);
84 self
85 }
86
87 pub fn layout(mut self, t: TableLayoutType) -> Table {
88 self.property = self.property.layout(t);
89 self
90 }
91
92 pub fn position(mut self, p: TablePositionProperty) -> Self {
93 self.property = self.property.position(p);
94 self
95 }
96
97 pub fn width(mut self, w: usize, t: WidthType) -> Table {
98 self.property = self.property.width(w, t);
99 self
100 }
101
102 pub fn margins(mut self, margins: TableCellMargins) -> Self {
103 self.property = self.property.set_margins(margins);
104 self
105 }
106
107 pub fn set_borders(mut self, borders: TableBorders) -> Self {
108 self.property = self.property.set_borders(borders);
109 self
110 }
111
112 pub fn set_border(mut self, border: TableBorder) -> Self {
113 self.property = self.property.set_border(border);
114 self
115 }
116
117 pub fn clear_border(mut self, position: TableBorderPosition) -> Self {
118 self.property = self.property.clear_border(position);
119 self
120 }
121
122 pub fn clear_all_border(mut self) -> Self {
123 self.property = self.property.clear_all_border();
124 self
125 }
126}
127
128impl BuildXML for Table {
129 fn build_to<W: Write>(
130 &self,
131 stream: xml::writer::EventWriter<W>,
132 ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
133 let grid = TableGrid::new(self.grid.clone());
134 XMLBuilder::from(stream)
135 .open_table()?
136 .add_child(&self.property)?
137 .add_child(&grid)?
138 .add_children(&self.rows)?
139 .close()?
140 .into_inner()
141 }
142}
143
144impl Serialize for TableChild {
145 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
146 where
147 S: Serializer,
148 {
149 match *self {
150 TableChild::TableRow(ref r) => {
151 let mut t = serializer.serialize_struct("TableRow", 2)?;
152 t.serialize_field("type", "tableRow")?;
153 t.serialize_field("data", r)?;
154 t.end()
155 }
156 }
157 }
158}
159
160#[cfg(test)]
161mod tests {
162
163 use super::*;
164 #[cfg(test)]
165 use pretty_assertions::assert_eq;
166 use std::str;
167
168 #[test]
169 fn test_table() {
170 let b = Table::new(vec![TableRow::new(vec![])]).build();
171 assert_eq!(
172 str::from_utf8(&b).unwrap(),
173 r#"<w:tbl><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><w:tblGrid /><w:tr><w:trPr /></w:tr></w:tbl>"#
174 );
175 }
176
177 #[test]
178 fn test_table_grid() {
179 let b = Table::new(vec![TableRow::new(vec![])])
180 .set_grid(vec![100, 200])
181 .build();
182 assert_eq!(
183 str::from_utf8(&b).unwrap(),
184 r#"<w:tbl><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><w:tblGrid><w:gridCol w:w="100" w:type="dxa" /><w:gridCol w:w="200" w:type="dxa" /></w:tblGrid><w:tr><w:trPr /></w:tr></w:tbl>"#
185 );
186 }
187
188 #[test]
189 fn test_table_json() {
190 let t = Table::new(vec![]).set_grid(vec![100, 200, 300]);
191 assert_eq!(
192 serde_json::to_string(&t).unwrap(),
193 r#"{"rows":[],"grid":[100,200,300],"hasNumbering":false,"property":{"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}}}}"#
194 );
195 }
196}