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:
@@ -161,26 +161,29 @@ impl Styles {
|
||||
|
||||
pub fn create_named_style(&mut self, style_name: &str, style: &Style) -> Result<(), String> {
|
||||
let style_index = self.create_new_style(style);
|
||||
self.add_named_cell_style(style_name, style_index)?;
|
||||
Ok(())
|
||||
self.add_named_cell_style(style_name, style_index)
|
||||
}
|
||||
|
||||
pub(crate) fn get_style_with_quote_prefix(&mut self, index: i32) -> i32 {
|
||||
let mut style = self.get_style(index);
|
||||
pub(crate) fn get_style_with_quote_prefix(&mut self, index: i32) -> Result<i32, String> {
|
||||
let mut style = self.get_style(index)?;
|
||||
style.quote_prefix = true;
|
||||
self.get_style_index_or_create(&style)
|
||||
Ok(self.get_style_index_or_create(&style))
|
||||
}
|
||||
|
||||
pub(crate) fn get_style_with_format(&mut self, index: i32, num_fmt: &str) -> i32 {
|
||||
let mut style = self.get_style(index);
|
||||
pub(crate) fn get_style_with_format(
|
||||
&mut self,
|
||||
index: i32,
|
||||
num_fmt: &str,
|
||||
) -> Result<i32, String> {
|
||||
let mut style = self.get_style(index)?;
|
||||
style.num_fmt = num_fmt.to_string();
|
||||
self.get_style_index_or_create(&style)
|
||||
Ok(self.get_style_index_or_create(&style))
|
||||
}
|
||||
|
||||
pub(crate) fn get_style_without_quote_prefix(&mut self, index: i32) -> i32 {
|
||||
let mut style = self.get_style(index);
|
||||
pub(crate) fn get_style_without_quote_prefix(&mut self, index: i32) -> Result<i32, String> {
|
||||
let mut style = self.get_style(index)?;
|
||||
style.quote_prefix = false;
|
||||
self.get_style_index_or_create(&style)
|
||||
Ok(self.get_style_index_or_create(&style))
|
||||
}
|
||||
|
||||
pub(crate) fn style_is_quote_prefix(&self, index: i32) -> bool {
|
||||
@@ -188,9 +191,11 @@ impl Styles {
|
||||
cell_xf.quote_prefix
|
||||
}
|
||||
|
||||
pub(crate) fn get_style(&self, index: i32) -> Style {
|
||||
let cell_xf = &self.cell_xfs[index as usize];
|
||||
|
||||
pub(crate) fn get_style(&self, index: i32) -> Result<Style, String> {
|
||||
let cell_xf = &self
|
||||
.cell_xfs
|
||||
.get(index as usize)
|
||||
.ok_or("Invalid index provided".to_string())?;
|
||||
let border_id = cell_xf.border_id as usize;
|
||||
let fill_id = cell_xf.fill_id as usize;
|
||||
let font_id = cell_xf.font_id as usize;
|
||||
@@ -198,14 +203,14 @@ impl Styles {
|
||||
let quote_prefix = cell_xf.quote_prefix;
|
||||
let alignment = cell_xf.alignment.clone();
|
||||
|
||||
Style {
|
||||
Ok(Style {
|
||||
alignment,
|
||||
num_fmt: get_num_fmt(num_fmt_id, &self.num_fmts),
|
||||
fill: self.fills[fill_id].clone(),
|
||||
font: self.fonts[font_id].clone(),
|
||||
border: self.borders[border_id].clone(),
|
||||
quote_prefix,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,8 +226,7 @@ impl Model {
|
||||
let style_index = self.workbook.styles.get_style_index_or_create(style);
|
||||
self.workbook
|
||||
.worksheet_mut(sheet)?
|
||||
.set_cell_style(row, column, style_index);
|
||||
Ok(())
|
||||
.set_cell_style(row, column, style_index)
|
||||
}
|
||||
|
||||
pub fn copy_cell_style(
|
||||
@@ -237,9 +241,7 @@ impl Model {
|
||||
|
||||
self.workbook
|
||||
.worksheet_mut(destination_cell.0)?
|
||||
.set_cell_style(destination_cell.1, destination_cell.2, source_style_index);
|
||||
|
||||
Ok(())
|
||||
.set_cell_style(destination_cell.1, destination_cell.2, source_style_index)
|
||||
}
|
||||
|
||||
/// Sets the style "style_name" in cell
|
||||
@@ -253,8 +255,7 @@ impl Model {
|
||||
let style_index = self.workbook.styles.get_style_index_by_name(style_name)?;
|
||||
self.workbook
|
||||
.worksheet_mut(sheet)?
|
||||
.set_cell_style(row, column, style_index);
|
||||
Ok(())
|
||||
.set_cell_style(row, column, style_index)
|
||||
}
|
||||
|
||||
pub fn set_sheet_style(&mut self, sheet: u32, style_name: &str) -> Result<(), String> {
|
||||
|
||||
Reference in New Issue
Block a user