[board] Implement fmt::Display on Square
Prints the square in algebraic notation
This commit is contained in:
parent
769886086c
commit
3b3062108a
2 changed files with 15 additions and 1 deletions
|
@ -1,6 +1,6 @@
|
||||||
// Eryn Wells <eryn@erynwells.me>
|
// Eryn Wells <eryn@erynwells.me>
|
||||||
|
|
||||||
use std::str::FromStr;
|
use std::{fmt, str::FromStr};
|
||||||
|
|
||||||
pub enum Direction {
|
pub enum Direction {
|
||||||
North,
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -263,4 +269,11 @@ mod tests {
|
||||||
assert!(square63.neighbor(Direction::NorthEast).is_none());
|
assert!(square63.neighbor(Direction::NorthEast).is_none());
|
||||||
assert!(square63.neighbor(Direction::East).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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,4 +36,5 @@ impl Square {
|
||||||
sq_constructor!(f6);
|
sq_constructor!(f6);
|
||||||
sq_constructor!(g3);
|
sq_constructor!(g3);
|
||||||
sq_constructor!(g5);
|
sq_constructor!(g5);
|
||||||
|
sq_constructor!(h8);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue