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());
}
}