[position] Rename FenError → ToFenError

Add an associated type called Error to the ToFen trait. This mirrors the try_from
any try_into traits.
This commit is contained in:
Eryn Wells 2024-01-28 10:02:53 -08:00
parent 808526f8f7
commit 77f419ad3b

View file

@ -5,16 +5,19 @@ use chessfriend_core::{Color, File, Piece, PlacedPiece, Rank, Square};
use std::fmt::Write; use std::fmt::Write;
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum FenError { pub enum ToFenError {
FmtError(std::fmt::Error), FmtError(std::fmt::Error),
} }
pub trait ToFen { pub trait ToFen {
fn to_fen(&self) -> Result<String, FenError>; type Error;
fn to_fen(&self) -> Result<String, Self::Error>;
} }
impl ToFen for Position { impl ToFen for Position {
fn to_fen(&self) -> Result<String, FenError> { type Error = ToFenError;
fn to_fen(&self) -> Result<String, Self::Error> {
let mut fen_string = String::new(); let mut fen_string = String::new();
let mut empty_squares: u8 = 0; let mut empty_squares: u8 = 0;
@ -25,27 +28,27 @@ impl ToFen for Position {
Some(piece) => { Some(piece) => {
if empty_squares > 0 { if empty_squares > 0 {
write!(fen_string, "{}", empty_squares) write!(fen_string, "{}", empty_squares)
.map_err(|err| FenError::FmtError(err))?; .map_err(|err| ToFenError::FmtError(err))?;
empty_squares = 0; empty_squares = 0;
} }
write!(fen_string, "{}", piece.to_fen()?) write!(fen_string, "{}", piece.to_fen()?)
.map_err(|err| FenError::FmtError(err))?; .map_err(|err| ToFenError::FmtError(err))?;
} }
None => empty_squares += 1, None => empty_squares += 1,
} }
} }
if empty_squares > 0 { if empty_squares > 0 {
write!(fen_string, "{}", empty_squares).map_err(|err| FenError::FmtError(err))?; write!(fen_string, "{}", empty_squares).map_err(|err| ToFenError::FmtError(err))?;
empty_squares = 0; empty_squares = 0;
} }
if rank != &Rank::ONE { if rank != &Rank::ONE {
write!(fen_string, "/").map_err(|err| FenError::FmtError(err))?; write!(fen_string, "/").map_err(|err| ToFenError::FmtError(err))?;
} }
} }
write!(fen_string, " {}", self.player_to_move().to_fen()?) write!(fen_string, " {}", self.player_to_move().to_fen()?)
.map_err(|err| FenError::FmtError(err))?; .map_err(|err| ToFenError::FmtError(err))?;
let castling = [ let castling = [
(Color::White, Castle::KingSide), (Color::White, Castle::KingSide),
@ -73,7 +76,7 @@ impl ToFen for Position {
" {}", " {}",
if castling.len() > 0 { &castling } else { "-" } if castling.len() > 0 { &castling } else { "-" }
) )
.map_err(|err| FenError::FmtError(err))?; .map_err(|err| ToFenError::FmtError(err))?;
write!( write!(
fen_string, fen_string,
@ -84,17 +87,19 @@ impl ToFen for Position {
"-".to_string() "-".to_string()
} }
) )
.map_err(|err| FenError::FmtError(err))?; .map_err(|err| ToFenError::FmtError(err))?;
write!(fen_string, " {}", self.ply_counter()).map_err(|err| FenError::FmtError(err))?; write!(fen_string, " {}", self.ply_counter()).map_err(|err| ToFenError::FmtError(err))?;
write!(fen_string, " {}", self.move_number()).map_err(|err| FenError::FmtError(err))?; write!(fen_string, " {}", self.move_number()).map_err(|err| ToFenError::FmtError(err))?;
Ok(fen_string) Ok(fen_string)
} }
} }
impl ToFen for Color { impl ToFen for Color {
fn to_fen(&self) -> Result<String, FenError> { type Error = ToFenError;
fn to_fen(&self) -> Result<String, Self::Error> {
match self { match self {
Color::White => Ok("w".to_string()), Color::White => Ok("w".to_string()),
Color::Black => Ok("b".to_string()), Color::Black => Ok("b".to_string()),
@ -103,7 +108,9 @@ impl ToFen for Color {
} }
impl ToFen for Piece { impl ToFen for Piece {
fn to_fen(&self) -> Result<String, FenError> { type Error = ToFenError;
fn to_fen(&self) -> Result<String, Self::Error> {
let ascii: char = self.to_ascii(); let ascii: char = self.to_ascii();
Ok(String::from(match self.color() { Ok(String::from(match self.color() {
Color::White => ascii.to_ascii_uppercase(), Color::White => ascii.to_ascii_uppercase(),
@ -113,7 +120,9 @@ impl ToFen for Piece {
} }
impl ToFen for PlacedPiece { impl ToFen for PlacedPiece {
fn to_fen(&self) -> Result<String, FenError> { type Error = ToFenError;
fn to_fen(&self) -> Result<String, Self::Error> {
Ok(self.piece().to_fen()?) Ok(self.piece().to_fen()?)
} }
} }