Error Handling of public Set functions (#88)

What are we trying to achieve ?

++ Currently all the major public set functions is panic prone and does not handle and return error. This PR tries to address to all those functions.

What major errors that could happen in these functions ?

++ All the functions which are being made as error safe is being tested against invalid sheet, row and column values, which could given by user

What are the list of functions whose return type has been altered ?

**base/src/model.rs**
1. update_cell_with_text
2. update_cell_with_bool
3. update_cell_with_number
4. set_user_input
5. get_cell_style_index
6. get_style_for_cell
7. set_cell_with_string

++> New functions being added

1. set_cell_with_boolean
2. set_cell_with_number

**base/src/styles.rs**

1. get_style_with_quote_prefix
3. get_style_with_format
4. get_style_without_quote_prefix
5. get_style

**base/src/worksheet.rs**

1. update_cell
2. set_cell_style
3. set_cell_with_formula
4. set_cell_with_number
6. set_cell_with_string
8. set_cell_with_boolean
9. set_cell_with_error
10. cell_clear_contents
11. cell_clear_contents_with_style

++> Above is the comprehensive list of all functions being ( most are public, some are private ) altered for better error handling. As a side effect of changing function signature, there are many changes being done to other functions ( mostly adding "?" to enable to error propagation further )
This commit is contained in:
Varun Hegde
2024-09-14 21:07:31 +05:30
committed by GitHub
parent 83a4431417
commit 2b03b3e3b9
26 changed files with 1087 additions and 362 deletions

View File

@@ -9,7 +9,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
for row in 1..100 {
for column in 1..100 {
let value = row * column;
model.set_user_input(0, row, column, format!("{}", value));
model
.set_user_input(0, row, column, format!("{}", value))
.unwrap();
}
}
// Adds a new sheet
@@ -17,7 +19,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// column 100 is CV
let last_column = number_to_column(100).unwrap();
let formula = format!("=SUM(Sheet1!A1:{}100)", last_column);
model.set_user_input(1, 1, 1, formula);
model.set_user_input(1, 1, 1, formula).unwrap();
// evaluates
model.evaluate();

View File

@@ -5,7 +5,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// We are going to change styles in cell A1
let (sheet, row, column) = (0, 1, 1);
let mut style = model.get_style_for_cell(sheet, row, column);
let mut style = model.get_style_for_cell(sheet, row, column)?;
style.fill.fg_color = Some("#FF9011".to_string());
style.font.b = true;
style.font.color = Some("#E91E63".to_string());

View File

@@ -15,16 +15,26 @@ pub fn new_empty_model() -> Model {
fn test_values() {
let mut model = new_empty_model();
// numbers
model.set_user_input(0, 1, 1, "123.456".to_string());
model
.set_user_input(0, 1, 1, "123.456".to_string())
.unwrap();
// strings
model.set_user_input(0, 2, 1, "Hello world!".to_string());
model.set_user_input(0, 3, 1, "Hello world!".to_string());
model.set_user_input(0, 4, 1, "你好世界!".to_string());
model
.set_user_input(0, 2, 1, "Hello world!".to_string())
.unwrap();
model
.set_user_input(0, 3, 1, "Hello world!".to_string())
.unwrap();
model
.set_user_input(0, 4, 1, "你好世界!".to_string())
.unwrap();
// booleans
model.set_user_input(0, 5, 1, "TRUE".to_string());
model.set_user_input(0, 6, 1, "FALSE".to_string());
model.set_user_input(0, 5, 1, "TRUE".to_string()).unwrap();
model.set_user_input(0, 6, 1, "FALSE".to_string()).unwrap();
// errors
model.set_user_input(0, 7, 1, "#VALUE!".to_string());
model
.set_user_input(0, 7, 1, "#VALUE!".to_string())
.unwrap();
// noop
model.evaluate();
@@ -81,14 +91,16 @@ fn test_values() {
#[test]
fn test_formulas() {
let mut model = new_empty_model();
model.set_user_input(0, 1, 1, "5.5".to_string());
model.set_user_input(0, 2, 1, "6.5".to_string());
model.set_user_input(0, 3, 1, "7.5".to_string());
model.set_user_input(0, 1, 1, "5.5".to_string()).unwrap();
model.set_user_input(0, 2, 1, "6.5".to_string()).unwrap();
model.set_user_input(0, 3, 1, "7.5".to_string()).unwrap();
model.set_user_input(0, 1, 2, "=A1*2".to_string());
model.set_user_input(0, 2, 2, "=A2*2".to_string());
model.set_user_input(0, 3, 2, "=A3*2".to_string());
model.set_user_input(0, 4, 2, "=SUM(A1:B3)".to_string());
model.set_user_input(0, 1, 2, "=A1*2".to_string()).unwrap();
model.set_user_input(0, 2, 2, "=A2*2".to_string()).unwrap();
model.set_user_input(0, 3, 2, "=A3*2".to_string()).unwrap();
model
.set_user_input(0, 4, 2, "=SUM(A1:B3)".to_string())
.unwrap();
model.evaluate();
let temp_file_name = "temp_file_test_formulas.xlsx";
@@ -127,12 +139,12 @@ fn test_sheets() {
#[test]
fn test_named_styles() {
let mut model = new_empty_model();
model.set_user_input(0, 1, 1, "5.5".to_string());
let mut style = model.get_style_for_cell(0, 1, 1);
model.set_user_input(0, 1, 1, "5.5".to_string()).unwrap();
let mut style = model.get_style_for_cell(0, 1, 1).unwrap();
style.font.b = true;
style.font.i = true;
assert!(model.set_cell_style(0, 1, 1, &style).is_ok());
let bold_style_index = model.get_cell_style_index(0, 1, 1);
let bold_style_index = model.get_cell_style_index(0, 1, 1).unwrap();
let e = model
.workbook
.styles

View File

@@ -158,42 +158,42 @@ fn test_split() {
fn test_model_has_correct_styles(model: &Model) {
// A1 is bold
let style_a1 = model.get_style_for_cell(0, 1, 1);
let style_a1 = model.get_style_for_cell(0, 1, 1).unwrap();
assert!(style_a1.font.b);
assert!(!style_a1.font.i);
assert!(!style_a1.font.u);
// B1 is Italics
let style_b1 = model.get_style_for_cell(0, 1, 2);
let style_b1 = model.get_style_for_cell(0, 1, 2).unwrap();
assert!(style_b1.font.i);
assert!(!style_b1.font.b);
assert!(!style_b1.font.u);
// C1 Underlined
let style_c1 = model.get_style_for_cell(0, 1, 3);
let style_c1 = model.get_style_for_cell(0, 1, 3).unwrap();
assert!(style_c1.font.u);
assert!(!style_c1.font.b);
assert!(!style_c1.font.i);
// D1 Bold and Italics
let style_d1 = model.get_style_for_cell(0, 1, 4);
let style_d1 = model.get_style_for_cell(0, 1, 4).unwrap();
assert!(style_d1.font.b);
assert!(style_d1.font.i);
assert!(!style_d1.font.u);
// E1 Bold, italics and underlined
let style_e1 = model.get_style_for_cell(0, 1, 5);
let style_e1 = model.get_style_for_cell(0, 1, 5).unwrap();
assert!(style_e1.font.b);
assert!(style_e1.font.i);
assert!(style_e1.font.u);
assert!(!style_e1.font.strike);
// F1 strikethrough
let style_f1 = model.get_style_for_cell(0, 1, 6);
let style_f1 = model.get_style_for_cell(0, 1, 6).unwrap();
assert!(style_f1.font.strike);
// G1 Double underlined just get simple underlined
let style_g1 = model.get_style_for_cell(0, 1, 7);
let style_g1 = model.get_style_for_cell(0, 1, 7).unwrap();
assert!(style_g1.font.u);
let height_row_3 = model.workbook.worksheet(0).unwrap().row_height(3).unwrap();
@@ -204,44 +204,84 @@ fn test_model_has_correct_styles(model: &Model) {
// Second sheet has alignment
// Horizontal
let alignment = model.get_style_for_cell(1, 2, 1).alignment;
let alignment = model.get_style_for_cell(1, 2, 1).unwrap().alignment;
assert_eq!(alignment, None);
let alignment = model.get_style_for_cell(1, 3, 1).alignment.unwrap();
let alignment = model
.get_style_for_cell(1, 3, 1)
.unwrap()
.alignment
.unwrap();
assert_eq!(alignment.horizontal, HorizontalAlignment::Left);
let alignment = model.get_style_for_cell(1, 4, 1).alignment.unwrap();
let alignment = model
.get_style_for_cell(1, 4, 1)
.unwrap()
.alignment
.unwrap();
assert_eq!(alignment.horizontal, HorizontalAlignment::Distributed);
let alignment = model.get_style_for_cell(1, 5, 1).alignment.unwrap();
let alignment = model
.get_style_for_cell(1, 5, 1)
.unwrap()
.alignment
.unwrap();
assert_eq!(alignment.horizontal, HorizontalAlignment::Right);
let alignment = model.get_style_for_cell(1, 6, 1).alignment.unwrap();
let alignment = model
.get_style_for_cell(1, 6, 1)
.unwrap()
.alignment
.unwrap();
assert_eq!(alignment.horizontal, HorizontalAlignment::Center);
let alignment = model.get_style_for_cell(1, 7, 1).alignment.unwrap();
let alignment = model
.get_style_for_cell(1, 7, 1)
.unwrap()
.alignment
.unwrap();
assert_eq!(alignment.horizontal, HorizontalAlignment::Fill);
let alignment = model.get_style_for_cell(1, 8, 1).alignment.unwrap();
let alignment = model
.get_style_for_cell(1, 8, 1)
.unwrap()
.alignment
.unwrap();
assert_eq!(alignment.horizontal, HorizontalAlignment::Justify);
// Vertical
let alignment = model.get_style_for_cell(1, 2, 2).alignment;
let alignment = model.get_style_for_cell(1, 2, 2).unwrap().alignment;
assert_eq!(alignment, None);
let alignment = model.get_style_for_cell(1, 3, 2).alignment;
let alignment = model.get_style_for_cell(1, 3, 2).unwrap().alignment;
assert_eq!(alignment, None);
let alignment = model.get_style_for_cell(1, 4, 2).alignment.unwrap();
let alignment = model
.get_style_for_cell(1, 4, 2)
.unwrap()
.alignment
.unwrap();
assert_eq!(alignment.vertical, VerticalAlignment::Top);
let alignment = model.get_style_for_cell(1, 5, 2).alignment.unwrap();
let alignment = model
.get_style_for_cell(1, 5, 2)
.unwrap()
.alignment
.unwrap();
assert_eq!(alignment.vertical, VerticalAlignment::Center);
let alignment = model.get_style_for_cell(1, 6, 2).alignment.unwrap();
let alignment = model
.get_style_for_cell(1, 6, 2)
.unwrap()
.alignment
.unwrap();
assert_eq!(alignment.vertical, VerticalAlignment::Justify);
let alignment = model.get_style_for_cell(1, 7, 2).alignment.unwrap();
let alignment = model
.get_style_for_cell(1, 7, 2)
.unwrap()
.alignment
.unwrap();
assert_eq!(alignment.vertical, VerticalAlignment::Distributed);
}
@@ -280,7 +320,9 @@ fn test_defined_names_casing() {
("=NaMeD3", "33"),
];
for (formula, expected_value) in test_cases {
model.set_user_input(0, row, column, formula.to_string());
model
.set_user_input(0, row, column, formula.to_string())
.unwrap();
model.evaluate();
assert_eq!(
model.get_formatted_cell_value(0, row, column).unwrap(),