Implement std::fmt::Display for board
This commit is contained in:
@@ -11,7 +11,7 @@ fn main() -> Result<(), ()> {
|
|||||||
match input_lines.next() {
|
match input_lines.next() {
|
||||||
None => break Ok(()),
|
None => break Ok(()),
|
||||||
Some(line) => match &line.unwrap()[..] {
|
Some(line) => match &line.unwrap()[..] {
|
||||||
"get_state" => println!("{}", board.to_string()),
|
"get_state" => println!("{}", board),
|
||||||
_ => break Err(()),
|
_ => break Err(()),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
64
rs/src/ui.rs
64
rs/src/ui.rs
@@ -1,59 +1,53 @@
|
|||||||
use crate::board;
|
use crate::board;
|
||||||
use crate::board::GridAxis;
|
use crate::board::GridAxis;
|
||||||
|
|
||||||
impl board::Color {
|
impl std::fmt::Display for board::Color {
|
||||||
fn to_string(&self) -> String {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
board::Color::White => "W".to_string(),
|
board::Color::White => write!(f, "W"),
|
||||||
board::Color::Black => "B".to_string(),
|
board::Color::Black => write!(f, "B"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl board::PieceType {
|
impl std::fmt::Display for board::PieceType {
|
||||||
fn to_string(&self) -> String {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
board::PieceType::Pawn => "P".to_string(),
|
board::PieceType::Pawn => write!(f, "P"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl board::Piece {
|
impl std::fmt::Display for board::Piece {
|
||||||
fn to_string(&self) -> String {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
self.piece_type.to_string() + &self.color.to_string()
|
write!(f, "{}{}", self.piece_type, self.color)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trait GridAxisIO
|
impl std::fmt::Display for board::Rank {
|
||||||
where
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
Self: GridAxis,
|
const ALL_CHARS: [char; 8] = ['1', '2', '3', '4', '5', '6', '7', '8'];
|
||||||
{
|
write!(f, "{}", ALL_CHARS[self.get_index() as usize])
|
||||||
const ALL_CHARS: [char; 8];
|
|
||||||
fn to_string(&self) -> String {
|
|
||||||
Self::ALL_CHARS[self.get_index() as usize].to_string()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GridAxisIO for board::Rank {
|
impl std::fmt::Display for board::File {
|
||||||
const ALL_CHARS: [char; Self::ALL_VALUES.len()] =
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
['1', '2', '3', '4', '5', '6', '7', '8'];
|
const ALL_CHARS: [char; 8] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
|
||||||
}
|
write!(f, "{}", ALL_CHARS[self.get_index() as usize])
|
||||||
|
|
||||||
impl GridAxisIO for board::File {
|
|
||||||
const ALL_CHARS: [char; Self::ALL_VALUES.len()] =
|
|
||||||
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
|
|
||||||
}
|
|
||||||
|
|
||||||
impl board::Position {
|
|
||||||
fn to_string(&self) -> String {
|
|
||||||
self.file.to_string() + &self.rank.to_string()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl board::Board {
|
impl std::fmt::Display for board::Position {
|
||||||
pub fn to_string(&self) -> String {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
self.iter()
|
write!(f, "{}{}", self.file, self.rank)
|
||||||
.map(|(position, piece)| piece.to_string() + &position.to_string())
|
}
|
||||||
.collect()
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for board::Board {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
Ok(for (position, piece) in self.iter() {
|
||||||
|
write!(f, "{}{}", piece, position)?
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user