[core] Address clippy suggestions; clean up unit tests

In coordinates.rs:

- Add some [must_use] decorators to some getters
- Rewrite some unit tests to remove the .expect() and use ? instead
This commit is contained in:
Eryn Wells 2024-04-25 08:05:07 -07:00
parent 1b63f56042
commit a2d0c638d0

View file

@ -59,6 +59,7 @@ macro_rules! range_bound_struct {
} }
impl $type { impl $type {
#[must_use]
$vis fn new(x: $repr) -> Option<Self> { $vis fn new(x: $repr) -> Option<Self> {
if x < $max { if x < $max {
Some(Self(x)) Some(Self(x))
@ -73,14 +74,21 @@ macro_rules! range_bound_struct {
/// ///
/// This function does not perform any bounds checking. It should only be called when /// This function does not perform any bounds checking. It should only be called when
/// the input is already known to be within bounds, i.e. when `x >= Self::FIRST && x < Self::LAST`. /// the input is already known to be within bounds, i.e. when `x >= Self::FIRST && x < Self::LAST`.
#[must_use]
$vis unsafe fn new_unchecked(x: $repr) -> Self { $vis unsafe fn new_unchecked(x: $repr) -> Self {
debug_assert!((Self::FIRST.0..=Self::LAST.0).contains(&x)); debug_assert!((Self::FIRST.0..=Self::LAST.0).contains(&x));
Self(x) Self(x)
} }
#[must_use]
$vis fn as_index(&self) -> &$repr { $vis fn as_index(&self) -> &$repr {
&self.0 &self.0
} }
#[must_use]
$vis fn iter(&self) -> impl Iterator<Item = Self> {
(Self::FIRST.0..=Self::LAST.0).map(Self)
}
} }
impl From<$type> for $repr { impl From<$type> for $repr {
@ -105,11 +113,13 @@ coordinate_enum!(
); );
impl Direction { impl Direction {
#[must_use]
pub fn to_offset(&self) -> i8 { pub fn to_offset(&self) -> i8 {
const OFFSETS: [i8; 8] = [8, 9, 1, -7, -8, -9, -1, 7]; const OFFSETS: [i8; 8] = [8, 9, 1, -7, -8, -9, -1, 7];
OFFSETS[*self as usize] OFFSETS[*self as usize]
} }
#[must_use]
pub fn opposite(&self) -> Direction { pub fn opposite(&self) -> Direction {
const OPPOSITES: [Direction; 8] = [ const OPPOSITES: [Direction; 8] = [
Direction::South, Direction::South,
@ -228,7 +238,7 @@ impl Square {
#[inline] #[inline]
pub fn file(self) -> File { pub fn file(self) -> File {
unsafe { File::new_unchecked((self as u8) & 0b000111) } unsafe { File::new_unchecked((self as u8) & 0b000_00111) }
} }
#[inline] #[inline]
@ -374,39 +384,45 @@ mod tests {
} }
#[test] #[test]
fn good_algebraic_input() { fn good_algebraic_input() -> Result<(), ParseSquareError> {
let sq = Square::from_algebraic_str("a4").expect("Failed to parse 'a4' square"); let sq = Square::from_algebraic_str("a4")?;
assert_eq!(sq.file(), File(0)); assert_eq!(sq.file(), File(0));
assert_eq!(sq.rank(), Rank(3)); assert_eq!(sq.rank(), Rank(3));
let sq = Square::from_algebraic_str("B8").expect("Failed to parse 'B8' square"); let sq = Square::from_algebraic_str("B8")?;
assert_eq!(sq.file(), File(1)); assert_eq!(sq.file(), File(1));
assert_eq!(sq.rank(), Rank(7)); assert_eq!(sq.rank(), Rank(7));
let sq = Square::from_algebraic_str("e4").expect("Failed to parse 'B8' square"); let sq = Square::from_algebraic_str("e4")?;
assert_eq!(sq.file(), File(4)); assert_eq!(sq.file(), File(4));
assert_eq!(sq.rank(), Rank(3)); assert_eq!(sq.rank(), Rank(3));
Ok(())
} }
#[test] #[test]
fn bad_algebraic_input() { fn bad_algebraic_input() -> Result<(), ParseSquareError> {
Square::from_algebraic_str("a0").expect_err("Got valid Square for 'a0'"); Square::from_algebraic_str("a0")?;
Square::from_algebraic_str("j3").expect_err("Got valid Square for 'j3'"); Square::from_algebraic_str("j3")?;
Square::from_algebraic_str("a11").expect_err("Got valid Square for 'a11'"); Square::from_algebraic_str("a11")?;
Square::from_algebraic_str("b-1").expect_err("Got valid Square for 'b-1'"); Square::from_algebraic_str("b-1")?;
Square::from_algebraic_str("a 1").expect_err("Got valid Square for 'a 1'"); Square::from_algebraic_str("a 1")?;
Square::from_algebraic_str("").expect_err("Got valid Square for ''"); Square::from_algebraic_str("")?;
Ok(())
} }
#[test] #[test]
fn from_index() { fn from_index() -> Result<(), ()> {
let sq = Square::try_from(4u32).expect("Unable to get Square from index"); let sq = Square::try_from(4u32)?;
assert_eq!(sq.file(), File(4)); assert_eq!(sq.file(), File(4));
assert_eq!(sq.rank(), Rank(0)); assert_eq!(sq.rank(), Rank(0));
let sq = Square::try_from(28u32).expect("Unable to get Square from index"); let sq = Square::try_from(28u32)?;
assert_eq!(sq.file(), File(4)); assert_eq!(sq.file(), File(4));
assert_eq!(sq.rank(), Rank(3)); assert_eq!(sq.rank(), Rank(3));
Ok(())
} }
#[test] #[test]