[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.
This commit is contained in:
Eryn Wells 2024-01-21 13:02:06 -08:00
parent 72316ad5f5
commit 3c699b0b3d

View file

@ -1,7 +1,7 @@
// Eryn Wells <eryn@erynwells.me>
use crate::{File, Position, Rank, Square};
use std::{fmt, fmt::Write};
use std::fmt;
pub struct DiagramFormatter<'a>(&'a Position);
@ -13,38 +13,36 @@ impl<'a> DiagramFormatter<'a> {
impl<'a> fmt::Display for DiagramFormatter<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut output = String::new();
output.push_str(" +-----------------+\n");
write!(f, " +-----------------+\n")?;
for rank in Rank::ALL.iter().rev() {
write!(output, "{} | ", rank)?;
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!(output, "{} ", placed_piece.piece())?,
None => output.push_str(". "),
Some(placed_piece) => write!(f, "{} ", placed_piece.piece())?,
None => write!(f, ". ")?,
}
}
output.push_str("|\n");
write!(f, "|\n")?;
}
output.push_str(" +-----------------+\n");
output.push_str(" a b c d e f g h\n");
write!(f, " +-----------------+\n")?;
write!(f, " a b c d e f g h\n")?;
write!(f, "{}", output)
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::piece::{Color, Piece};
use crate::{Position, PositionBuilder};
use crate::Position;
#[test]
#[ignore]
fn empty() {
let pos = Position::empty();
let diagram = DiagramFormatter(&pos);
@ -52,15 +50,15 @@ mod tests {
}
#[test]
#[ignore]
fn one_king() {
let pos = PositionBuilder::new()
.place_piece(piece!(Black King on H3))
.build();
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);