From 3c699b0b3d7c338b4bcda2b2e8c568aed4ec0a05 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 21 Jan 2024 13:02:06 -0800 Subject: [PATCH] [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. --- board/src/position/diagram_formatter.rs | 30 ++++++++++++------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/board/src/position/diagram_formatter.rs b/board/src/position/diagram_formatter.rs index 7bd8ccd..b007d20 100644 --- a/board/src/position/diagram_formatter.rs +++ b/board/src/position/diagram_formatter.rs @@ -1,7 +1,7 @@ // Eryn Wells 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);