Skip to content

Commit

Permalink
data validation: convert warnings to errors
Browse files Browse the repository at this point in the history
Feature #97
  • Loading branch information
jmcnamara committed Jul 13, 2024
1 parent f8cb0c5 commit 38f6dd9
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 77 deletions.
20 changes: 10 additions & 10 deletions examples/app_data_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ fn main() -> Result<(), XlsxError> {

let data_validation = DataValidation::new()
.allow_whole_number(DataValidationRule::Between(1, 100))
.set_input_title("Enter an integer:")
.set_input_message("between 1 and 100");
.set_input_title("Enter an integer:")?
.set_input_message("between 1 and 100")?;

worksheet.add_data_validation(24, 1, 24, 1, &data_validation)?;

Expand All @@ -199,10 +199,10 @@ fn main() -> Result<(), XlsxError> {

let data_validation = DataValidation::new()
.allow_whole_number(DataValidationRule::Between(1, 100))
.set_input_title("Enter an integer:")
.set_input_message("between 1 and 100")
.set_error_title("Input value is not valid!")
.set_error_message("It should be an integer between 1 and 100");
.set_input_title("Enter an integer:")?
.set_input_message("between 1 and 100")?
.set_error_title("Input value is not valid!")?
.set_error_message("It should be an integer between 1 and 100")?;

worksheet.add_data_validation(26, 1, 26, 1, &data_validation)?;

Expand All @@ -214,10 +214,10 @@ fn main() -> Result<(), XlsxError> {

let data_validation = DataValidation::new()
.allow_whole_number(DataValidationRule::Between(1, 100))
.set_input_title("Enter an integer:")
.set_input_message("between 1 and 100")
.set_error_title("Input value is not valid!")
.set_error_message("It should be an integer between 1 and 100")
.set_input_title("Enter an integer:")?
.set_input_message("between 1 and 100")?
.set_error_title("Input value is not valid!")?
.set_error_message("It should be an integer between 1 and 100")?
.set_error_style(DataValidationErrorStyle::Information);

worksheet.add_data_validation(28, 1, 28, 1, &data_validation)?;
Expand Down
8 changes: 4 additions & 4 deletions examples/doc_data_validation_intro1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ fn main() -> Result<(), XlsxError> {

let data_validation = DataValidation::new()
.allow_whole_number(DataValidationRule::Between(1, 5))
.set_input_title("Enter a star rating!")
.set_input_message("Enter rating 1-5.\nWhole numbers only.")
.set_error_title("Value outside allowed range")
.set_error_message("The input value must be an integer in the range 1-5.");
.set_input_title("Enter a star rating!")?
.set_input_message("Enter rating 1-5.\nWhole numbers only.")?
.set_error_title("Value outside allowed range")?
.set_error_message("The input value must be an integer in the range 1-5.")?;

worksheet.add_data_validation(1, 3, 1, 3, &data_validation)?;

Expand Down
4 changes: 2 additions & 2 deletions examples/doc_data_validation_set_error_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ fn main() -> Result<(), XlsxError> {

let data_validation = DataValidation::new()
.allow_whole_number(DataValidationRule::Between(1, 10))
.set_error_title("Value outside allowed range")
.set_error_message("The input value must be an integer in the range 1-10.");
.set_error_title("Value outside allowed range")?
.set_error_message("The input value must be an integer in the range 1-10.")?;

worksheet.add_data_validation(1, 3, 1, 3, &data_validation)?;

Expand Down
2 changes: 1 addition & 1 deletion examples/doc_data_validation_set_error_title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() -> Result<(), XlsxError> {

let data_validation = DataValidation::new()
.allow_whole_number(DataValidationRule::Between(1, 10))
.set_error_title("Danger, Will Robinson!");
.set_error_title("Danger, Will Robinson!")?;

worksheet.add_data_validation(1, 3, 1, 3, &data_validation)?;

Expand Down
4 changes: 2 additions & 2 deletions examples/doc_data_validation_set_input_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ fn main() -> Result<(), XlsxError> {

let data_validation = DataValidation::new()
.allow_whole_number(DataValidationRule::Between(1, 5))
.set_input_title("Enter a star rating!")
.set_input_message("Enter rating 1-5.\nWhole numbers only.");
.set_input_title("Enter a star rating!")?
.set_input_message("Enter rating 1-5.\nWhole numbers only.")?;

worksheet.add_data_validation(1, 3, 1, 3, &data_validation)?;

Expand Down
84 changes: 49 additions & 35 deletions src/data_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ use std::fmt;
///
/// let data_validation = DataValidation::new()
/// .allow_whole_number(DataValidationRule::Between(1, 5))
/// .set_input_title("Enter a star rating!")
/// .set_input_message("Enter rating 1-5.\nWhole numbers only.")
/// .set_error_title("Value outside allowed range")
/// .set_error_message("The input value must be an integer in the range 1-5.");
/// .set_input_title("Enter a star rating!")?
/// .set_input_message("Enter rating 1-5.\nWhole numbers only.")?
/// .set_error_title("Value outside allowed range")?
/// .set_error_message("The input value must be an integer in the range 1-5.")?;
///
/// worksheet.add_data_validation(1, 3, 1, 3, &data_validation)?;
///
Expand Down Expand Up @@ -1013,26 +1013,28 @@ impl DataValidation {
/// The title is only visible if there is also an input message. See the
/// [`DataValidation::set_input_message()`] example below.
///
/// Note, Excel limits the title to 32 characters.
/// # Errors
///
/// * [`XlsxError::DataValidationError`] - The length of the title exceeds
/// Excel's limit of 32 characters.
///
/// # Parameters
///
/// * `text` - Title string. Must be less than or equal to the Excel limit
/// of 32 characters.
///
pub fn set_input_title(mut self, text: impl Into<String>) -> DataValidation {
pub fn set_input_title(mut self, text: impl Into<String>) -> Result<DataValidation, XlsxError> {
let text = text.into();
let length = text.chars().count();

if length > 32 {
eprintln!(
return Err(XlsxError::DataValidationError(format!(
"Validation title length '{length}' greater than Excel's limit of 32 characters."
);
return self;
)));
}

self.input_title = text;
self
Ok(self)
}

/// Set the input message when a data validation cell is entered.
Expand All @@ -1041,7 +1043,10 @@ impl DataValidation {
/// is entered. This can we used to explain to the user what the data
/// validation rules are for the cell.
///
/// Note, Excel limits the message text to 255 characters.
/// # Errors
///
/// * [`XlsxError::DataValidationError`] - The length of the message exceeds
/// Excel's limit of 255 characters.
///
/// # Parameters
///
Expand Down Expand Up @@ -1069,8 +1074,8 @@ impl DataValidation {
/// #
/// let data_validation = DataValidation::new()
/// .allow_whole_number(DataValidationRule::Between(1, 5))
/// .set_input_title("Enter a star rating!")
/// .set_input_message("Enter rating 1-5.\nWhole numbers only.");
/// .set_input_title("Enter a star rating!")?
/// .set_input_message("Enter rating 1-5.\nWhole numbers only.")?;
///
/// worksheet.add_data_validation(1, 3, 1, 3, &data_validation)?;
/// #
Expand All @@ -1086,19 +1091,21 @@ impl DataValidation {
/// <img
/// src="https://rustxlsxwriter.github.io/images/data_validation_set_input_message.png">
///
pub fn set_input_message(mut self, text: impl Into<String>) -> DataValidation {
pub fn set_input_message(
mut self,
text: impl Into<String>,
) -> Result<DataValidation, XlsxError> {
let text = text.into();
let length = text.chars().count();

if length > 255 {
eprintln!(
"Validation message length '{length}' greater than Excel's limit of 255 characters."
);
return self;
return Err(XlsxError::DataValidationError(format!(
"Validation message length '{length}' greater than Excel's limit of 255 characters."
)));
}

self.input_message = text;
self
Ok(self)
}

/// Toggle option to show an error message when there is a validation error.
Expand All @@ -1124,7 +1131,10 @@ impl DataValidation {
/// This option is used to set a title in bold for the error message when
/// there is a validation error.
///
/// Note, Excel limits the title to 32 characters.
/// # Errors
///
/// * [`XlsxError::DataValidationError`] - The length of the title exceeds
/// Excel's limit of 32 characters.
///
/// # Parameters
///
Expand All @@ -1150,7 +1160,7 @@ impl DataValidation {
/// #
/// let data_validation = DataValidation::new()
/// .allow_whole_number(DataValidationRule::Between(1, 10))
/// .set_error_title("Danger, Will Robinson!");
/// .set_error_title("Danger, Will Robinson!")?;
///
/// worksheet.add_data_validation(1, 3, 1, 3, &data_validation)?;
///
Expand All @@ -1165,19 +1175,18 @@ impl DataValidation {
///
/// <img src="https://rustxlsxwriter.github.io/images/data_validation_set_error_title.png">
///
pub fn set_error_title(mut self, text: impl Into<String>) -> DataValidation {
pub fn set_error_title(mut self, text: impl Into<String>) -> Result<DataValidation, XlsxError> {
let text = text.into();
let length = text.chars().count();

if length > 32 {
eprintln!(
return Err(XlsxError::DataValidationError(format!(
"Validation title length '{length}' greater than Excel's limit of 32 characters."
);
return self;
)));
}

self.error_title = text;
self
Ok(self)
}

/// Set the error message when a data validation cell is entered.
Expand All @@ -1186,7 +1195,10 @@ impl DataValidation {
/// is entered. This can we used to explain to the user what the data
/// validation rules are for the cell.
///
/// Note, Excel limits the message text to 255 characters.
/// # Errors
///
/// * [`XlsxError::DataValidationError`] - The length of the message exceeds
/// Excel's limit of 255 characters.
///
/// # Parameters
///
Expand All @@ -1213,8 +1225,8 @@ impl DataValidation {
/// #
/// let data_validation = DataValidation::new()
/// .allow_whole_number(DataValidationRule::Between(1, 10))
/// .set_error_title("Value outside allowed range")
/// .set_error_message("The input value must be an integer in the range 1-10.");
/// .set_error_title("Value outside allowed range")?
/// .set_error_message("The input value must be an integer in the range 1-10.")?;
///
/// worksheet.add_data_validation(1, 3, 1, 3, &data_validation)?;
///
Expand All @@ -1229,19 +1241,21 @@ impl DataValidation {
///
/// <img src="https://rustxlsxwriter.github.io/images/data_validation_set_error_message.png">
///
pub fn set_error_message(mut self, text: impl Into<String>) -> DataValidation {
pub fn set_error_message(
mut self,
text: impl Into<String>,
) -> Result<DataValidation, XlsxError> {
let text = text.into();
let length = text.chars().count();

if length > 255 {
eprintln!(
"Validation message length '{length}' greater than Excel's limit of 255 characters."
);
return self;
return Err(XlsxError::DataValidationError(format!(
"Validation message length '{length}' greater than Excel's limit of 255 characters."
)));
}

self.error_message = text;
self
Ok(self)
}

/// Set the style of the error dialog type for a data validation.
Expand Down
44 changes: 25 additions & 19 deletions src/data_validation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ mod data_validation_tests {

let data_validation = DataValidation::new()
.allow_whole_number(DataValidationRule::NotEqualTo(10))
.set_input_title("Title 1");
.set_input_title("Title 1")?;

worksheet.add_data_validation(0, 0, 0, 0, &data_validation)?;

Expand Down Expand Up @@ -314,8 +314,8 @@ mod data_validation_tests {

let data_validation = DataValidation::new()
.allow_whole_number(DataValidationRule::NotEqualTo(10))
.set_input_title("Title 1")
.set_input_message("Message 1");
.set_input_title("Title 1")?
.set_input_message("Message 1")?;

worksheet.add_data_validation(0, 0, 0, 0, &data_validation)?;

Expand Down Expand Up @@ -356,10 +356,10 @@ mod data_validation_tests {

let data_validation = DataValidation::new()
.allow_whole_number(DataValidationRule::NotEqualTo(10))
.set_error_title("Title 2")
.set_error_message("Message 2")
.set_input_title("Title 1")
.set_input_message("Message 1");
.set_error_title("Title 2")?
.set_error_message("Message 2")?
.set_input_title("Title 1")?
.set_input_message("Message 1")?;

worksheet.add_data_validation(0, 0, 0, 0, &data_validation)?;

Expand Down Expand Up @@ -1147,7 +1147,7 @@ mod data_validation_tests {

let data_validation = DataValidation::new()
.allow_any_value()
.set_input_title("Title 1");
.set_input_title("Title 1")?;

worksheet.add_data_validation(0, 0, 0, 0, &data_validation)?;

Expand Down Expand Up @@ -1184,7 +1184,7 @@ mod data_validation_tests {
let mut worksheet = Worksheet::new();
worksheet.set_selected(true);

let data_validation = DataValidation::new().set_input_title("Title 1");
let data_validation = DataValidation::new().set_input_title("Title 1")?;

worksheet.add_data_validation(0, 0, 0, 0, &data_validation)?;

Expand Down Expand Up @@ -1223,7 +1223,7 @@ mod data_validation_tests {

let data_validation = DataValidation::new()
.allow_any_value()
.set_input_message("Message 1");
.set_input_message("Message 1")?;

worksheet.add_data_validation(0, 0, 0, 0, &data_validation)?;

Expand Down Expand Up @@ -1262,8 +1262,8 @@ mod data_validation_tests {

let data_validation = DataValidation::new()
.allow_any_value()
.set_error_title("Title 2")
.set_error_message("Message 2");
.set_error_title("Title 2")?
.set_error_message("Message 2")?;

worksheet.add_data_validation(0, 0, 0, 0, &data_validation)?;

Expand Down Expand Up @@ -1347,14 +1347,20 @@ mod data_validation_tests {
];

// Check for invalid string lengths.
let data_validation = DataValidation::new()
.allow_any_value()
.set_input_title(invalid_title)
.set_input_message(&invalid_message)
.set_error_title(invalid_title)
.set_error_message(&invalid_message);
let result = DataValidation::new().set_input_title(invalid_title);
assert!(matches!(result, Err(XlsxError::DataValidationError(_))));

worksheet.add_data_validation(0, 0, 0, 0, &data_validation)?;
// Check for invalid string lengths.
let result = DataValidation::new().set_input_message(&invalid_message);
assert!(matches!(result, Err(XlsxError::DataValidationError(_))));

// Check for invalid string lengths.
let result = DataValidation::new().set_error_title(invalid_title);
assert!(matches!(result, Err(XlsxError::DataValidationError(_))));

// Check for invalid string lengths.
let result = DataValidation::new().set_error_message(&invalid_message);
assert!(matches!(result, Err(XlsxError::DataValidationError(_))));

// Check for invalid string list.
let result = DataValidation::new().allow_list_strings(&list_values);
Expand Down
8 changes: 4 additions & 4 deletions src/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6466,10 +6466,10 @@ impl Worksheet {
///
/// let data_validation = DataValidation::new()
/// .allow_whole_number(DataValidationRule::Between(1, 5))
/// .set_input_title("Enter a star rating!")
/// .set_input_message("Enter rating 1-5.\nWhole numbers only.")
/// .set_error_title("Value outside allowed range")
/// .set_error_message("The input value must be an integer in the range 1-5.");
/// .set_input_title("Enter a star rating!")?
/// .set_input_message("Enter rating 1-5.\nWhole numbers only.")?
/// .set_error_title("Value outside allowed range")?
/// .set_error_message("The input value must be an integer in the range 1-5.")?;
///
/// worksheet.add_data_validation(1, 3, 1, 3, &data_validation)?;
///
Expand Down

0 comments on commit 38f6dd9

Please sign in to comment.