From 3b3062108ab4cc879eaa64755825ad11cb6b3bb3 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 5 Jan 2024 18:22:12 -0800 Subject: [PATCH] [board] Implement fmt::Display on Square Prints the square in algebraic notation --- board/src/square.rs | 15 ++++++++++++++- board/src/tests.rs | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) 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); }