use super::gamestate::Gamestate;
use std::error;
use std::fmt;
#[derive(Debug, PartialEq)]
pub enum GameError {
MovingPieceNotFound,
KingIsSafe,
KingIsInCheck,
KingIsInCheckmate,
KingCannotCastleSafetly,
OurKingMustBeSafe,
CaptureNotSet,
CaptureAlly,
NoCapturePiece,
GameOver(Gamestate),
CastlingUnderCheck,
CastlingUnavailable,
CastlingSquaresNotEmpty,
InvalidPawnMovement,
ParsingTurnFailed,
UndoNotAvailable,
}
impl error::Error for GameError {}
impl fmt::Display for GameError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.get_str())
}
}
impl GameError {
fn get_str(&self) -> String {
match self {
Self::MovingPieceNotFound => {
"Source square not found for the moving piece".to_string()
}
Self::NoCapturePiece => {
"Nothing to capture, dst square empty".to_string()
}
Self::KingIsInCheck => "Invalid turn: King is in check".to_string(),
Self::KingIsInCheckmate => {
"Invalid turn: King is in checkmate".to_string()
}
Self::OurKingMustBeSafe => {
"Invalid turn: Our king is in check".to_string()
}
Self::CaptureNotSet => {
"Invalid turn: Unexpected capture".to_string()
}
Self::CaptureAlly => {
"Capturing ally pieces not allowed".to_string()
}
Self::CastlingUnavailable => "Castling not available".to_string(),
Self::GameOver(gamestate) => format!("Game over: {}", gamestate),
Self::CastlingUnderCheck => {
"King under check cannot castle check".to_string()
}
Self::KingCannotCastleSafetly => {
"King cannot safetly perform castling".to_string()
}
Self::CastlingSquaresNotEmpty => {
"Squares between rook and king must be empty for castling"
.to_string()
}
Self::KingIsSafe => {
"Invalid turn: King not supposed to be safe".to_string()
}
Self::InvalidPawnMovement => {
"Pawns move diagonally only by capture".to_string()
}
Self::ParsingTurnFailed => "Parsing turn failed".to_string(),
Self::UndoNotAvailable => "Undo not available".to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn print() {
let errors = [
GameError::MovingPieceNotFound,
GameError::KingIsSafe,
GameError::KingIsInCheck,
GameError::KingIsInCheckmate,
GameError::KingCannotCastleSafetly,
GameError::OurKingMustBeSafe,
GameError::CaptureNotSet,
GameError::CaptureAlly,
GameError::NoCapturePiece,
GameError::GameOver(Gamestate::Ongoing),
GameError::CastlingUnderCheck,
GameError::CastlingUnavailable,
GameError::CastlingSquaresNotEmpty,
GameError::InvalidPawnMovement,
GameError::ParsingTurnFailed,
GameError::UndoNotAvailable,
];
errors.iter().for_each(|err| {
err.to_string();
});
}
}