docx_rust/formatting/
table_property.rs1use 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#[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 #[xml(default, child = "w:pPr")]
70 pub paragraph: Option<super::ParagraphProperty<'a>>,
71 #[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, FirstRow, LastRow, FirstCol, LastCol, Band1Vert, Band2Vert, Band1Horz, Band2Horz, NeCell, NwCell, SeCell, SwCell, }
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);