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

85 lines
2.4 KiB
Rust
Raw Normal View History

2019-11-12 10:32:50 +02:00
use super::{Justification, TableBorders, TableCellMargins, TableIndent, TableWidth};
use crate::documents::BuildXML;
use crate::types::*;
use crate::xml_builder::*;
2019-11-12 12:21:08 +02:00
#[derive(Debug, Clone)]
2019-11-12 10:32:50 +02:00
pub struct TableProperty {
width: TableWidth,
justification: Justification,
borders: TableBorders,
margins: TableCellMargins,
indent: Option<TableIndent>,
}
impl Default for TableProperty {
fn default() -> Self {
TableProperty {
width: TableWidth::new(9638, WidthType::DXA),
justification: Justification::new("left"),
borders: TableBorders::new(),
margins: TableCellMargins::new(),
indent: None,
}
}
}
impl TableProperty {
pub fn new() -> TableProperty {
Default::default()
}
pub fn indent(mut self, v: usize) -> TableProperty {
self.indent = Some(TableIndent::new(v, WidthType::DXA));
self
}
pub fn width(mut self, v: usize) -> TableProperty {
self.width = TableWidth::new(v, WidthType::DXA);
self
}
pub fn justify(mut self, v: &str) -> TableProperty {
self.justification = Justification::new(v);
self
}
}
impl BuildXML for TableProperty {
fn build(&self) -> Vec<u8> {
XMLBuilder::new()
.open_table_property()
.add_child(&self.width)
.add_child(&self.justification)
.add_child(&self.borders)
.add_child(&self.margins)
.add_optional_child(&self.indent)
.close()
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
use pretty_assertions::assert_eq;
use std::str;
#[test]
fn test_default() {
let c = TableProperty::new();
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<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>"#
);
}
}