umya_spreadsheet/structs/
data_validation.rs

1// dataValidation
2use super::BooleanValue;
3use super::DataValidationOperatorValues;
4use super::DataValidationValues;
5use super::EnumValue;
6use super::SequenceOfReferences;
7use super::StringValue;
8use crate::reader::driver::*;
9use crate::writer::driver::*;
10use quick_xml::events::{BytesStart, Event};
11use quick_xml::Reader;
12use quick_xml::Writer;
13use std::io::Cursor;
14use std::vec;
15
16#[derive(Default, Debug, Clone)]
17pub struct DataValidation {
18    r#type: EnumValue<DataValidationValues>,
19    operator: EnumValue<DataValidationOperatorValues>,
20    allow_blank: BooleanValue,
21    show_input_message: BooleanValue,
22    show_error_message: BooleanValue,
23    prompt_title: StringValue,
24    prompt: StringValue,
25    error_title: StringValue,
26    error_messsage: StringValue,
27    sequence_of_references: SequenceOfReferences,
28    formula1: StringValue,
29    formula2: StringValue,
30}
31impl DataValidation {
32    #[inline]
33    pub fn get_type(&self) -> &DataValidationValues {
34        self.r#type.get_value()
35    }
36
37    #[inline]
38    pub fn set_type(&mut self, value: DataValidationValues) -> &mut Self {
39        self.r#type.set_value(value);
40        self
41    }
42
43    #[inline]
44    pub fn get_operator(&self) -> &DataValidationOperatorValues {
45        self.operator.get_value()
46    }
47
48    #[inline]
49    pub fn set_operator(&mut self, value: DataValidationOperatorValues) -> &mut Self {
50        self.operator.set_value(value);
51        self
52    }
53
54    #[inline]
55    pub fn get_allow_blank(&self) -> &bool {
56        self.allow_blank.get_value()
57    }
58
59    #[inline]
60    pub fn set_allow_blank(&mut self, value: bool) -> &mut Self {
61        self.allow_blank.set_value(value);
62        self
63    }
64
65    #[inline]
66    pub fn get_show_input_message(&self) -> &bool {
67        self.show_input_message.get_value()
68    }
69
70    #[inline]
71    pub fn set_show_input_message(&mut self, value: bool) -> &mut Self {
72        self.show_input_message.set_value(value);
73        self
74    }
75
76    #[inline]
77    pub fn get_show_error_message(&self) -> &bool {
78        self.show_error_message.get_value()
79    }
80
81    #[inline]
82    pub fn set_show_error_message(&mut self, value: bool) -> &mut Self {
83        self.show_error_message.set_value(value);
84        self
85    }
86
87    #[inline]
88    pub fn get_prompt_title(&self) -> &str {
89        self.prompt_title.get_value_str()
90    }
91
92    #[inline]
93    pub fn set_prompt_title<S: Into<String>>(&mut self, value: S) -> &mut Self {
94        self.prompt_title.set_value(value);
95        self
96    }
97
98    #[inline]
99    pub fn get_error_title(&self) -> &str {
100        self.error_title.get_value_str()
101    }
102
103    #[inline]
104    pub fn set_error_title<S: Into<String>>(&mut self, value: S) -> &mut Self {
105        self.error_title.set_value(value);
106        self
107    }
108
109    #[inline]
110    pub fn get_error_message(&self) -> &str {
111        self.error_messsage.get_value_str()
112    }
113
114    #[inline]
115    pub fn set_error_message<S: Into<String>>(&mut self, value: S) -> &mut Self {
116        self.error_messsage.set_value(value);
117        self
118    }
119
120    #[inline]
121    pub fn get_prompt(&self) -> &str {
122        self.prompt.get_value_str()
123    }
124
125    #[inline]
126    pub fn set_prompt<S: Into<String>>(&mut self, value: S) -> &mut Self {
127        self.prompt.set_value(value);
128        self
129    }
130
131    #[inline]
132    pub fn get_sequence_of_references(&self) -> &SequenceOfReferences {
133        &self.sequence_of_references
134    }
135
136    #[inline]
137    pub fn get_sequence_of_references_mut(&mut self) -> &mut SequenceOfReferences {
138        &mut self.sequence_of_references
139    }
140
141    #[inline]
142    pub fn set_sequence_of_references(&mut self, value: SequenceOfReferences) -> &mut Self {
143        self.sequence_of_references = value;
144        self
145    }
146
147    #[inline]
148    pub fn get_formula1(&self) -> &str {
149        self.formula1.get_value_str()
150    }
151
152    #[inline]
153    pub fn set_formula1<S: Into<String>>(&mut self, value: S) -> &mut Self {
154        self.formula1.set_value(value);
155        self
156    }
157
158    #[inline]
159    pub fn get_formula2(&self) -> &str {
160        self.formula2.get_value_str()
161    }
162
163    #[inline]
164    pub fn set_formula2<S: Into<String>>(&mut self, value: S) -> &mut Self {
165        self.formula2.set_value(value);
166        self
167    }
168
169    pub(crate) fn set_attributes<R: std::io::BufRead>(
170        &mut self,
171        reader: &mut Reader<R>,
172        e: &BytesStart,
173        empty_flg: bool,
174    ) {
175        if let Some(v) = get_attribute(e, b"type") {
176            self.r#type.set_value_string(v);
177        }
178
179        if let Some(v) = get_attribute(e, b"operator") {
180            self.operator.set_value_string(v);
181        }
182
183        if let Some(v) = get_attribute(e, b"allowBlank") {
184            self.allow_blank.set_value_string(v);
185        }
186
187        if let Some(v) = get_attribute(e, b"showInputMessage") {
188            self.show_input_message.set_value_string(v);
189        }
190
191        if let Some(v) = get_attribute(e, b"showErrorMessage") {
192            self.show_error_message.set_value_string(v);
193        }
194
195        if let Some(v) = get_attribute(e, b"errorTitle") {
196            self.error_title.set_value_string(v);
197        }
198
199        if let Some(v) = get_attribute(e, b"error") {
200            self.error_messsage.set_value_string(v);
201        }
202
203        if let Some(v) = get_attribute(e, b"promptTitle") {
204            self.prompt_title.set_value_string(v);
205        }
206
207        if let Some(v) = get_attribute(e, b"prompt") {
208            self.prompt.set_value_string(v);
209        }
210
211        if let Some(v) = get_attribute(e, b"sqref") {
212            self.sequence_of_references.set_sqref(v);
213        }
214
215        if empty_flg {
216            return;
217        }
218
219        let mut value: String = String::new();
220        let mut buf = Vec::new();
221        loop {
222            match reader.read_event_into(&mut buf) {
223                Ok(Event::Text(e)) => {
224                    value = e.unescape().unwrap().to_string();
225                }
226                Ok(Event::End(ref e)) => match e.name().into_inner() {
227                    b"formula1" => {
228                        self.formula1.set_value_string(std::mem::take(&mut value));
229                    }
230                    b"formula2" => {
231                        self.formula2.set_value_string(std::mem::take(&mut value));
232                    }
233                    b"dataValidation" => return,
234                    _ => {}
235                },
236                Ok(Event::Eof) => panic!("Error: Could not find {} end element", "dataValidation"),
237                Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
238                _ => {}
239            }
240            buf.clear();
241        }
242    }
243
244    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
245        let is_inner = self.formula1.has_value() || self.formula2.has_value();
246
247        // dataValidation
248        let mut attributes: Vec<(&str, &str)> = Vec::new();
249
250        if self.r#type.has_value() {
251            attributes.push(("type", self.r#type.get_value_string()));
252        }
253
254        if self.allow_blank.has_value() {
255            attributes.push(("allowBlank", self.allow_blank.get_value_string()));
256        }
257
258        if self.show_input_message.has_value() {
259            attributes.push((
260                "showInputMessage",
261                self.show_input_message.get_value_string(),
262            ));
263        }
264
265        if self.operator.has_value() {
266            attributes.push(("operator", self.operator.get_value_string()));
267        }
268
269        if self.show_error_message.has_value() {
270            attributes.push((
271                "showErrorMessage",
272                self.show_error_message.get_value_string(),
273            ));
274        }
275
276        if self.error_title.has_value() {
277            attributes.push(("errorTitle", self.error_title.get_value_str()));
278        }
279
280        if self.error_messsage.has_value() {
281            attributes.push(("error", self.error_messsage.get_value_str()));
282        }
283
284        if self.prompt_title.has_value() {
285            attributes.push(("promptTitle", self.prompt_title.get_value_str()));
286        }
287
288        if self.prompt.has_value() {
289            attributes.push(("prompt", self.prompt.get_value_str()));
290        }
291
292        let sequence_of_references = &self.sequence_of_references.get_sqref();
293        if !sequence_of_references.is_empty() {
294            attributes.push(("sqref", sequence_of_references));
295        }
296
297        write_start_tag(writer, "dataValidation", attributes, !is_inner);
298        if is_inner {
299            if self.formula1.has_value() {
300                write_start_tag(writer, "formula1", vec![], false);
301                write_text_node(writer, self.formula1.get_value_str());
302                write_end_tag(writer, "formula1");
303            }
304            if self.formula2.has_value() {
305                write_start_tag(writer, "formula2", vec![], false);
306                write_text_node(writer, self.formula2.get_value_str());
307                write_end_tag(writer, "formula2");
308            }
309            write_end_tag(writer, "dataValidation");
310        }
311    }
312}