chess_notation_parser/
castling.rs1use crate::flag::{Flag, FlagCheck};
4use std::fmt;
5
6#[derive(Copy, Clone, Debug, PartialEq, Eq)]
8pub enum CastlingType {
9 Long,
11
12 Short,
14}
15
16impl CastlingType {
17 pub fn opposite(&self) -> Self {
19 match self {
20 Self::Short => Self::Long,
21 Self::Long => Self::Short,
22 }
23 }
24}
25
26impl fmt::Display for CastlingType {
27 #[rustfmt::skip]
28 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29 write!(f, "{}", match self {
30 Self::Long => "0-0-0",
31 Self::Short => "0-0",
32 }
33 )
34 }
35}
36
37#[derive(Copy, Clone, Debug, PartialEq)]
57pub struct Castling {
58 pub r#type: CastlingType,
60
61 pub flags: u8,
64}
65
66impl FlagCheck for Castling {
67 fn get_flags(&self) -> u8 {
68 self.flags
69 }
70}
71
72impl fmt::Display for Castling {
73 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74 match self.flags {
75 Flag::NONE => write!(f, "{}", self.r#type),
76 Flag::CHECK => write!(f, "{}+", self.r#type),
77 Flag::CHECKMATE => write!(f, "{}#", self.r#type),
78 _ => panic!("Invalid flag used for castling struct"),
79 }
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn opposite_castling_type() {
89 let short = CastlingType::Short;
90 let long = CastlingType::Long;
91
92 assert_eq!(short, long.opposite());
93 assert_eq!(short, short.opposite().opposite());
94
95 assert_eq!(long, short.opposite());
96 assert_eq!(long, long.opposite().opposite());
97 }
98}