1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::borrow::Cow;
use strong_xml::{XmlRead, XmlWrite};

use crate::{
    __setter, __xml_test_suites,
    formatting::{TableBorders, TableIndent, TableJustification, TableWidth},
};

use super::table_margin::TableMargins;

/// Table Property
///
/// ```rust
/// use docx_rust::formatting::*;
///
/// let prop = TableProperty::default()
///     .style_id("foo")
///     .justification(TableJustificationVal::Start)
///     .indent((50, TableIndentUnit::Pct))
///     .width((50, TableWidthUnit::Pct));
/// ```
#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[xml(tag = "w:tblPr")]
pub struct TableProperty<'a> {
    #[xml(child = "w:tblStyle")]
    pub style_id: Option<TableStyleId<'a>>,
    #[xml(child = "w:jc")]
    pub justification: Option<TableJustification>,
    #[xml(child = "w:tblBorders")]
    pub borders: Option<TableBorders<'a>>,
    #[xml(child = "w:tblInd")]
    pub indent: Option<TableIndent>,
    #[xml(child = "w:tblCellMar")]
    pub margins: Option<TableMargins<'a>>,
    #[xml(child = "w:tblW")]
    pub width: Option<TableWidth>,
}

impl<'a> TableProperty<'a> {
    __setter!(style_id: Option<TableStyleId<'a>>);
    __setter!(justification: Option<TableJustification>);
    __setter!(borders: Option<TableBorders<'a>>);
    __setter!(indent: Option<TableIndent>);
    __setter!(width: Option<TableWidth>);
}

#[derive(Debug, XmlRead, XmlWrite, Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[xml(tag = "w:tblStyle")]
pub struct TableStyleId<'a> {
    #[xml(attr = "w:val")]
    pub value: Cow<'a, str>,
}

impl<'a, T: Into<Cow<'a, str>>> From<T> for TableStyleId<'a> {
    fn from(val: T) -> Self {
        TableStyleId { value: val.into() }
    }
}

__xml_test_suites!(
    TableProperty,
    TableProperty::default(),
    r#"<w:tblPr/>"#,
    TableProperty::default().style_id("id"),
    r#"<w:tblPr><w:tblStyle w:val="id"/></w:tblPr>"#,
    TableProperty::default().justification(crate::formatting::TableJustificationVal::Start),
    r#"<w:tblPr><w:jc w:val="start"/></w:tblPr>"#,
    TableProperty::default().borders(TableBorders::default()),
    r#"<w:tblPr><w:tblBorders/></w:tblPr>"#,
    TableProperty::default().indent(TableIndent::default()),
    r#"<w:tblPr><w:tblInd/></w:tblPr>"#,
    TableProperty::default().width(TableWidth::default()),
    r#"<w:tblPr><w:tblW/></w:tblPr>"#,
);