umya_spreadsheet/structs/
alignment.rs1use super::BooleanValue;
3use super::EnumValue;
4use super::HorizontalAlignmentValues;
5use super::UInt32Value;
6use super::VerticalAlignmentValues;
7use crate::reader::driver::*;
8use crate::writer::driver::*;
9use md5::Digest;
10use quick_xml::events::BytesStart;
11use quick_xml::Reader;
12use quick_xml::Writer;
13use std::io::Cursor;
14
15#[derive(Default, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
16pub struct Alignment {
17 horizontal: EnumValue<HorizontalAlignmentValues>,
18 vertical: EnumValue<VerticalAlignmentValues>,
19 wrap_text: BooleanValue,
20 text_rotation: UInt32Value,
21}
22
23impl Alignment {
24 #[inline]
25 pub fn get_horizontal(&self) -> &HorizontalAlignmentValues {
26 self.horizontal.get_value()
27 }
28
29 #[inline]
30 pub fn set_horizontal(&mut self, value: HorizontalAlignmentValues) {
31 self.horizontal.set_value(value);
32 }
33
34 #[inline]
35 pub fn get_vertical(&self) -> &VerticalAlignmentValues {
36 self.vertical.get_value()
37 }
38
39 #[inline]
40 pub fn set_vertical(&mut self, value: VerticalAlignmentValues) {
41 self.vertical.set_value(value);
42 }
43
44 #[inline]
45 pub fn get_wrap_text(&self) -> &bool {
46 self.wrap_text.get_value()
47 }
48
49 #[inline]
50 pub fn set_wrap_text(&mut self, value: bool) {
51 self.wrap_text.set_value(value);
52 }
53
54 #[inline]
55 pub fn get_text_rotation(&self) -> &u32 {
56 self.text_rotation.get_value()
57 }
58
59 #[inline]
60 pub fn set_text_rotation(&mut self, value: u32) {
61 self.text_rotation.set_value(value);
62 }
63
64 pub(crate) fn get_hash_code(&self) -> String {
65 format!(
66 "{:x}",
67 md5::Md5::digest(format!(
68 "{}{}{}{}",
69 &self.horizontal.get_hash_string(),
70 &self.vertical.get_hash_string(),
71 &self.wrap_text.get_hash_string(),
72 &self.text_rotation.get_hash_string(),
73 ))
74 )
75 }
76
77 #[inline]
78 pub(crate) fn set_attributes<R: std::io::BufRead>(
79 &mut self,
80 _reader: &mut Reader<R>,
81 e: &BytesStart,
82 ) {
83 set_string_from_xml!(self, e, horizontal, "horizontal");
84 set_string_from_xml!(self, e, vertical, "vertical");
85 set_string_from_xml!(self, e, wrap_text, "wrapText");
86 set_string_from_xml!(self, e, text_rotation, "textRotation");
87 }
88
89 pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
90 let mut attributes: Vec<(&str, &str)> = Vec::new();
92 if self.horizontal.has_value() {
93 attributes.push(("horizontal", self.horizontal.get_value_string()));
94 }
95 if self.vertical.has_value() {
96 attributes.push(("vertical", self.vertical.get_value_string()));
97 }
98 if self.wrap_text.has_value() {
99 attributes.push(("wrapText", self.wrap_text.get_value_string()));
100 }
101 let text_rotation = self.text_rotation.get_value_string();
102 if self.text_rotation.has_value() {
103 attributes.push(("textRotation", &text_rotation));
104 }
105 write_start_tag(writer, "alignment", attributes, true);
106 }
107}