diff --git a/board/src/square.rs b/board/src/square.rs index 5655200..821d5de 100644 --- a/board/src/square.rs +++ b/board/src/square.rs @@ -1,6 +1,6 @@ // Eryn Wells -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"); + } } diff --git a/board/src/tests.rs b/board/src/tests.rs index e1018e2..1bc0085 100644 --- a/board/src/tests.rs +++ b/board/src/tests.rs @@ -36,4 +36,5 @@ impl Square { sq_constructor!(f6); sq_constructor!(g3); sq_constructor!(g5); + sq_constructor!(h8); }