2020-02-11 10:01:39 +02:00
|
|
|
use serde::Serialize;
|
|
|
|
|
2020-04-27 20:00:02 +03:00
|
|
|
use super::*;
|
2019-11-12 12:21:08 +02:00
|
|
|
use crate::documents::BuildXML;
|
2019-12-13 05:02:49 +02:00
|
|
|
use crate::types::*;
|
2019-11-12 12:21:08 +02:00
|
|
|
use crate::xml_builder::*;
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-12-08 21:14:27 +02:00
|
|
|
pub struct Table {
|
2019-12-11 07:12:22 +02:00
|
|
|
pub rows: Vec<TableRow>,
|
2019-12-12 11:10:57 +02:00
|
|
|
pub grid: Vec<usize>,
|
2020-02-11 10:01:39 +02:00
|
|
|
pub has_numbering: bool,
|
|
|
|
pub property: TableProperty,
|
2019-11-12 12:21:08 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl Table {
|
|
|
|
pub fn new(rows: Vec<TableRow>) -> Table {
|
2019-11-12 12:21:08 +02:00
|
|
|
let property = TableProperty::new();
|
2019-12-16 04:36:04 +02:00
|
|
|
let has_numbering = rows.iter().any(|c| c.has_numbering);
|
2019-11-13 07:08:00 +02:00
|
|
|
let grid = vec![];
|
|
|
|
Self {
|
|
|
|
property,
|
|
|
|
rows,
|
|
|
|
grid,
|
2019-12-16 04:36:04 +02:00
|
|
|
has_numbering,
|
2019-11-13 07:08:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
pub fn add_row(mut self, row: TableRow) -> Table {
|
|
|
|
self.rows.push(row);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn set_grid(mut self, grid: Vec<usize>) -> Table {
|
2019-11-13 07:08:00 +02:00
|
|
|
self.grid = grid;
|
|
|
|
self
|
2019-11-12 12:21:08 +02:00
|
|
|
}
|
2019-12-13 05:02:49 +02:00
|
|
|
|
2020-03-10 05:02:51 +02:00
|
|
|
pub fn indent(mut self, v: i32) -> Table {
|
2019-12-13 05:02:49 +02:00
|
|
|
self.property = self.property.indent(v);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn align(mut self, v: TableAlignmentType) -> Table {
|
|
|
|
self.property = self.property.align(v);
|
|
|
|
self
|
|
|
|
}
|
2019-12-14 17:39:01 +02:00
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
pub fn width(mut self, w: usize, t: WidthType) -> Table {
|
|
|
|
self.property = self.property.width(w, t);
|
2019-12-14 17:39:01 +02:00
|
|
|
self
|
|
|
|
}
|
2020-04-27 20:00:02 +03:00
|
|
|
|
|
|
|
pub fn set_borders(mut self, borders: TableBorders) -> Self {
|
|
|
|
self.property = self.property.set_borders(borders);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_border(mut self, border: TableBorder) -> Self {
|
|
|
|
self.property = self.property.set_border(border);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear_border(mut self, position: BorderPosition) -> Self {
|
|
|
|
self.property = self.property.clear_border(position);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear_all_border(mut self) -> Self {
|
|
|
|
self.property = self.property.clear_all_border();
|
|
|
|
self
|
|
|
|
}
|
2019-11-12 12:21:08 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
impl BuildXML for Table {
|
2019-11-12 12:21:08 +02:00
|
|
|
fn build(&self) -> Vec<u8> {
|
2019-11-13 07:08:00 +02:00
|
|
|
let grid = TableGrid::new(self.grid.clone());
|
2019-11-12 12:21:08 +02:00
|
|
|
let b = XMLBuilder::new()
|
|
|
|
.open_table()
|
|
|
|
.add_child(&self.property)
|
2019-11-13 07:08:00 +02:00
|
|
|
.add_child(&grid)
|
2019-11-12 12:21:08 +02:00
|
|
|
.add_children(&self.rows);
|
|
|
|
b.close().build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
#[cfg(test)]
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
#[test]
|
2019-11-13 07:08:00 +02:00
|
|
|
fn test_table() {
|
2019-11-12 12:21:08 +02:00
|
|
|
let b = Table::new(vec![TableRow::new(vec![])]).build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2019-11-13 06:55:58 +02:00
|
|
|
r#"<w:tbl><w:tblPr><w:tblW w:w="0" w:type="dxa" /><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:tblCellMar>
|
2019-11-12 12:21:08 +02:00
|
|
|
<w:top w:w="55" w:type="dxa" />
|
|
|
|
<w:left w:w="54" w:type="dxa" />
|
|
|
|
<w:bottom w:w="55" w:type="dxa" />
|
|
|
|
<w:right w:w="55" w:type="dxa" />
|
2019-11-13 08:00:53 +02:00
|
|
|
</w:tblCellMar></w:tblPr><w:tblGrid /><w:tr><w:trPr /></w:tr></w:tbl>"#
|
2019-11-12 12:21:08 +02:00
|
|
|
);
|
|
|
|
}
|
2019-11-13 07:08:00 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_table_grid() {
|
|
|
|
let b = Table::new(vec![TableRow::new(vec![])])
|
|
|
|
.set_grid(vec![100, 200])
|
|
|
|
.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
|
|
|
r#"<w:tbl><w:tblPr><w:tblW w:w="0" w:type="dxa" /><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:tblCellMar>
|
|
|
|
<w:top w:w="55" w:type="dxa" />
|
|
|
|
<w:left w:w="54" w:type="dxa" />
|
|
|
|
<w:bottom w:w="55" w:type="dxa" />
|
|
|
|
<w:right w:w="55" w:type="dxa" />
|
|
|
|
</w:tblCellMar></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>"#
|
|
|
|
);
|
|
|
|
}
|
2020-02-11 10:01:39 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_table_json() {
|
|
|
|
let t = Table::new(vec![]).set_grid(vec![100, 200, 300]);
|
|
|
|
assert_eq!(
|
|
|
|
serde_json::to_string(&t).unwrap(),
|
2020-04-27 20:00:02 +03:00
|
|
|
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}},"margins":{"top":55,"left":54,"bottom":55,"right":55},"indent":null}}"#
|
2020-02-11 10:01:39 +02:00
|
|
|
);
|
|
|
|
}
|
2019-11-12 12:21:08 +02:00
|
|
|
}
|