From e8965d9b4fc74616c13119081001f3a9c0d852e0 Mon Sep 17 00:00:00 2001 From: Thiago Ramos <99thiagodejesus@gmail.com> Date: Sat, 17 May 2025 13:44:16 -0300 Subject: [PATCH 1/6] refactor: adjust clippy formatting issues --- src/grid_events.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grid_events.rs b/src/grid_events.rs index 6cdf7be..758a614 100644 --- a/src/grid_events.rs +++ b/src/grid_events.rs @@ -58,7 +58,7 @@ pub struct ChangesEventValue { /// /// These functions: /// - Receive a reference to `ChangesEventValue` -pub type ChangesEventFn = Box () + Send + 'static + Sync>; +pub type ChangesEventFn = Box; /// Represents a registered event listener function. /// From 6ca1541f1d50da51f83032323c9b69515910bb70 Mon Sep 17 00:00:00 2001 From: Thiago Ramos <99thiagodejesus@gmail.com> Date: Sat, 17 May 2025 14:06:25 -0300 Subject: [PATCH 2/6] refactor: adjust clippy formatting issues --- examples/managing_grid.rs | 8 ++++---- src/error.rs | 6 +++--- src/grid_engine.rs | 35 ++++++++++++++++------------------- src/grid_events.rs | 12 ++---------- src/inner_grid.rs | 16 +++++++++------- src/node.rs | 4 ++-- 6 files changed, 36 insertions(+), 45 deletions(-) diff --git a/examples/managing_grid.rs b/examples/managing_grid.rs index fa15c3e..319065b 100644 --- a/examples/managing_grid.rs +++ b/examples/managing_grid.rs @@ -25,7 +25,7 @@ fn print_grid(grid: &GridEngine) { for i in 0..grid.get_inner_grid().cols() { grid_str_formatted.push_str(&format!(" {} ", i)); } - grid_str_formatted.push_str("\n"); + grid_str_formatted.push('\n'); grid.get_inner_grid() .iter_rows() @@ -35,16 +35,16 @@ fn print_grid(grid: &GridEngine) { if index == 0 { grid_str_formatted.push_str(&format!("{:0>2}", row_number)); } - return match cell { + match cell { Some(item) => { grid_str_formatted.push_str(&format!("[{}]", item)); } None => { - grid_str_formatted.push_str(&format!("[{}]", " ".repeat(1))); + grid_str_formatted.push_str(&format!("[{}]", " ")); } }; }); - grid_str_formatted.push_str("\n"); + grid_str_formatted.push('\n'); }); println!("{}", grid_str_formatted); diff --git a/src/error.rs b/src/error.rs index 742d666..a3ef218 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,14 +3,14 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum GridEngineError { #[error(transparent)] - InnerGridError(#[from] InnerGridError), + InnerGrid(#[from] InnerGridError), #[error(transparent)] - ItemError(#[from] ItemError), + Item(#[from] ItemError), // Temporary error for unhandled errors, must be removed and all errors should be handled #[error("UnhandledError: {0}")] - UnhandledError(Box), + Unhandled(Box), } #[derive(Error, Debug)] diff --git a/src/grid_engine.rs b/src/grid_engine.rs index 9044060..ab1a407 100644 --- a/src/grid_engine.rs +++ b/src/grid_engine.rs @@ -47,7 +47,7 @@ use crate::error::{GridEngineError, InnerGridError, ItemError}; use crate::grid_events::{ChangesEventValue, GridEvents}; use crate::inner_grid::{InnerGrid, UpdateGridOperation}; use crate::node::Node; -use crate::utils::{for_cell, ForCellArgs}; +use crate::utils::{ForCellArgs, for_cell}; use std::{collections::BTreeMap, fmt::Debug}; /// Represents data for an item addition change @@ -132,8 +132,7 @@ impl GridEngine { } fn new_node(&mut self, id: String, x: usize, y: usize, w: usize, h: usize) -> Node { - let node = Node::new(id, x, y, w, h); - node + Node::new(id, x, y, w, h) } fn create_add_change(&mut self, node: Node) { @@ -196,8 +195,8 @@ impl GridEngine { w: usize, h: usize, ) -> Result<&Node, GridEngineError> { - if self.items.get(&id).is_some() { - return Err(GridEngineError::ItemError(ItemError::ItemAlreadyExists { + if self.items.contains_key(&id) { + return Err(GridEngineError::Item(ItemError::ItemAlreadyExists { id: id.clone(), })); }; @@ -216,7 +215,7 @@ impl GridEngine { .items .get(&node_id) .ok_or(InnerGridError::MismatchedGridItem { id: node_id })?; - Ok(&node) + Ok(node) } fn create_remove_change(&mut self, node: &Node) { @@ -248,7 +247,7 @@ impl GridEngine { pub fn remove_item(&mut self, id: &str) -> Result { let node = match self.items.get(id) { Some(node) => node, - None => Err(GridEngineError::ItemError(ItemError::ItemNotFound { + None => Err(GridEngineError::Item(ItemError::ItemNotFound { id: id.to_string(), }))?, } @@ -301,7 +300,7 @@ impl GridEngine { )?; if !collides_with.contains(&node) { - collides_with.push(&node); + collides_with.push(node); } } } @@ -423,7 +422,7 @@ impl GridEngine { ) -> Result<(), GridEngineError> { let node = match self.items.get(id) { Some(node) => node, - None => Err(GridEngineError::ItemError(ItemError::ItemNotFound { + None => Err(GridEngineError::Item(ItemError::ItemNotFound { id: id.to_string(), }))?, }; @@ -454,7 +453,7 @@ impl GridEngine { /// * `Ok(())` - If all changes were applied successfully /// * `Err(GridEngineError)` - If any change application fails /// ``` - fn apply_changes(&mut self, changes: &Vec) -> Result<(), GridEngineError> { + fn apply_changes(&mut self, changes: &[Change]) -> Result<(), GridEngineError> { for change in changes.iter() { match &change { Change::Add(data) => { @@ -485,7 +484,7 @@ impl GridEngine { } self.events.trigger_changes_event(&ChangesEventValue { - changes: changes.iter().map(|change| change.clone()).collect(), + changes: changes.to_vec(), }); Ok(()) } @@ -705,28 +704,26 @@ mod tests { assert!( engine .will_collides_with( - &engine.items.get(&item_0_id).unwrap(), + engine.items.get(&item_0_id).unwrap(), 0, 0, &mut engine.grid.clone() ) .unwrap() - .len() - == 0 + .is_empty() ); // Asserts that does not collide with empty position assert!( engine .will_collides_with( - &engine.items.get(&item_0_id).unwrap(), + engine.items.get(&item_0_id).unwrap(), 2, 2, &mut engine.grid.clone() ) .unwrap() - .len() - == 0 + .is_empty() ); // Asserts that collide with occupied position @@ -736,7 +733,7 @@ mod tests { assert!( engine .will_collides_with( - &engine.items.get(&item_0_id).unwrap(), + engine.items.get(&item_0_id).unwrap(), 1, 2, &mut engine.grid.clone() @@ -750,7 +747,7 @@ mod tests { assert!( engine .will_collides_with( - &engine.items.get(&item_0_id).unwrap(), + engine.items.get(&item_0_id).unwrap(), 1, 1, &mut engine.grid.clone() diff --git a/src/grid_events.rs b/src/grid_events.rs index 758a614..6a12626 100644 --- a/src/grid_events.rs +++ b/src/grid_events.rs @@ -84,7 +84,7 @@ impl Debug for ListenerFunction { /// `GridEvents` manages a collection of event listeners that are notified /// whenever changes occur in the grid. It provides methods to register /// and remove listeners, as well as trigger events when changes happen. -#[derive(Debug)] +#[derive(Debug, Default)] pub struct GridEvents { /// Collection of registered change event listeners changes_listeners: Vec, @@ -161,14 +161,6 @@ impl GridEvents { } } -impl Default for GridEvents { - fn default() -> Self { - Self { - changes_listeners: Vec::new(), - } - } -} - #[cfg(test)] mod tests { use super::*; @@ -259,7 +251,7 @@ mod tests { events.trigger_changes_event(&event); let received = received_changes.lock().unwrap(); - let received_change = received.get(0).unwrap(); + let received_change = received.first().unwrap(); assert_eq!(received_change, &change); } } diff --git a/src/inner_grid.rs b/src/inner_grid.rs index de0c194..2723e06 100644 --- a/src/inner_grid.rs +++ b/src/inner_grid.rs @@ -28,9 +28,9 @@ //! The grid automatically expands vertically when needed, allowing for //! flexible layout management while maintaining horizontal constraints. +use crate::{error::InnerGridError, node::Node}; use grid::Grid; use std::ops::{Deref, DerefMut}; -use crate::{error::InnerGridError, node::Node}; /// Operation to perform when updating the grid. #[derive(Debug, Clone, Copy)] @@ -134,7 +134,7 @@ impl InnerGrid { self.handle_expansion(x, y); } - return self.inner.get(y, x); + self.inner.get(y, x) } pub(crate) fn get_mut(&mut self, x: usize, y: usize) -> Option<&mut Option> { @@ -142,7 +142,7 @@ impl InnerGrid { self.handle_expansion(x, y); } - return self.inner.get_mut(y, x); + self.inner.get_mut(y, x) } /// Updates a cell in the grid based on the specified operation. @@ -238,8 +238,9 @@ mod tests { }; // First add the node - grid.get_mut(1, 1) - .map(|cell| *cell = Some("test_node".to_string())); + if let Some(cell) = grid.get_mut(1, 1) { + *cell = Some("test_node".to_string()); + } // Then remove it grid.update(&node, 1, 1, UpdateGridOperation::Remove) @@ -260,8 +261,9 @@ mod tests { }; // Add a different node's ID - grid.get_mut(1, 1) - .map(|cell| *cell = Some("different_node".to_string())); + if let Some(cell) = grid.get_mut(1, 1) { + *cell = Some("different_node".to_string()); + } // Try to remove our node grid.update(&node, 1, 1, UpdateGridOperation::Remove) diff --git a/src/node.rs b/src/node.rs index b4caa7a..65279a1 100644 --- a/src/node.rs +++ b/src/node.rs @@ -27,7 +27,7 @@ use crate::{ error::InnerGridError, inner_grid::{InnerGrid, UpdateGridOperation}, - utils::{for_cell, ForCellArgs}, + utils::{ForCellArgs, for_cell}, }; /// Represents an item in the grid with position and dimensions. @@ -116,7 +116,7 @@ impl Node { update_operation: UpdateGridOperation, ) -> Result<(), InnerGridError> { self.for_cell(&mut |x, y| { - return grid.update(self, x, y, update_operation); + grid.update(self, x, y, update_operation) })?; Ok(()) From 1c531c8bcbba93bf2102d25a122c9e06ce67fdf4 Mon Sep 17 00:00:00 2001 From: Thiago Ramos <99thiagodejesus@gmail.com> Date: Sat, 17 May 2025 14:10:26 -0300 Subject: [PATCH 3/6] chore: update PR flow name --- .github/workflows/pull_request.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 538b903..166c451 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -1,4 +1,4 @@ -name: Rust +name: Pull Request on: pull_request: From 493717e513b28efbd35c2969c38ca8de28ae139b Mon Sep 17 00:00:00 2001 From: Thiago Ramos <99thiagodejesus@gmail.com> Date: Sat, 17 May 2025 14:13:01 -0300 Subject: [PATCH 4/6] refactor: apply lint --- src/node.rs | 4 +-- src/utils.rs | 88 ++++++++++++++++++++++++++++------------------------ 2 files changed, 49 insertions(+), 43 deletions(-) diff --git a/src/node.rs b/src/node.rs index 65279a1..88b8fdd 100644 --- a/src/node.rs +++ b/src/node.rs @@ -115,9 +115,7 @@ impl Node { grid: &mut InnerGrid, update_operation: UpdateGridOperation, ) -> Result<(), InnerGridError> { - self.for_cell(&mut |x, y| { - grid.update(self, x, y, update_operation) - })?; + self.for_cell(&mut |x, y| grid.update(self, x, y, update_operation))?; Ok(()) } diff --git a/src/utils.rs b/src/utils.rs index abd946a..8be2e1b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -104,38 +104,44 @@ mod tests { fn test_for_cell_handles_zero_dimensions() { let mut callback = |_x, _y| Ok(()); - assert!(for_cell( - ForCellArgs { - x: 0, - y: 0, - w: 0, - h: 1 - }, - &mut callback - ) - .is_ok()); + assert!( + for_cell( + ForCellArgs { + x: 0, + y: 0, + w: 0, + h: 1 + }, + &mut callback + ) + .is_ok() + ); - assert!(for_cell( - ForCellArgs { - x: 0, - y: 0, - w: 1, - h: 0 - }, - &mut callback - ) - .is_ok()); + assert!( + for_cell( + ForCellArgs { + x: 0, + y: 0, + w: 1, + h: 0 + }, + &mut callback + ) + .is_ok() + ); - assert!(for_cell( - ForCellArgs { - x: 0, - y: 0, - w: 0, - h: 0 - }, - &mut callback - ) - .is_ok()); + assert!( + for_cell( + ForCellArgs { + x: 0, + y: 0, + w: 0, + h: 0 + }, + &mut callback + ) + .is_ok() + ); } #[test] @@ -148,15 +154,17 @@ mod tests { } }; - assert!(for_cell( - ForCellArgs { - x: 1, - y: 1, - w: 2, - h: 1 - }, - &mut callback - ) - .is_err()); + assert!( + for_cell( + ForCellArgs { + x: 1, + y: 1, + w: 2, + h: 1 + }, + &mut callback + ) + .is_err() + ); } } From b1dc084fb724ceba713fd4e67c4b08193e92072a Mon Sep 17 00:00:00 2001 From: Thiago Ramos <99thiagodejesus@gmail.com> Date: Sun, 18 May 2025 15:46:20 -0300 Subject: [PATCH 5/6] chore: update ci --- .github/workflows/pull_request.yaml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 166c451..3eeaf97 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -8,11 +8,20 @@ env: CARGO_TERM_COLOR: always jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install Rust + uses: actions-rust-lang/setup-rust-toolchain@v1 + - name: Build + run: cargo build --verbose formatting: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: actions-rust-lang/setup-rust-toolchain@v1 + - name: Install Rust + uses: actions-rust-lang/setup-rust-toolchain@v1 with: components: rustfmt - uses: actions-rust-lang/rustfmt@v1 From 8d87a77d3bbcca99fb0fd8569296ae35e2d6ecc7 Mon Sep 17 00:00:00 2001 From: Thiago Ramos <99thiagodejesus@gmail.com> Date: Sun, 18 May 2025 15:49:38 -0300 Subject: [PATCH 6/6] chore: update ci --- .github/workflows/pull_request.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 3eeaf97..e239752 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -25,7 +25,7 @@ jobs: with: components: rustfmt - uses: actions-rust-lang/rustfmt@v1 - - run: cargo clippy --all-targets --all-features + - run: cargo clippy --all-features --all-targets tests: runs-on: ubuntu-latest steps: