From d3386bcb528e727f8476cce799f66feb64032d27 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 1 Apr 2024 20:21:05 +0100 Subject: [PATCH] Add 'chess_board::InvalidError' --- src/board/chess_board/error.rs | 50 ++++++++++++++++++++++++++++++++++ src/board/chess_board/mod.rs | 3 ++ 2 files changed, 53 insertions(+) create mode 100644 src/board/chess_board/error.rs diff --git a/src/board/chess_board/error.rs b/src/board/chess_board/error.rs new file mode 100644 index 0000000..e531f54 --- /dev/null +++ b/src/board/chess_board/error.rs @@ -0,0 +1,50 @@ +/// A singular type for all errors that could happen during [ChessBoard::is_valid]. +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum InvalidError { + /// Too many pieces. + TooManyPieces, + /// Missing king. + MissingKing, + /// Pawns on the first/last rank. + InvalidPawnPosition, + /// Castling rights do not match up with the state of the board. + InvalidCastlingRights, + /// En-passant target square is not empty and behind an opponent's pawn. + InvalidEnPassant, + /// The two kings are next to each other. + NeighbouringKings, + /// The opponent is currently in check. + OpponentInCheck, + /// The piece-specific boards are overlapping. + OverlappingPieces, + /// The color-specific boards are overlapping. + OverlappingColors, + /// The pre-computed combined occupancy boards does not match the other boards. + ErroneousCombinedOccupancy, +} + +impl std::fmt::Display for InvalidError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let error_msg = match self { + Self::TooManyPieces => "Too many pieces.", + Self::MissingKing => "Missing king.", + Self::InvalidPawnPosition => "Pawns on the first/last rank.", + Self::InvalidCastlingRights => { + "Castling rights do not match up with the state of the board." + } + Self::InvalidEnPassant => { + "En-passant target square is not empty and behind an opponent's pawn." + } + Self::NeighbouringKings => "The two kings are next to each other.", + Self::OpponentInCheck => "The opponent is currently in check.", + Self::OverlappingPieces => "The piece-specific boards are overlapping.", + Self::OverlappingColors => "The color-specific boards are overlapping.", + Self::ErroneousCombinedOccupancy => { + "The pre-computed combined occupancy boards does not match the other boards." + } + }; + write!(f, "{}", error_msg) + } +} + +impl std::error::Error for InvalidError {} diff --git a/src/board/chess_board/mod.rs b/src/board/chess_board/mod.rs index 838769a..17d34e8 100644 --- a/src/board/chess_board/mod.rs +++ b/src/board/chess_board/mod.rs @@ -5,6 +5,9 @@ use crate::{ use super::{Bitboard, CastleRights, Color, File, Move, Piece, Rank, Square}; +mod error; +pub use error::*; + /// Represent an on-going chess game. #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct ChessBoard {