2024-04-03 22:25:45 +02:00
|
|
|
/// A singular type for all errors that could happen during [crate::board::ChessBoard::is_valid].
|
2024-04-01 21:21:05 +02:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2024-04-03 21:15:17 +02:00
|
|
|
pub enum ValidationError {
|
2024-04-01 21:21:05 +02:00
|
|
|
/// 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,
|
2024-04-01 22:54:32 +02:00
|
|
|
/// En-passant target square is not empty, behind an opponent's pawn, on the correct rank.
|
2024-04-01 21:21:05 +02:00
|
|
|
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,
|
2024-04-01 23:41:01 +02:00
|
|
|
/// Half-move clock is higher than total number of plies.
|
|
|
|
HalfMoveClockTooHigh,
|
2024-04-01 23:59:06 +02:00
|
|
|
/// The total plie count does not match the current player.
|
|
|
|
IncoherentPlieCount,
|
2024-04-01 21:21:05 +02:00
|
|
|
}
|
|
|
|
|
2024-04-03 21:15:17 +02:00
|
|
|
impl std::fmt::Display for ValidationError {
|
2024-04-01 21:21:05 +02:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let error_msg = match self {
|
2024-04-06 12:17:45 +02:00
|
|
|
Self::TooManyPieces => "too many pieces",
|
|
|
|
Self::MissingKing => "missing king",
|
|
|
|
Self::InvalidPawnPosition => "pawns on the first/last rank",
|
2024-04-01 21:21:05 +02:00
|
|
|
Self::InvalidCastlingRights => {
|
2024-04-06 12:17:45 +02:00
|
|
|
"castling rights do not match up with the state of the board"
|
2024-04-01 21:21:05 +02:00
|
|
|
}
|
|
|
|
Self::InvalidEnPassant => {
|
2024-04-06 12:17:45 +02:00
|
|
|
"en-passant target square is not empty, behind an opponent's pawn, on the correct rank"
|
2024-04-01 21:21:05 +02:00
|
|
|
}
|
2024-04-06 12:17:45 +02:00
|
|
|
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",
|
2024-04-01 21:21:05 +02:00
|
|
|
Self::ErroneousCombinedOccupancy => {
|
2024-04-06 12:17:45 +02:00
|
|
|
"the pre-computed combined occupancy boards does not match the other boards"
|
2024-04-01 21:21:05 +02:00
|
|
|
}
|
2024-04-06 12:17:45 +02:00
|
|
|
Self::HalfMoveClockTooHigh => "half-move clock is higher than total number of plies",
|
|
|
|
Self::IncoherentPlieCount => "the total plie count does not match the current player",
|
2024-04-01 21:21:05 +02:00
|
|
|
};
|
|
|
|
write!(f, "{}", error_msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-03 21:15:17 +02:00
|
|
|
impl std::error::Error for ValidationError {}
|