docx_reader/documents/elements/
table_property.rs1use serde::Serialize;
2
3use super::*;
4use crate::types::*;
5
6#[derive(Debug, Clone, PartialEq, Serialize)]
7#[serde(rename_all = "camelCase")]
8pub struct TableProperty {
9 width: TableWidth,
10 justification: Justification,
11 borders: TableBorders,
12 #[serde(skip_serializing_if = "Option::is_none")]
13 margins: Option<TableCellMargins>,
14 #[serde(skip_serializing_if = "Option::is_none")]
15 indent: Option<TableIndent>,
16 #[serde(skip_serializing_if = "Option::is_none")]
17 style: Option<TableStyle>,
18 #[serde(skip_serializing_if = "Option::is_none")]
19 layout: Option<TableLayout>,
20}
21
22impl Default for TableProperty {
23 fn default() -> Self {
24 TableProperty {
25 width: TableWidth::new(0, WidthType::Auto),
26 justification: Justification::new("left"),
27 borders: TableBorders::new(),
28 margins: None,
29 indent: None,
30 style: None,
31 layout: None,
32 }
33 }
34}
35
36impl TableProperty {
37 pub fn new() -> TableProperty {
38 Default::default()
39 }
40
41 pub fn without_borders() -> TableProperty {
42 TableProperty {
43 borders: TableBorders::with_empty(),
44 ..Default::default()
45 }
46 }
47
48 pub fn indent(mut self, v: i32) -> TableProperty {
49 self.indent = Some(TableIndent::new(v, WidthType::Dxa));
50 self
51 }
52
53 pub fn width(mut self, v: usize, t: WidthType) -> TableProperty {
54 self.width = TableWidth::new(v, t);
55 self
56 }
57
58 pub fn align(mut self, v: TableAlignmentType) -> TableProperty {
59 self.justification = Justification::new(v.to_string());
60 self
61 }
62
63 pub fn set_margins(mut self, margins: TableCellMargins) -> Self {
64 self.margins = Some(margins);
65 self
66 }
67
68 pub fn cell_margin_top(mut self, v: usize, t: WidthType) -> Self {
69 if let Some(margins) = self.margins {
70 self.margins = Some(margins.margin_top(v, t));
71 } else {
72 let margins = TableCellMargins::new();
73 self.margins = Some(margins.margin_top(v, t));
74 }
75 self
76 }
77
78 pub fn cell_margin_right(mut self, v: usize, t: WidthType) -> Self {
79 if let Some(margins) = self.margins {
80 self.margins = Some(margins.margin_right(v, t));
81 } else {
82 let margins = TableCellMargins::new();
83 self.margins = Some(margins.margin_right(v, t));
84 }
85 self
86 }
87
88 pub fn cell_margin_bottom(mut self, v: usize, t: WidthType) -> Self {
89 if let Some(margins) = self.margins {
90 self.margins = Some(margins.margin_bottom(v, t));
91 } else {
92 let margins = TableCellMargins::new();
93 self.margins = Some(margins.margin_bottom(v, t));
94 }
95 self
96 }
97
98 pub fn cell_margin_left(mut self, v: usize, t: WidthType) -> Self {
99 if let Some(margins) = self.margins {
100 self.margins = Some(margins.margin_left(v, t));
101 } else {
102 let margins = TableCellMargins::new();
103 self.margins = Some(margins.margin_left(v, t));
104 }
105 self
106 }
107
108 pub fn set_borders(mut self, borders: TableBorders) -> Self {
109 self.borders = borders;
110 self
111 }
112
113 pub fn set_border(mut self, border: TableBorder) -> Self {
114 self.borders = self.borders.set(border);
115 self
116 }
117
118 pub fn clear_border(mut self, position: TableBorderPosition) -> Self {
119 self.borders = self.borders.clear(position);
120 self
121 }
122
123 pub fn clear_all_border(mut self) -> Self {
124 self.borders = self.borders.clear_all();
125 self
126 }
127
128 pub fn style(mut self, s: impl Into<String>) -> Self {
129 self.style = Some(TableStyle::new(s));
130 self
131 }
132
133 pub fn layout(mut self, t: TableLayoutType) -> Self {
134 self.layout = Some(TableLayout::new(t));
135 self
136 }
137}