chore(lint): fix clippy warnings

This commit is contained in:
Kristofers Solo 2024-12-08 19:45:43 +02:00
parent e59271cf01
commit dcc68ffc00
8 changed files with 81 additions and 47 deletions

10
Cargo.lock generated
View File

@ -2063,7 +2063,7 @@ checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df"
[[package]] [[package]]
name = "hexlab" name = "hexlab"
version = "0.1.4" version = "0.1.5"
dependencies = [ dependencies = [
"bevy", "bevy",
"hexx", "hexx",
@ -2251,9 +2251,9 @@ dependencies = [
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.161" version = "0.2.167"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc"
[[package]] [[package]]
name = "libloading" name = "libloading"
@ -3548,9 +3548,9 @@ dependencies = [
[[package]] [[package]]
name = "ttf-parser" name = "ttf-parser"
version = "0.25.0" version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5902c5d130972a0000f60860bfbf46f7ca3db5391eddfedd1b8728bd9dc96c0e" checksum = "d2df906b07856748fa3f6e0ad0cbaa047052d4a7dd609e231c4f72cee8c36f31"
[[package]] [[package]]
name = "twox-hash" name = "twox-hash"

View File

@ -1,7 +1,7 @@
[package] [package]
name = "hexlab" name = "hexlab"
authors = ["Kristofers Solo <dev@kristofers.xyz>"] authors = ["Kristofers Solo <dev@kristofers.xyz>"]
version = "0.1.4" version = "0.1.5"
edition = "2021" edition = "2021"
description = "A hexagonal maze generation and manipulation library" description = "A hexagonal maze generation and manipulation library"
repository = "https://github.com/kristoferssolo/hexlab" repository = "https://github.com/kristoferssolo/hexlab"
@ -46,3 +46,9 @@ panic = "abort" # Smaller binary size
[package.metadata.docs.rs] [package.metadata.docs.rs]
all-features = true all-features = true
rustdoc-args = ["--cfg", "docsrs"] rustdoc-args = ["--cfg", "docsrs"]
[lints.clippy]
pedantic = "warn"
nursery = "warn"
unwrap_used = "warn"
expect_used = "warn"

View File

@ -76,6 +76,7 @@ pub enum MazeBuilderError {
/// .build() /// .build()
/// .expect("Failed to create maze"); /// .expect("Failed to create maze");
/// ``` /// ```
#[allow(clippy::module_name_repetitions)]
#[derive(Default)] #[derive(Default)]
pub struct MazeBuilder { pub struct MazeBuilder {
radius: Option<u32>, radius: Option<u32>,
@ -87,6 +88,7 @@ pub struct MazeBuilder {
impl MazeBuilder { impl MazeBuilder {
/// Creates a new [`MazeBuilder`] instance. /// Creates a new [`MazeBuilder`] instance.
#[inline] #[inline]
#[must_use]
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
} }
@ -97,7 +99,8 @@ impl MazeBuilder {
/// ///
/// * `radius` - The size of the maze (number of tiles along one edge). /// * `radius` - The size of the maze (number of tiles along one edge).
#[inline] #[inline]
pub fn with_radius(mut self, radius: u32) -> Self { #[must_use]
pub const fn with_radius(mut self, radius: u32) -> Self {
self.radius = Some(radius); self.radius = Some(radius);
self self
} }
@ -108,7 +111,8 @@ impl MazeBuilder {
/// ///
/// * `seed` - The random seed value. /// * `seed` - The random seed value.
#[inline] #[inline]
pub fn with_seed(mut self, seed: u64) -> Self { #[must_use]
pub const fn with_seed(mut self, seed: u64) -> Self {
self.seed = Some(seed); self.seed = Some(seed);
self self
} }
@ -121,13 +125,15 @@ impl MazeBuilder {
/// ///
/// * `generator_type` - The maze generation algorithm to use. /// * `generator_type` - The maze generation algorithm to use.
#[inline] #[inline]
pub fn with_generator(mut self, generator_type: GeneratorType) -> Self { #[must_use]
pub const fn with_generator(mut self, generator_type: GeneratorType) -> Self {
self.generator_type = generator_type; self.generator_type = generator_type;
self self
} }
#[inline] #[inline]
pub fn with_start_position(mut self, pos: Hex) -> Self { #[must_use]
pub const fn with_start_position(mut self, pos: Hex) -> Self {
self.start_position = Some(pos); self.start_position = Some(pos);
self self
} }
@ -159,7 +165,7 @@ impl MazeBuilder {
/// ``` /// ```
pub fn build(self) -> Result<HexMaze, MazeBuilderError> { pub fn build(self) -> Result<HexMaze, MazeBuilderError> {
let radius = self.radius.ok_or(MazeBuilderError::NoRadius)?; let radius = self.radius.ok_or(MazeBuilderError::NoRadius)?;
let mut maze = self.create_hex_maze(radius); let mut maze = create_hex_maze(radius);
if let Some(start_pos) = self.start_position { if let Some(start_pos) = self.start_position {
if maze.get_tile(&start_pos).is_none() { if maze.get_tile(&start_pos).is_none() {
@ -174,29 +180,29 @@ impl MazeBuilder {
Ok(maze) Ok(maze)
} }
fn create_hex_maze(&self, radius: u32) -> HexMaze {
let mut maze = HexMaze::new();
let radius = radius as i32;
for q in -radius..=radius {
let r1 = (-radius).max(-q - radius);
let r2 = radius.min(-q + radius);
for r in r1..=r2 {
let pos = Hex::new(q, r);
maze.add_tile(pos);
}
}
maze
}
fn generate_maze(&self, maze: &mut HexMaze) { fn generate_maze(&self, maze: &mut HexMaze) {
match self.generator_type { match self.generator_type {
GeneratorType::RecursiveBacktracking => { GeneratorType::RecursiveBacktracking => {
generate_backtracking(maze, self.start_position, self.seed) generate_backtracking(maze, self.start_position, self.seed);
} }
} }
} }
} }
fn create_hex_maze(radius: u32) -> HexMaze {
let mut maze = HexMaze::new();
let radius = i32::try_from(radius).unwrap_or(5);
for q in -radius..=radius {
let r1 = (-radius).max(-q - radius);
let r2 = radius.min(-q + radius);
for r in r1..=r2 {
let pos = Hex::new(q, r);
maze.add_tile(pos);
}
}
maze
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {

View File

@ -6,13 +6,14 @@ use rand_chacha::ChaCha8Rng;
use crate::HexMaze; use crate::HexMaze;
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]
pub enum GeneratorType { pub enum GeneratorType {
#[default] #[default]
RecursiveBacktracking, RecursiveBacktracking,
} }
pub(crate) fn generate_backtracking(maze: &mut HexMaze, start_pos: Option<Hex>, seed: Option<u64>) { pub fn generate_backtracking(maze: &mut HexMaze, start_pos: Option<Hex>, seed: Option<u64>) {
if maze.is_empty() { if maze.is_empty() {
return; return;
} }
@ -21,10 +22,10 @@ pub(crate) fn generate_backtracking(maze: &mut HexMaze, start_pos: Option<Hex>,
let mut visited = HashSet::new(); let mut visited = HashSet::new();
let mut rng: Box<dyn RngCore> = match seed { let mut rng: Box<dyn RngCore> = seed.map_or_else(
Some(seed) => Box::new(ChaCha8Rng::seed_from_u64(seed)), || Box::new(thread_rng()) as Box<dyn RngCore>,
None => Box::new(thread_rng()), |seed| Box::new(ChaCha8Rng::seed_from_u64(seed)) as Box<dyn RngCore>,
}; );
recursive_backtrack(maze, start, &mut visited, &mut rng); recursive_backtrack(maze, start, &mut visited, &mut rng);
} }

View File

@ -15,6 +15,7 @@ pub struct HexMaze(HashMap<Hex, HexTile>);
impl HexMaze { impl HexMaze {
/// Creates a new empty maze /// Creates a new empty maze
#[inline] #[inline]
#[must_use]
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
} }
@ -28,29 +29,32 @@ impl HexMaze {
/// Adds a wall in the specified direction at the given coordinates /// Adds a wall in the specified direction at the given coordinates
pub fn add_wall(&mut self, coord: Hex, direction: EdgeDirection) { pub fn add_wall(&mut self, coord: Hex, direction: EdgeDirection) {
if let Some(tile) = self.0.get_mut(&coord) { if let Some(tile) = self.0.get_mut(&coord) {
tile.walls.add(direction) tile.walls.add(direction);
} }
} }
/// Returns a reference to the tile at the specified coordinates /// Returns a reference to the tile at the specified coordinates
#[inline] #[inline]
#[must_use]
pub fn get_tile(&self, coord: &Hex) -> Option<&HexTile> { pub fn get_tile(&self, coord: &Hex) -> Option<&HexTile> {
self.0.get(coord) self.0.get(coord)
} }
/// Returns a reference to the walls at the specified coordinates /// Returns a reference to the walls at the specified coordinates
pub fn get_walls(&self, coord: &Hex) -> Option<&Walls> { pub fn get_walls(&self, coord: &Hex) -> Option<&Walls> {
self.0.get(coord).map(|tile| tile.walls()) self.0.get(coord).map(HexTile::walls)
} }
/// Returns the number of tiles in the maze /// Returns the number of tiles in the maze
#[inline] #[inline]
#[must_use]
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.0.len() self.0.len()
} }
/// Returns true if the maze is empty /// Returns true if the maze is empty
#[inline] #[inline]
#[must_use]
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.0.is_empty() self.0.is_empty()
} }

View File

@ -21,6 +21,7 @@ pub struct HexTile {
impl HexTile { impl HexTile {
/// Creates a new tile with pos and default walls /// Creates a new tile with pos and default walls
#[must_use]
pub fn new(pos: Hex) -> Self { pub fn new(pos: Hex) -> Self {
Self { Self {
pos, pos,
@ -30,22 +31,28 @@ impl HexTile {
/// Returns a reference to the tile's walls /// Returns a reference to the tile's walls
#[inline] #[inline]
pub fn walls(&self) -> &Walls { #[must_use]
pub const fn walls(&self) -> &Walls {
&self.walls &self.walls
} }
/// Returns position of the tile /// Returns position of the tile
#[inline] #[inline]
pub fn pos(&self) -> Hex { #[must_use]
pub const fn pos(&self) -> Hex {
self.pos self.pos
} }
#[cfg(feature = "bevy")] #[cfg(feature = "bevy")]
#[inline]
#[must_use]
pub fn to_vec2(&self, layout: &HexLayout) -> Vec2 { pub fn to_vec2(&self, layout: &HexLayout) -> Vec2 {
layout.hex_to_world_pos(self.pos) layout.hex_to_world_pos(self.pos)
} }
#[cfg(feature = "bevy")] #[cfg(feature = "bevy")]
#[inline]
#[must_use]
pub fn to_vec3(&self, layout: &HexLayout) -> Vec3 { pub fn to_vec3(&self, layout: &HexLayout) -> Vec3 {
let pos = self.to_vec2(layout); let pos = self.to_vec2(layout);
Vec3::new(pos.x, 0., pos.y) Vec3::new(pos.x, 0., pos.y)

View File

@ -1,13 +1,13 @@
mod builder; mod builder;
mod generator; mod generator;
mod maze; mod hex_maze;
mod tile; mod hex_tile;
mod walls; mod walls;
pub use builder::{MazeBuilder, MazeBuilderError}; pub use builder::{MazeBuilder, MazeBuilderError};
pub use generator::GeneratorType; pub use generator::GeneratorType;
pub use maze::HexMaze; pub use hex_maze::HexMaze;
pub use tile::HexTile; pub use hex_tile::HexTile;
pub use walls::Walls; pub use walls::Walls;
pub mod prelude { pub mod prelude {

View File

@ -62,6 +62,8 @@ impl Walls {
/// assert!(walls.is_closed()); /// assert!(walls.is_closed());
/// assert_eq!(walls.count(), 6); /// assert_eq!(walls.count(), 6);
/// ``` /// ```
#[inline]
#[must_use]
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
} }
@ -77,7 +79,9 @@ impl Walls {
/// assert!(walls.is_empty()); /// assert!(walls.is_empty());
/// assert_eq!(walls.count(), 0); /// assert_eq!(walls.count(), 0);
/// ``` /// ```
pub fn empty() -> Self { #[inline]
#[must_use]
pub const fn empty() -> Self {
Self(0) Self(0)
} }
@ -95,7 +99,9 @@ impl Walls {
/// let walls = Walls::new(); /// let walls = Walls::new();
/// assert!(!walls.is_empty()); /// assert!(!walls.is_empty());
/// ``` /// ```
pub fn is_empty(&self) -> bool { #[inline]
#[must_use]
pub const fn is_empty(&self) -> bool {
self.0 == 0 self.0 == 0
} }
@ -193,7 +199,8 @@ impl Walls {
/// assert_eq!(walls.as_bits(), 0b111111); /// assert_eq!(walls.as_bits(), 0b111111);
/// ``` /// ```
#[inline] #[inline]
pub fn as_bits(&self) -> u8 { #[must_use]
pub const fn as_bits(&self) -> u8 {
self.0 self.0
} }
@ -215,8 +222,9 @@ impl Walls {
/// assert_eq!(walls.count(), 2); /// assert_eq!(walls.count(), 2);
/// ``` /// ```
#[inline] #[inline]
#[must_use]
pub fn count(&self) -> u8 { pub fn count(&self) -> u8 {
self.0.count_ones() as u8 u8::try_from(self.0.count_ones()).unwrap_or_default()
} }
/// Returns all possible directions as a `Walls` value /// Returns all possible directions as a `Walls` value
@ -234,8 +242,9 @@ impl Walls {
/// assert!(all_walls.is_closed()); /// assert!(all_walls.is_closed());
/// ``` /// ```
#[inline] #[inline]
pub fn all_directions() -> Self { #[must_use]
Self(0b111111) pub const fn all_directions() -> Self {
Self(0b11_1111)
} }
/// Toggles a wall in the specified direction. /// Toggles a wall in the specified direction.
@ -293,6 +302,7 @@ impl Walls {
/// assert!(walls.is_closed()); /// assert!(walls.is_closed());
/// ``` /// ```
#[inline] #[inline]
#[must_use]
pub fn is_closed(&self) -> bool { pub fn is_closed(&self) -> bool {
self.count() == 6 self.count() == 6
} }
@ -354,7 +364,7 @@ impl<const N: usize> From<[EdgeDirection; N]> for Walls {
impl Default for Walls { impl Default for Walls {
fn default() -> Self { fn default() -> Self {
Self(0b111111) Self(0b11_1111)
} }
} }