[board] Implement fmt::Display on Square

Prints the square in algebraic notation
This commit is contained in:
Eryn Wells 2024-01-05 18:22:12 -08:00
parent 769886086c
commit 3b3062108a
2 changed files with 15 additions and 1 deletions

View file

@ -1,6 +1,6 @@
// Eryn Wells <eryn@erynwells.me>
use std::str::FromStr;
use std::{fmt, str::FromStr};
pub enum Direction {
North,
@ -156,6 +156,12 @@ impl FromStr for Square {
}
}
impl fmt::Display for Square {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}{}", ('a' as u8 + self.file) as char, self.rank + 1)
}
}
#[cfg(test)]
mod tests {
use super::*;
@ -263,4 +269,11 @@ mod tests {
assert!(square63.neighbor(Direction::NorthEast).is_none());
assert!(square63.neighbor(Direction::East).is_none());
}
#[test]
fn display() {
assert_eq!(format!("{}", Square::c5()), "c5");
assert_eq!(format!("{}", Square::a1()), "a1");
assert_eq!(format!("{}", Square::h8()), "h8");
}
}

View file

@ -36,4 +36,5 @@ impl Square {
sq_constructor!(f6);
sq_constructor!(g3);
sq_constructor!(g5);
sq_constructor!(h8);
}