docx-rs/docx-core/src/documents/elements/table.rs

50 lines
1.4 KiB
Rust
Raw Normal View History

2019-11-12 12:21:08 +02:00
use super::{TableProperty, TableRow};
use crate::documents::BuildXML;
use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct Table {
property: TableProperty,
rows: Vec<TableRow>,
}
impl Table {
pub fn new(rows: Vec<TableRow>) -> Table {
let property = TableProperty::new();
Self { property, rows }
}
}
impl BuildXML for Table {
fn build(&self) -> Vec<u8> {
let b = XMLBuilder::new()
.open_table()
.add_child(&self.property)
.add_children(&self.rows);
b.close().build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_row() {
let b = Table::new(vec![TableRow::new(vec![])]).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:tbl><w:tblPr><w:tblW w:w="9638" 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:insideH 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:tr><w:trPr /></w:tr></w:tbl>"#
);
}
}