docx_rust/formatting/
table_width.rs

1use hard_xml::{XmlRead, XmlWrite};
2
3use crate::{__string_enum, __xml_test_suites};
4
5/// Table Width
6///
7/// ```rust
8/// use docx_rust::formatting::*;
9///
10/// let width = TableWidth::from(42isize);
11/// let width = TableWidth::from(TableWidthUnit::Pct);
12/// let width = TableWidth::from((42, TableWidthUnit::Dxa));
13/// ```
14#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
15#[cfg_attr(test, derive(PartialEq))]
16#[xml(tag = "w:tblW")]
17pub struct TableWidth {
18    #[xml(attr = "w:w")]
19    pub value: Option<isize>,
20    #[xml(attr = "w:type")]
21    pub unit: Option<TableWidthUnit>,
22}
23
24#[derive(Debug, Default, XmlRead, XmlWrite, Clone)]
25#[cfg_attr(test, derive(PartialEq))]
26#[xml(tag = "w:tcW")]
27pub struct TableCellWidth {
28    #[xml(attr = "w:w")]
29    pub value: Option<isize>,
30    #[xml(attr = "w:type")]
31    pub unit: Option<TableWidthUnit>,
32}
33
34impl From<isize> for TableWidth {
35    fn from(val: isize) -> Self {
36        TableWidth {
37            value: Some(val),
38            unit: None,
39        }
40    }
41}
42
43impl From<TableWidthUnit> for TableWidth {
44    fn from(val: TableWidthUnit) -> Self {
45        TableWidth {
46            value: None,
47            unit: Some(val),
48        }
49    }
50}
51
52impl From<(isize, TableWidthUnit)> for TableWidth {
53    fn from(val: (isize, TableWidthUnit)) -> Self {
54        TableWidth {
55            value: Some(val.0),
56            unit: Some(val.1),
57        }
58    }
59}
60
61#[derive(Debug, Clone)]
62#[cfg_attr(test, derive(PartialEq))]
63pub enum TableWidthUnit {
64    Auto,
65    Dxa,
66    Nil,
67    Pct,
68}
69
70__string_enum! {
71    TableWidthUnit {
72        Auto = "auto",
73        Dxa = "dxa",
74        Nil = "nil",
75        Pct = "pct",
76    }
77}
78
79__xml_test_suites!(
80    TableWidth,
81    TableWidth::default(),
82    "<w:tblW/>",
83    TableWidth::from(42),
84    r#"<w:tblW w:w="42"/>"#,
85    TableWidth::from(TableWidthUnit::Pct),
86    r#"<w:tblW w:type="pct"/>"#,
87    TableWidth::from((42, TableWidthUnit::Dxa)),
88    r#"<w:tblW w:w="42" w:type="dxa"/>"#,
89);