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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use std::borrow::Cow;
use strong_xml::{XmlRead, XmlWrite};

use crate::{
    __setter, __string_enum, __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:tblW")]
    pub width: Option<TableWidth>,
    #[xml(child = "w:jc")]
    pub justification: Option<TableJustification>,
    #[xml(child = "w:tblInd")]
    pub indent: Option<TableIndent>,
    #[xml(child = "w:tblBorders")]
    pub borders: Option<TableBorders<'a>>,
    #[xml(child = "w:tblCellMar")]
    pub margins: Option<TableMargins<'a>>,
}

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() }
    }
}

#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[xml(tag = "w:tblStylePr")]
pub struct ConditionalTableProperty<'a> {
    #[xml(attr = "type")]
    pub condition: ConditionType,
    /// Specifies a set of paragraph properties
    #[xml(default, child = "w:pPr")]
    pub paragraph: Option<super::ParagraphProperty<'a>>,
    /// Specifies a set of character properties
    #[xml(default, child = "w:rPr")]
    pub character: Option<super::CharacterProperty<'a>>,
    #[xml(default, child = "w:tblPr")]
    pub table: Option<TableProperty<'a>>,
    #[xml(child = "w:trPr")]
    pub table_row: Option<crate::formatting::TableRowProperty>,
    #[xml(child = "w:tcPr")]
    pub table_cell: Option<crate::formatting::TableCellProperty>,
}

#[derive(Debug, Default, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub enum ConditionType {
    #[default]
    WholeTable, //Whole table formatting.
    FirstRow,  //First Row Conditional Formatting.
    LastRow,   //Last table row formatting.
    FirstCol,  //First Column Conditional Formatting.
    LastCol,   //Last table column formatting.
    Band1Vert, //Banded Column Conditional Formatting.
    Band2Vert, //Even Column Stripe Conditional Formatting.
    Band1Horz, //Banded Row Conditional Formatting.
    Band2Horz, //Even Row Stripe Conditional Formatting.
    NeCell,    //Top right table cell formatting.
    NwCell,    //Top left table cell formatting.
    SeCell,    //Bottom right table cell formatting.
    SwCell,    //Bottom left table cell formatting.
}

__string_enum! {
    ConditionType {
        WholeTable = "wholeTable",
        FirstRow = "firstRow",
        LastRow = "lastRow",
        FirstCol = "firstCol",
        LastCol = "lastCol",
        Band1Vert = "band1Vert",
        Band2Vert = "band2Vert",
        Band1Horz = "band1Horz",
        Band2Horz = "band2Horz",
        NeCell = "neCell",
        NwCell = "nwCell",
        SeCell = "seCell",
        SwCell = "swCell",
    }
}

__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>"#,
);