chess_turn_engine/game/
availableturn.rs

1use std::fmt;
2
3/// Data which describes the turn that can be played.
4///
5/// In case of a castling turn, king's source and destination squares will be
6/// provided.
7///
8/// Exact turn written in the algebraic chess notation format contains spoilers,
9/// so it should be fetched via `get_turn` function.
10pub struct AvailableTurn {
11    /// Source square
12    pub src: String,
13
14    /// Destination square
15    pub dst: String,
16
17    /// Piece making the move
18    pub piece: String,
19
20    /// Captured piece
21    pub captured: Option<String>,
22
23    /// Chess notation format of the turn
24    turn: String,
25}
26
27impl fmt::Display for AvailableTurn {
28    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29        write!(
30            f,
31            "AvailableTurn (src: {}, dst: {}, piece: {}, captured: {:?}, \
32             turn: {}",
33            self.src, self.dst, self.piece, self.captured, self.turn
34        )
35    }
36}
37
38impl AvailableTurn {
39    /// Create `AvailableTurn`
40    pub fn new(
41        src: String,
42        dst: String,
43        piece: String,
44        captured: Option<String>,
45        turn: String,
46    ) -> AvailableTurn {
47        Self {
48            src,
49            dst,
50            piece,
51            captured,
52            turn,
53        }
54    }
55
56    /// Fetch turn written in chess notation format
57    ///
58    /// Turn info contains spoilers about the turn (checkmate or check),
59    /// that's why the `turn` string is not publicly provided in the struct
60    pub fn get_turn(&self) -> &str {
61        self.turn.as_str()
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn print() {
71        let src = String::from("a2");
72        let dst = String::from("a3");
73        let turn = String::from("a3");
74        let piece = String::from("Pawn");
75        let captured: Option<String> = None;
76
77        assert_eq!(
78            format!(
79                "AvailableTurn (src: {}, dst: {}, piece: {}, captured: {:?}, \
80                 turn: {}",
81                src, dst, piece, captured, turn
82            ),
83            AvailableTurn::new(
84                src.clone(),
85                dst.clone(),
86                piece,
87                captured,
88                turn.clone()
89            )
90            .to_string()
91        );
92    }
93}