chessfriend/board/src/position/diagram_formatter.rs
Eryn Wells 3c699b0b3d [board] Remove the intermediate String when building a diagram representation of a Position
Also ignore the tests in position::diagram_formatter::tests. These don't assert
anything; they're just there for visual spot check.
2024-01-21 13:02:06 -08:00

67 lines
1.6 KiB
Rust

// Eryn Wells <eryn@erynwells.me>
use crate::{File, Position, Rank, Square};
use std::fmt;
pub struct DiagramFormatter<'a>(&'a Position);
impl<'a> DiagramFormatter<'a> {
pub fn new(position: &'a Position) -> DiagramFormatter {
DiagramFormatter(position)
}
}
impl<'a> fmt::Display for DiagramFormatter<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, " +-----------------+\n")?;
for rank in Rank::ALL.iter().rev() {
write!(f, "{rank} | ")?;
for file in File::ALL.iter() {
let square = Square::from_file_rank(*file, *rank);
match self.0.piece_on_square(square) {
Some(placed_piece) => write!(f, "{} ", placed_piece.piece())?,
None => write!(f, ". ")?,
}
}
write!(f, "|\n")?;
}
write!(f, " +-----------------+\n")?;
write!(f, " a b c d e f g h\n")?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Position;
#[test]
#[ignore]
fn empty() {
let pos = Position::empty();
let diagram = DiagramFormatter(&pos);
println!("{}", diagram);
}
#[test]
#[ignore]
fn one_king() {
let pos = position![Black King on H3];
let diagram = DiagramFormatter(&pos);
println!("{}", diagram);
}
#[test]
#[ignore]
fn starting() {
let pos = Position::starting();
let diagram = DiagramFormatter(&pos);
println!("{}", diagram);
}
}