1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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();
});
}
}