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

@@ -42,7 +42,17 @@ impl Worksheet {
self.sheet_data.get_mut(&row)?.get_mut(&column)
}
pub(crate) fn update_cell(&mut self, row: i32, column: i32, new_cell: Cell) {
pub(crate) fn update_cell(
&mut self,
row: i32,
column: i32,
new_cell: Cell,
) -> Result<(), String> {
// validate row and column arg before updating cell of worksheet
if !is_valid_row(row) || !is_valid_column_number(column) {
return Err("Incorrect row or column".to_string());
}
match self.sheet_data.get_mut(&row) {
Some(column_data) => match column_data.get(&column) {
Some(_cell) => {
@@ -58,6 +68,7 @@ impl Worksheet {
self.sheet_data.insert(row, column_data);
}
}
Ok(())
}
// TODO [MVP]: Pass the cell style from the model
@@ -128,53 +139,94 @@ impl Worksheet {
Ok(())
}
pub fn set_cell_style(&mut self, row: i32, column: i32, style_index: i32) {
pub fn set_cell_style(
&mut self,
row: i32,
column: i32,
style_index: i32,
) -> Result<(), String> {
match self.cell_mut(row, column) {
Some(cell) => {
cell.set_style(style_index);
}
None => {
self.cell_clear_contents_with_style(row, column, style_index);
self.cell_clear_contents_with_style(row, column, style_index)?;
}
}
Ok(())
// TODO: cleanup check if the old cell style is still in use
}
pub fn set_cell_with_formula(&mut self, row: i32, column: i32, index: i32, style: i32) {
pub fn set_cell_with_formula(
&mut self,
row: i32,
column: i32,
index: i32,
style: i32,
) -> Result<(), String> {
let cell = Cell::new_formula(index, style);
self.update_cell(row, column, cell);
self.update_cell(row, column, cell)
}
pub fn set_cell_with_number(&mut self, row: i32, column: i32, value: f64, style: i32) {
pub fn set_cell_with_number(
&mut self,
row: i32,
column: i32,
value: f64,
style: i32,
) -> Result<(), String> {
let cell = Cell::new_number(value, style);
self.update_cell(row, column, cell);
self.update_cell(row, column, cell)
}
pub fn set_cell_with_string(&mut self, row: i32, column: i32, index: i32, style: i32) {
pub fn set_cell_with_string(
&mut self,
row: i32,
column: i32,
index: i32,
style: i32,
) -> Result<(), String> {
let cell = Cell::new_string(index, style);
self.update_cell(row, column, cell);
self.update_cell(row, column, cell)
}
pub fn set_cell_with_boolean(&mut self, row: i32, column: i32, value: bool, style: i32) {
pub fn set_cell_with_boolean(
&mut self,
row: i32,
column: i32,
value: bool,
style: i32,
) -> Result<(), String> {
let cell = Cell::new_boolean(value, style);
self.update_cell(row, column, cell);
self.update_cell(row, column, cell)
}
pub fn set_cell_with_error(&mut self, row: i32, column: i32, error: Error, style: i32) {
pub fn set_cell_with_error(
&mut self,
row: i32,
column: i32,
error: Error,
style: i32,
) -> Result<(), String> {
let cell = Cell::new_error(error, style);
self.update_cell(row, column, cell);
self.update_cell(row, column, cell)
}
pub fn cell_clear_contents(&mut self, row: i32, column: i32) {
pub fn cell_clear_contents(&mut self, row: i32, column: i32) -> Result<(), String> {
let s = self.get_style(row, column);
let cell = Cell::EmptyCell { s };
self.update_cell(row, column, cell);
self.update_cell(row, column, cell)
}
pub fn cell_clear_contents_with_style(&mut self, row: i32, column: i32, style: i32) {
pub fn cell_clear_contents_with_style(
&mut self,
row: i32,
column: i32,
style: i32,
) -> Result<(), String> {
let cell = Cell::EmptyCell { s: style };
self.update_cell(row, column, cell);
self.update_cell(row, column, cell)
}
pub fn set_frozen_rows(&mut self, frozen_rows: i32) -> Result<(), String> {