From 27f46e3f0f4b2cac655e7dab80a097790bd351d5 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 21 Oct 2023 15:11:56 -0400 Subject: [PATCH 1/4] support for dataValidation.operator --- src/structs.rs | 3 ++ src/structs/data_validation.rs | 8 ++- .../data_validation_operator_values.rs | 53 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/structs/data_validation_operator_values.rs diff --git a/src/structs.rs b/src/structs.rs index b07c9c8a..dd417b3d 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -400,6 +400,9 @@ pub use self::formula::*; mod data_validation_values; pub use self::data_validation_values::*; +mod data_validation_operator_values; +pub use self::data_validation_operator_values::*; + mod data_validation; pub use self::data_validation::*; diff --git a/src/structs/data_validation.rs b/src/structs/data_validation.rs index 8d6352af..79cd4fc8 100644 --- a/src/structs/data_validation.rs +++ b/src/structs/data_validation.rs @@ -1,5 +1,6 @@ // dataValidation use super::BooleanValue; +use super::DataValidationOperatorValues; use super::DataValidationValues; use super::EnumValue; use super::SequenceOfReferences; @@ -15,6 +16,7 @@ use writer::driver::*; #[derive(Default, Debug, Clone)] pub struct DataValidation { r#type: EnumValue, + operator: EnumValue, allow_blank: BooleanValue, show_input_message: BooleanValue, show_error_message: BooleanValue, @@ -217,6 +219,10 @@ impl DataValidation { )); } + if self.operator.has_value() { + attributes.push(("operator", self.operator.get_value_string())); + } + if self.show_error_message.has_value() { attributes.push(( "showErrorMessage", @@ -233,7 +239,7 @@ impl DataValidation { } let sequence_of_references = &self.sequence_of_references.get_sqref(); - if sequence_of_references != "" { + if !sequence_of_references.is_empty() { attributes.push(("sqref", sequence_of_references)); } diff --git a/src/structs/data_validation_operator_values.rs b/src/structs/data_validation_operator_values.rs new file mode 100644 index 00000000..6ad2e9c4 --- /dev/null +++ b/src/structs/data_validation_operator_values.rs @@ -0,0 +1,53 @@ +use super::EnumTrait; +use std::str::FromStr; + +#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] +pub enum DataValidationOperatorValues { + Between, + Equal, + GreaterThan, + GreaterThanOrEqual, + LessThan, + LessThanOrEqual, + NotBetween, + NotEqual, +} + +impl EnumTrait for DataValidationOperatorValues { + fn get_value_string(&self) -> &str { + match self { + Self::Between => "between", + Self::Equal => "equal", + Self::GreaterThan => "greaterThan", + Self::GreaterThanOrEqual => "greaterThanOrEqual", + Self::LessThan => "lessThan", + Self::LessThanOrEqual => "lessThanOrEqual", + Self::NotBetween => "notBetween", + Self::NotEqual => "notEqual", + } + } +} + +impl Default for DataValidationOperatorValues { + fn default() -> Self { + Self::LessThan + } +} + +impl FromStr for DataValidationOperatorValues { + type Err = (); + + fn from_str(input: &str) -> Result { + Ok(match input { + "between" => Self::Between, + "equal" => Self::Equal, + "greaterThan" => Self::GreaterThan, + "greaterThanOrEqual" => Self::GreaterThanOrEqual, + "lessThan" => Self::LessThan, + "lessThanOrEqual" => Self::LessThanOrEqual, + "notBetween" => Self::NotBetween, + "notEqual" => Self::NotEqual, + _ => return Err(()), + }) + } +} From c770d68b957dd9e1afd1146a7734dbd95e285314 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 21 Oct 2023 15:15:58 -0400 Subject: [PATCH 2/4] cleanup clippy warnings a lot of unnecessary matching here when you can just use if/let syntax --- src/structs/data_validation.rs | 49 ++++++++++------------------------ 1 file changed, 14 insertions(+), 35 deletions(-) diff --git a/src/structs/data_validation.rs b/src/structs/data_validation.rs index 79cd4fc8..3a9ab2a5 100644 --- a/src/structs/data_validation.rs +++ b/src/structs/data_validation.rs @@ -118,53 +118,32 @@ impl DataValidation { e: &BytesStart, empty_flg: bool, ) { - match get_attribute(e, b"type") { - Some(v) => { - self.r#type.set_value_string(v); - } - None => {} + if let Some(v) = get_attribute(e, b"type") { + self.r#type.set_value_string(v); } - match get_attribute(e, b"allowBlank") { - Some(v) => { - self.allow_blank.set_value_string(v); - } - None => {} + if let Some(v) = get_attribute(e, b"allowBlank") { + self.allow_blank.set_value_string(v); } - match get_attribute(e, b"showInputMessage") { - Some(v) => { - self.show_input_message.set_value_string(v); - } - None => {} + if let Some(v) = get_attribute(e, b"showInputMessage") { + self.show_input_message.set_value_string(v); } - match get_attribute(e, b"showErrorMessage") { - Some(v) => { - self.show_error_message.set_value_string(v); - } - None => {} + if let Some(v) = get_attribute(e, b"showErrorMessage") { + self.show_error_message.set_value_string(v); } - match get_attribute(e, b"promptTitle") { - Some(v) => { - self.prompt_title.set_value_string(v); - } - None => {} + if let Some(v) = get_attribute(e, b"promptTitle") { + self.prompt_title.set_value_string(v); } - match get_attribute(e, b"prompt") { - Some(v) => { - self.prompt.set_value_string(v); - } - None => {} + if let Some(v) = get_attribute(e, b"prompt") { + self.prompt.set_value_string(v); } - match get_attribute(e, b"sqref") { - Some(v) => { - self.sequence_of_references.set_sqref(v); - } - None => {} + if let Some(v) = get_attribute(e, b"sqref") { + self.sequence_of_references.set_sqref(v); } if empty_flg { From 2e4ba70adc061e55951c40edf6ae8c5b81745a91 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 21 Oct 2023 15:30:09 -0400 Subject: [PATCH 3/4] setters and getters for `operator` --- src/structs/data_validation.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/structs/data_validation.rs b/src/structs/data_validation.rs index 3a9ab2a5..020ad57e 100644 --- a/src/structs/data_validation.rs +++ b/src/structs/data_validation.rs @@ -36,6 +36,15 @@ impl DataValidation { self } + pub fn get_operator(&self) -> &DataValidationOperatorValues { + self.operator.get_value() + } + + pub fn set_operator(&mut self, value: DataValidationOperatorValues) -> &mut Self { + self.operator.set_value(value); + self + } + pub fn get_allow_blank(&self) -> &bool { self.allow_blank.get_value() } From ce792342b405e9a42e2a15c53075c285cca296f8 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 21 Oct 2023 16:08:49 -0400 Subject: [PATCH 4/4] load operator in .set_attributes() --- src/structs/data_validation.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/structs/data_validation.rs b/src/structs/data_validation.rs index 020ad57e..c0d0b796 100644 --- a/src/structs/data_validation.rs +++ b/src/structs/data_validation.rs @@ -131,6 +131,10 @@ impl DataValidation { self.r#type.set_value_string(v); } + if let Some(v) = get_attribute(e, b"operator") { + self.operator.set_value_string(v); + } + if let Some(v) = get_attribute(e, b"allowBlank") { self.allow_blank.set_value_string(v); }