Adding code documentation to base/src/actions.rs.

This commit is contained in:
fosdick.io
2024-02-06 13:08:10 -07:00
parent 67e8b0ce14
commit 47fafa1a4e
2 changed files with 104 additions and 43 deletions

View File

@@ -8,18 +8,36 @@ use crate::model::Model;
// I feel this is unimportant for now. // I feel this is unimportant for now.
impl Model { impl Model {
/// This function iterates over all cells in the model and shifts their formulas according to the displacement data.
///
/// # Arguments
///
/// * `displace_data` - A reference to `DisplaceData` describing the displacement's direction and magnitude.
fn displace_cells(&mut self, displace_data: &DisplaceData) { fn displace_cells(&mut self, displace_data: &DisplaceData) {
let cells = self.get_all_cells(); let cells = self.get_all_cells();
for cell in cells { for cell in cells {
self.shift_cell_formula(cell.index, cell.row, cell.column, displace_data); self.shift_cell_formula(cell.index, cell.row, cell.column, displace_data);
} }
} }
/// Returns the list of columns in row
/// Retrieves the column indices for a specific row in a given sheet, sorted in ascending or descending order.
///
/// # Arguments
///
/// * `sheet` - The sheet number to retrieve columns from.
/// * `row` - The row number to retrieve columns for.
/// * `descending` - If true, the columns are returned in descending order; otherwise, in ascending order.
///
/// # Returns
///
/// This function returns a `Result` containing either:
/// - `Ok(Vec<i32>)`: A vector of column indices for the specified row, sorted according to the `descending` flag.
/// - `Err(String)`: An error message if the sheet cannot be found.
fn get_columns_for_row( fn get_columns_for_row(
&self, &self,
sheet: u32, sheet: u32,
row: i32, row: i32,
descending: bool, descending: bool
) -> Result<Vec<i32>, String> { ) -> Result<Vec<i32>, String> {
let worksheet = self.workbook.worksheet(sheet)?; let worksheet = self.workbook.worksheet(sheet)?;
if let Some(row_data) = worksheet.sheet_data.get(&row) { if let Some(row_data) = worksheet.sheet_data.get(&row) {
@@ -34,17 +52,24 @@ impl Model {
} }
} }
/// Moves the contents of cell (source_row, source_column) tp (target_row, target_column) /// Moves the contents of cell (source_row, source_column) to (target_row, target_column).
///
/// # Arguments
///
/// * `sheet` - The sheet number to retrieve columns from.
/// * `source_row` - The row index of the cell's current location.
/// * `source_column` - The column index of the cell's current location.
/// * `target_row` - The row index of the cell's new location.
/// * `target_column` - The column index of the cell's new location.
fn move_cell( fn move_cell(
&mut self, &mut self,
sheet: u32, sheet: u32,
source_row: i32, source_row: i32,
source_column: i32, source_column: i32,
target_row: i32, target_row: i32,
target_column: i32, target_column: i32
) -> Result<(), String> { ) -> Result<(), String> {
let source_cell = self let source_cell = self.workbook
.workbook
.worksheet(sheet)? .worksheet(sheet)?
.cell(source_row, source_column) .cell(source_row, source_column)
.ok_or("Expected Cell to exist")?; .ok_or("Expected Cell to exist")?;
@@ -54,18 +79,25 @@ impl Model {
.cell_formula(sheet, source_row, source_column)? .cell_formula(sheet, source_row, source_column)?
.unwrap_or_else(|| source_cell.get_text(&self.workbook.shared_strings, &self.language)); .unwrap_or_else(|| source_cell.get_text(&self.workbook.shared_strings, &self.language));
self.set_user_input(sheet, target_row, target_column, formula_or_value); self.set_user_input(sheet, target_row, target_column, formula_or_value);
self.workbook self.workbook.worksheet_mut(sheet)?.set_cell_style(target_row, target_column, style);
.worksheet_mut(sheet)?
.set_cell_style(target_row, target_column, style);
self.delete_cell(sheet, source_row, source_column)?; self.delete_cell(sheet, source_row, source_column)?;
Ok(()) Ok(())
} }
/// Inserts one or more new columns into the model at the specified index.
///
/// This method shifts existing columns to the right to make space for the new columns.
///
/// # Arguments
///
/// * `sheet` - The sheet number to retrieve columns from.
/// * `column` - The index at which the new columns should be inserted.
/// * `column_count` - The number of columns to insert.
pub fn insert_columns( pub fn insert_columns(
&mut self, &mut self,
sheet: u32, sheet: u32,
column: i32, column: i32,
column_count: i32, column_count: i32
) -> Result<(), String> { ) -> Result<(), String> {
if column_count <= 0 { if column_count <= 0 {
return Err("Cannot add a negative number of cells :)".to_string()); return Err("Cannot add a negative number of cells :)".to_string());
@@ -75,8 +107,7 @@ impl Model {
let last_column = dimensions.max_column + column_count; let last_column = dimensions.max_column + column_count;
if last_column > LAST_COLUMN { if last_column > LAST_COLUMN {
return Err( return Err(
"Cannot shift cells because that would delete cells at the end of a row" "Cannot shift cells because that would delete cells at the end of a row".to_string()
.to_string(),
); );
} }
let worksheet = self.workbook.worksheet(sheet)?; let worksheet = self.workbook.worksheet(sheet)?;
@@ -94,20 +125,29 @@ impl Model {
} }
// Update all formulas in the workbook // Update all formulas in the workbook
self.displace_cells(&DisplaceData::Column { self.displace_cells(
&(DisplaceData::Column {
sheet, sheet,
column, column,
delta: column_count, delta: column_count,
}); })
);
Ok(()) Ok(())
} }
/// Deletes one or more columns from the model starting at the specified index.
///
/// # Arguments
///
/// * `sheet` - The sheet number to retrieve columns from.
/// * `column` - The index of the first column to delete.
/// * `count` - The number of columns to delete.
pub fn delete_columns( pub fn delete_columns(
&mut self, &mut self,
sheet: u32, sheet: u32,
column: i32, column: i32,
column_count: i32, column_count: i32
) -> Result<(), String> { ) -> Result<(), String> {
if column_count <= 0 { if column_count <= 0 {
return Err("Please use insert columns instead".to_string()); return Err("Please use insert columns instead".to_string());
@@ -133,15 +173,24 @@ impl Model {
} }
// Update all formulas in the workbook // Update all formulas in the workbook
self.displace_cells(&DisplaceData::Column { self.displace_cells(
&(DisplaceData::Column {
sheet, sheet,
column, column,
delta: -column_count, delta: -column_count,
}); })
);
Ok(()) Ok(())
} }
/// Inserts one or more new rows into the model at the specified index.
///
/// # Arguments
///
/// * `sheet` - The sheet number to retrieve columns from.
/// * `row` - The index at which the new rows should be inserted.
/// * `row_count` - The number of rows to insert.
pub fn insert_rows(&mut self, sheet: u32, row: i32, row_count: i32) -> Result<(), String> { pub fn insert_rows(&mut self, sheet: u32, row: i32, row_count: i32) -> Result<(), String> {
if row_count <= 0 { if row_count <= 0 {
return Err("Cannot add a negative number of cells :)".to_string()); return Err("Cannot add a negative number of cells :)".to_string());
@@ -151,8 +200,7 @@ impl Model {
let last_row = dimensions.max_row + row_count; let last_row = dimensions.max_row + row_count;
if last_row > LAST_ROW { if last_row > LAST_ROW {
return Err( return Err(
"Cannot shift cells because that would delete cells at the end of a column" "Cannot shift cells because that would delete cells at the end of a column".to_string()
.to_string(),
); );
} }
@@ -190,15 +238,24 @@ impl Model {
self.workbook.worksheets[sheet as usize].rows = new_rows; self.workbook.worksheets[sheet as usize].rows = new_rows;
// Update all formulas in the workbook // Update all formulas in the workbook
self.displace_cells(&DisplaceData::Row { self.displace_cells(
&(DisplaceData::Row {
sheet, sheet,
row, row,
delta: row_count, delta: row_count,
}); })
);
Ok(()) Ok(())
} }
/// Deletes one or more rows from the model starting at the specified index.
///
/// # Arguments
///
/// * `sheet` - The sheet number to retrieve columns from.
/// * `row` - The index of the first row to delete.
/// * `row_count` - The number of rows to delete.
pub fn delete_rows(&mut self, sheet: u32, row: i32, row_count: i32) -> Result<(), String> { pub fn delete_rows(&mut self, sheet: u32, row: i32, row_count: i32) -> Result<(), String> {
if row_count <= 0 { if row_count <= 0 {
return Err("Please use insert rows instead".to_string()); return Err("Please use insert rows instead".to_string());
@@ -242,11 +299,13 @@ impl Model {
} }
} }
self.workbook.worksheets[sheet as usize].rows = new_rows; self.workbook.worksheets[sheet as usize].rows = new_rows;
self.displace_cells(&DisplaceData::Row { self.displace_cells(
&(DisplaceData::Row {
sheet, sheet,
row, row,
delta: -row_count, delta: -row_count,
}); })
);
Ok(()) Ok(())
} }
@@ -266,7 +325,7 @@ impl Model {
&mut self, &mut self,
sheet: u32, sheet: u32,
column: i32, column: i32,
delta: i32, delta: i32
) -> Result<(), &'static str> { ) -> Result<(), &'static str> {
// Check boundaries // Check boundaries
let target_column = column + delta; let target_column = column + delta;
@@ -280,11 +339,13 @@ impl Model {
// TODO: Add the actual displacement of data and styles // TODO: Add the actual displacement of data and styles
// Update all formulas in the workbook // Update all formulas in the workbook
self.displace_cells(&DisplaceData::ColumnMove { self.displace_cells(
&(DisplaceData::ColumnMove {
sheet, sheet,
column, column,
delta, delta,
}); })
);
Ok(()) Ok(())
} }

View File

@@ -1,4 +1,4 @@
//! This cate reads an xlsx file and transforms it into an internal representation ([`Model`]). //! This crate reads an xlsx file and transforms it into an internal representation ([`Model`]).
//! An `xlsx` is a zip file containing a set of folders and `xml` files. The IronCalc json structure mimics the relevant parts of the Excel zip. //! An `xlsx` is a zip file containing a set of folders and `xml` files. The IronCalc json structure mimics the relevant parts of the Excel zip.
//! Although the xlsx structure is quite complicated, it's essentials regarding the spreadsheet technology are easier to grasp. //! Although the xlsx structure is quite complicated, it's essentials regarding the spreadsheet technology are easier to grasp.
//! //!