Skip to main content

umya_spreadsheet/structs/
alignment.rs

1// alignment
2use std::io::Cursor;
3
4use md5::Digest;
5use quick_xml::{
6    Reader,
7    Writer,
8    events::BytesStart,
9};
10
11use super::{
12    BooleanValue,
13    EnumValue,
14    HorizontalAlignmentValues,
15    UInt32Value,
16    VerticalAlignmentValues,
17};
18use crate::{
19    reader::driver::{
20        get_attribute,
21        set_string_from_xml,
22    },
23    writer::driver::write_start_tag,
24};
25
26#[derive(Default, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
27pub struct Alignment {
28    horizontal:    EnumValue<HorizontalAlignmentValues>,
29    vertical:      EnumValue<VerticalAlignmentValues>,
30    wrap_text:     BooleanValue,
31    text_rotation: UInt32Value,
32}
33
34impl Alignment {
35    #[inline]
36    #[must_use]
37    pub fn horizontal(&self) -> &HorizontalAlignmentValues {
38        self.horizontal.value()
39    }
40
41    #[inline]
42    #[must_use]
43    #[deprecated(since = "3.0.0", note = "Use horizontal()")]
44    pub fn get_horizontal(&self) -> &HorizontalAlignmentValues {
45        self.horizontal()
46    }
47
48    #[inline]
49    pub fn set_horizontal(&mut self, value: HorizontalAlignmentValues) {
50        self.horizontal.set_value(value);
51    }
52
53    #[inline]
54    #[must_use]
55    pub fn vertical(&self) -> &VerticalAlignmentValues {
56        self.vertical.value()
57    }
58
59    #[inline]
60    #[must_use]
61    #[deprecated(since = "3.0.0", note = "Use vertical()")]
62    pub fn get_vertical(&self) -> &VerticalAlignmentValues {
63        self.vertical()
64    }
65
66    #[inline]
67    pub fn set_vertical(&mut self, value: VerticalAlignmentValues) {
68        self.vertical.set_value(value);
69    }
70
71    #[inline]
72    #[must_use]
73    pub fn wrap_text(&self) -> bool {
74        self.wrap_text.value()
75    }
76
77    #[inline]
78    #[must_use]
79    #[deprecated(since = "3.0.0", note = "Use wrap_text()")]
80    pub fn get_wrap_text(&self) -> bool {
81        self.wrap_text()
82    }
83
84    #[inline]
85    pub fn set_wrap_text(&mut self, value: bool) {
86        self.wrap_text.set_value(value);
87    }
88
89    #[inline]
90    #[must_use]
91    pub fn text_rotation(&self) -> u32 {
92        self.text_rotation.value()
93    }
94
95    #[inline]
96    #[must_use]
97    #[deprecated(since = "3.0.0", note = "Use text_rotation()")]
98    pub fn get_text_rotation(&self) -> u32 {
99        self.text_rotation()
100    }
101
102    #[inline]
103    pub fn set_text_rotation(&mut self, value: u32) {
104        self.text_rotation.set_value(value);
105    }
106
107    pub(crate) fn hash_code(&self) -> String {
108        format!(
109            "{:x}",
110            md5::Md5::digest(format!(
111                "{}{}{}{}",
112                self.horizontal.hash_string(),
113                self.vertical.hash_string(),
114                self.wrap_text.hash_string(),
115                self.text_rotation.hash_string(),
116            ))
117        )
118    }
119
120    #[deprecated(since = "3.0.0", note = "Use hash_code()")]
121    pub(crate) fn get_hash_code(&self) -> String {
122        self.hash_code()
123    }
124
125    #[inline]
126    pub(crate) fn set_attributes<R: std::io::BufRead>(
127        &mut self,
128        _reader: &mut Reader<R>,
129        e: &BytesStart,
130    ) {
131        set_string_from_xml!(self, e, horizontal, "horizontal");
132        set_string_from_xml!(self, e, vertical, "vertical");
133        set_string_from_xml!(self, e, wrap_text, "wrapText");
134        set_string_from_xml!(self, e, text_rotation, "textRotation");
135    }
136
137    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
138        // alignment
139        let mut attributes: crate::structs::AttrCollection = Vec::new();
140        if self.horizontal.has_value() {
141            attributes.push(("horizontal", self.horizontal.value_string()).into());
142        }
143        if self.vertical.has_value() {
144            attributes.push(("vertical", self.vertical.value_string()).into());
145        }
146        if self.wrap_text.has_value() {
147            attributes.push(("wrapText", self.wrap_text.value_string()).into());
148        }
149        let text_rotation = self.text_rotation.value_string();
150        if self.text_rotation.has_value() {
151            attributes.push(("textRotation", text_rotation).into());
152        }
153        write_start_tag(writer, "alignment", attributes, true);
154    }
155}