docx_rust/formatting/
table_property.rs

1use hard_xml::{XmlRead, XmlWrite};
2use std::borrow::Cow;
3
4use crate::{
5    __setter, __string_enum, __xml_test_suites,
6    formatting::{TableBorders, TableIndent, TableJustification, TableWidth},
7};
8
9use super::table_margin::TableMargins;
10
11/// Table Property
12///
13/// ```rust
14/// use docx_rust::formatting::*;
15///
16/// let prop = TableProperty::default()
17///     .style_id("foo")
18///     .justification(TableJustificationVal::Start)
19///     .indent((50, TableIndentUnit::Pct))
20///     .width((50, TableWidthUnit::Pct));
21/// ```
22#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
23#[cfg_attr(test, derive(PartialEq))]
24#[xml(tag = "w:tblPr")]
25pub struct TableProperty<'a> {
26    #[xml(child = "w:tblStyle")]
27    pub style_id: Option<TableStyleId<'a>>,
28    #[xml(child = "w:tblW")]
29    pub width: Option<TableWidth>,
30    #[xml(child = "w:jc")]
31    pub justification: Option<TableJustification>,
32    #[xml(child = "w:tblInd")]
33    pub indent: Option<TableIndent>,
34    #[xml(child = "w:tblBorders")]
35    pub borders: Option<TableBorders<'a>>,
36    #[xml(child = "w:tblCellMar")]
37    pub margins: Option<TableMargins<'a>>,
38}
39
40impl<'a> TableProperty<'a> {
41    __setter!(style_id: Option<TableStyleId<'a>>);
42    __setter!(justification: Option<TableJustification>);
43    __setter!(borders: Option<TableBorders<'a>>);
44    __setter!(indent: Option<TableIndent>);
45    __setter!(width: Option<TableWidth>);
46}
47
48#[derive(Debug, XmlRead, XmlWrite, Clone)]
49#[cfg_attr(test, derive(PartialEq))]
50#[xml(tag = "w:tblStyle")]
51pub struct TableStyleId<'a> {
52    #[xml(attr = "w:val")]
53    pub value: Cow<'a, str>,
54}
55
56impl<'a, T: Into<Cow<'a, str>>> From<T> for TableStyleId<'a> {
57    fn from(val: T) -> Self {
58        TableStyleId { value: val.into() }
59    }
60}
61
62#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
63#[cfg_attr(test, derive(PartialEq))]
64#[xml(tag = "w:tblStylePr")]
65pub struct ConditionalTableProperty<'a> {
66    #[xml(attr = "type")]
67    pub condition: Option<ConditionType>,
68    /// Specifies a set of paragraph properties
69    #[xml(default, child = "w:pPr")]
70    pub paragraph: Option<super::ParagraphProperty<'a>>,
71    /// Specifies a set of character properties
72    #[xml(default, child = "w:rPr")]
73    pub character: Option<super::CharacterProperty<'a>>,
74    #[xml(default, child = "w:tblPr")]
75    pub table: Option<TableProperty<'a>>,
76    #[xml(child = "w:trPr")]
77    pub table_row: Option<crate::formatting::TableRowProperty>,
78    #[xml(child = "w:tcPr")]
79    pub table_cell: Option<crate::formatting::TableCellProperty>,
80}
81
82#[derive(Debug, Default, Clone)]
83#[cfg_attr(test, derive(PartialEq))]
84pub enum ConditionType {
85    #[default]
86    WholeTable, //Whole table formatting.
87    FirstRow,  //First Row Conditional Formatting.
88    LastRow,   //Last table row formatting.
89    FirstCol,  //First Column Conditional Formatting.
90    LastCol,   //Last table column formatting.
91    Band1Vert, //Banded Column Conditional Formatting.
92    Band2Vert, //Even Column Stripe Conditional Formatting.
93    Band1Horz, //Banded Row Conditional Formatting.
94    Band2Horz, //Even Row Stripe Conditional Formatting.
95    NeCell,    //Top right table cell formatting.
96    NwCell,    //Top left table cell formatting.
97    SeCell,    //Bottom right table cell formatting.
98    SwCell,    //Bottom left table cell formatting.
99}
100
101__string_enum! {
102    ConditionType {
103        WholeTable = "wholeTable",
104        FirstRow = "firstRow",
105        LastRow = "lastRow",
106        FirstCol = "firstCol",
107        LastCol = "lastCol",
108        Band1Vert = "band1Vert",
109        Band2Vert = "band2Vert",
110        Band1Horz = "band1Horz",
111        Band2Horz = "band2Horz",
112        NeCell = "neCell",
113        NwCell = "nwCell",
114        SeCell = "seCell",
115        SwCell = "swCell",
116    }
117}
118
119__xml_test_suites!(
120    TableProperty,
121    TableProperty::default(),
122    r#"<w:tblPr/>"#,
123    TableProperty::default().style_id("id"),
124    r#"<w:tblPr><w:tblStyle w:val="id"/></w:tblPr>"#,
125    TableProperty::default().justification(crate::formatting::TableJustificationVal::Start),
126    r#"<w:tblPr><w:jc w:val="start"/></w:tblPr>"#,
127    TableProperty::default().borders(TableBorders::default()),
128    r#"<w:tblPr><w:tblBorders/></w:tblPr>"#,
129    TableProperty::default().indent(TableIndent::default()),
130    r#"<w:tblPr><w:tblInd/></w:tblPr>"#,
131    TableProperty::default().width(TableWidth::default()),
132    r#"<w:tblPr><w:tblW/></w:tblPr>"#,
133);