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