docx_rs/documents/elements/
table_position_property.rs

1use serde::Serialize;
2use std::io::Write;
3
4use crate::documents::BuildXML;
5use crate::xml_builder::*;
6
7/// https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.tablepositionproperties?view=openxml-3.0.1
8#[derive(Debug, Clone, PartialEq, Serialize, Default)]
9#[serde(rename_all = "camelCase")]
10#[cfg_attr(feature = "wasm", derive(ts_rs::TS), ts(export))]
11pub struct TablePositionProperty {
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub left_from_text: Option<i32>,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub right_from_text: Option<i32>,
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub vertical_anchor: Option<String>,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub horizontal_anchor: Option<String>,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub position_x_alignment: Option<String>,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub position_y_alignment: Option<String>,
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub position_x: Option<i32>,
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub position_y: Option<i32>,
28}
29
30impl TablePositionProperty {
31    pub fn new() -> TablePositionProperty {
32        Default::default()
33    }
34
35    pub fn left_from_text(mut self, v: i32) -> Self {
36        self.left_from_text = Some(v);
37        self
38    }
39
40    pub fn right_from_text(mut self, v: i32) -> Self {
41        self.right_from_text = Some(v);
42        self
43    }
44
45    pub fn vertical_anchor(mut self, v: impl Into<String>) -> Self {
46        self.vertical_anchor = Some(v.into());
47        self
48    }
49
50    pub fn horizontal_anchor(mut self, v: impl Into<String>) -> Self {
51        self.horizontal_anchor = Some(v.into());
52        self
53    }
54
55    pub fn position_x_alignment(mut self, v: impl Into<String>) -> Self {
56        self.position_x_alignment = Some(v.into());
57        self
58    }
59
60    pub fn position_y_alignment(mut self, v: impl Into<String>) -> Self {
61        self.position_y_alignment = Some(v.into());
62        self
63    }
64
65    pub fn position_x(mut self, v: i32) -> Self {
66        self.position_x = Some(v);
67        self
68    }
69
70    pub fn position_y(mut self, v: i32) -> Self {
71        self.position_y = Some(v);
72        self
73    }
74}
75
76impl BuildXML for TablePositionProperty {
77    fn build_to<W: Write>(
78        &self,
79        stream: xml::writer::EventWriter<W>,
80    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
81        XMLBuilder::from(stream)
82            .table_position_property(self)?
83            .into_inner()
84    }
85}
86
87#[cfg(test)]
88mod tests {
89
90    use super::*;
91    #[cfg(test)]
92    use pretty_assertions::assert_eq;
93    use std::str;
94
95    #[test]
96    fn test_default() {
97        let b = TablePositionProperty::new().build();
98        assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:tblpPr />"#);
99    }
100
101    #[test]
102    fn test_some_attrs() {
103        let b = TablePositionProperty::new()
104            .left_from_text(142)
105            .right_from_text(142)
106            .vertical_anchor("text")
107            .horizontal_anchor("margin")
108            .position_x_alignment("right")
109            .position_y(511)
110            .build();
111        assert_eq!(
112            str::from_utf8(&b).unwrap(),
113            r#"<w:tblpPr w:leftFromText="142" w:rightFromText="142" w:vertAnchor="text" w:horzAnchor="margin" w:tblpXSpec="right" w:tblpY="511" />"#
114        );
115    }
116}