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
use crate::flag::{Flag, FlagCheck};
use std::fmt;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum CastlingType {
Long,
Short,
}
impl CastlingType {
pub fn opposite(&self) -> Self {
match self {
Self::Short => Self::Long,
Self::Long => Self::Short,
}
}
}
impl fmt::Display for CastlingType {
#[rustfmt::skip]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", match self {
Self::Long => "0-0-0",
Self::Short => "0-0",
}
)
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Castling {
pub r#type: CastlingType,
pub flags: u8,
}
impl FlagCheck for Castling {
fn get_flags(&self) -> u8 {
self.flags
}
}
impl fmt::Display for Castling {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.flags {
Flag::NONE => write!(f, "{}", self.r#type),
Flag::CHECK => write!(f, "{}+", self.r#type),
Flag::CHECKMATE => write!(f, "{}#", self.r#type),
_ => panic!("Invalid flag used for castling struct"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn opposite_castling_type() {
let short = CastlingType::Short;
let long = CastlingType::Long;
assert_eq!(short, long.opposite());
assert_eq!(short, short.opposite().opposite());
assert_eq!(long, short.opposite());
assert_eq!(long, long.opposite().opposite());
}
}