From 47fafa1a4ed0ee4fcb249d70a72b54f2a5f56d79 Mon Sep 17 00:00:00 2001 From: "fosdick.io" <67963637+fosdickio@users.noreply.github.com> Date: Tue, 6 Feb 2024 13:08:10 -0700 Subject: [PATCH 1/3] Adding code documentation to base/src/actions.rs. --- base/src/actions.rs | 145 +++++++++++++++++++++++++++++++------------- xlsx/src/lib.rs | 2 +- 2 files changed, 104 insertions(+), 43 deletions(-) diff --git a/base/src/actions.rs b/base/src/actions.rs index e93709e..ffced33 100644 --- a/base/src/actions.rs +++ b/base/src/actions.rs @@ -1,4 +1,4 @@ -use crate::constants::{LAST_COLUMN, LAST_ROW}; +use crate::constants::{ LAST_COLUMN, LAST_ROW }; use crate::expressions::parser::stringify::DisplaceData; use crate::model::Model; @@ -8,18 +8,36 @@ use crate::model::Model; // I feel this is unimportant for now. 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) { let cells = self.get_all_cells(); for cell in cells { 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)`: 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( &self, sheet: u32, row: i32, - descending: bool, + descending: bool ) -> Result, String> { let worksheet = self.workbook.worksheet(sheet)?; 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( &mut self, sheet: u32, source_row: i32, source_column: i32, target_row: i32, - target_column: i32, + target_column: i32 ) -> Result<(), String> { - let source_cell = self - .workbook + let source_cell = self.workbook .worksheet(sheet)? .cell(source_row, source_column) .ok_or("Expected Cell to exist")?; @@ -54,18 +79,25 @@ impl Model { .cell_formula(sheet, source_row, source_column)? .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.workbook - .worksheet_mut(sheet)? - .set_cell_style(target_row, target_column, style); + self.workbook.worksheet_mut(sheet)?.set_cell_style(target_row, target_column, style); self.delete_cell(sheet, source_row, source_column)?; 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( &mut self, sheet: u32, column: i32, - column_count: i32, + column_count: i32 ) -> Result<(), String> { if column_count <= 0 { 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; if last_column > LAST_COLUMN { return Err( - "Cannot shift cells because that would delete cells at the end of a row" - .to_string(), + "Cannot shift cells because that would delete cells at the end of a row".to_string() ); } let worksheet = self.workbook.worksheet(sheet)?; @@ -94,20 +125,29 @@ impl Model { } // Update all formulas in the workbook - self.displace_cells(&DisplaceData::Column { - sheet, - column, - delta: column_count, - }); + self.displace_cells( + &(DisplaceData::Column { + sheet, + column, + delta: column_count, + }) + ); 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( &mut self, sheet: u32, column: i32, - column_count: i32, + column_count: i32 ) -> Result<(), String> { if column_count <= 0 { return Err("Please use insert columns instead".to_string()); @@ -133,15 +173,24 @@ impl Model { } // Update all formulas in the workbook - self.displace_cells(&DisplaceData::Column { - sheet, - column, - delta: -column_count, - }); + self.displace_cells( + &(DisplaceData::Column { + sheet, + column, + delta: -column_count, + }) + ); 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> { if row_count <= 0 { 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; if last_row > LAST_ROW { return Err( - "Cannot shift cells because that would delete cells at the end of a column" - .to_string(), + "Cannot shift cells because that would delete cells at the end of a column".to_string() ); } @@ -190,15 +238,24 @@ impl Model { self.workbook.worksheets[sheet as usize].rows = new_rows; // Update all formulas in the workbook - self.displace_cells(&DisplaceData::Row { - sheet, - row, - delta: row_count, - }); + self.displace_cells( + &(DisplaceData::Row { + sheet, + row, + delta: row_count, + }) + ); 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> { if row_count <= 0 { 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.displace_cells(&DisplaceData::Row { - sheet, - row, - delta: -row_count, - }); + self.displace_cells( + &(DisplaceData::Row { + sheet, + row, + delta: -row_count, + }) + ); Ok(()) } @@ -266,7 +325,7 @@ impl Model { &mut self, sheet: u32, column: i32, - delta: i32, + delta: i32 ) -> Result<(), &'static str> { // Check boundaries let target_column = column + delta; @@ -280,11 +339,13 @@ impl Model { // TODO: Add the actual displacement of data and styles // Update all formulas in the workbook - self.displace_cells(&DisplaceData::ColumnMove { - sheet, - column, - delta, - }); + self.displace_cells( + &(DisplaceData::ColumnMove { + sheet, + column, + delta, + }) + ); Ok(()) } diff --git a/xlsx/src/lib.rs b/xlsx/src/lib.rs index 39789da..e2f90e3 100644 --- a/xlsx/src/lib.rs +++ b/xlsx/src/lib.rs @@ -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. //! Although the xlsx structure is quite complicated, it's essentials regarding the spreadsheet technology are easier to grasp. //! From 8f1fc2fa65015468bafe87d76adbb81b73b4b963 Mon Sep 17 00:00:00 2001 From: "fosdick.io" <67963637+fosdickio@users.noreply.github.com> Date: Tue, 6 Feb 2024 14:00:04 -0700 Subject: [PATCH 2/3] Adding GitHub Actions for building/testing and deploying cargo docs to GitHub Pages. --- .github/workflows/deploy-cargo-docs.yaml | 29 ++++++++++++++++++++++++ .github/workflows/rust-build-test.yaml | 26 +++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 .github/workflows/deploy-cargo-docs.yaml create mode 100644 .github/workflows/rust-build-test.yaml diff --git a/.github/workflows/deploy-cargo-docs.yaml b/.github/workflows/deploy-cargo-docs.yaml new file mode 100644 index 0000000..9f6a2c6 --- /dev/null +++ b/.github/workflows/deploy-cargo-docs.yaml @@ -0,0 +1,29 @@ +name: Deploy Docs to GitHub Pages + +on: + push: + branches: + - main + +jobs: + deploy-docs: + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v2 + + - name: Install Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + profile: minimal + override: true + + - name: Generate Documentation + run: cargo doc --no-deps + + - name: Deploy to GitHub Pages + uses: peaceiris/actions-gh-pages@v3 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: ./target/doc diff --git a/.github/workflows/rust-build-test.yaml b/.github/workflows/rust-build-test.yaml new file mode 100644 index 0000000..71a161a --- /dev/null +++ b/.github/workflows/rust-build-test.yaml @@ -0,0 +1,26 @@ +name: Rust + +on: + push: + branches: ["main"] + pull_request: + branches: ["main"] + +env: + CARGO_TERM_COLOR: always + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Build + run: cargo build --release --verbose + + - name: Run tests + run: cargo test --verbose + + - name: Linting + run: make lint From ec85eb7cf22ce6d6bbfeccc1eeeddeefbd0b176d Mon Sep 17 00:00:00 2001 From: "fosdick.io" <67963637+fosdickio@users.noreply.github.com> Date: Wed, 7 Feb 2024 10:22:05 -0700 Subject: [PATCH 3/3] Applying @nhatcher's review feedback. --- .github/workflows/deploy-cargo-docs.yaml | 2 +- .github/workflows/rust-build-test.yaml | 7 ++----- Makefile | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/.github/workflows/deploy-cargo-docs.yaml b/.github/workflows/deploy-cargo-docs.yaml index 9f6a2c6..34f8362 100644 --- a/.github/workflows/deploy-cargo-docs.yaml +++ b/.github/workflows/deploy-cargo-docs.yaml @@ -20,7 +20,7 @@ jobs: override: true - name: Generate Documentation - run: cargo doc --no-deps + run: make docs - name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v3 diff --git a/.github/workflows/rust-build-test.yaml b/.github/workflows/rust-build-test.yaml index 71a161a..468b711 100644 --- a/.github/workflows/rust-build-test.yaml +++ b/.github/workflows/rust-build-test.yaml @@ -19,8 +19,5 @@ jobs: - name: Build run: cargo build --release --verbose - - name: Run tests - run: cargo test --verbose - - - name: Linting - run: make lint + - name: Linting and testing + run: make tests diff --git a/Makefile b/Makefile index ecfc3f7..ac31afb 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ format: cargo fmt tests: lint - cargo test + cargo test --verbose clean: cargo clean