[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:
parent
808526f8f7
commit
77f419ad3b
1 changed files with 24 additions and 15 deletions
|
@ -5,16 +5,19 @@ use chessfriend_core::{Color, File, Piece, PlacedPiece, Rank, Square};
|
|||
use std::fmt::Write;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum FenError {
|
||||
pub enum ToFenError {
|
||||
FmtError(std::fmt::Error),
|
||||
}
|
||||
|
||||
pub trait ToFen {
|
||||
fn to_fen(&self) -> Result<String, FenError>;
|
||||
type Error;
|
||||
fn to_fen(&self) -> Result<String, Self::Error>;
|
||||
}
|
||||
|
||||
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 empty_squares: u8 = 0;
|
||||
|
@ -25,27 +28,27 @@ impl ToFen for Position {
|
|||
Some(piece) => {
|
||||
if empty_squares > 0 {
|
||||
write!(fen_string, "{}", empty_squares)
|
||||
.map_err(|err| FenError::FmtError(err))?;
|
||||
.map_err(|err| ToFenError::FmtError(err))?;
|
||||
empty_squares = 0;
|
||||
}
|
||||
write!(fen_string, "{}", piece.to_fen()?)
|
||||
.map_err(|err| FenError::FmtError(err))?;
|
||||
.map_err(|err| ToFenError::FmtError(err))?;
|
||||
}
|
||||
None => empty_squares += 1,
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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()?)
|
||||
.map_err(|err| FenError::FmtError(err))?;
|
||||
.map_err(|err| ToFenError::FmtError(err))?;
|
||||
|
||||
let castling = [
|
||||
(Color::White, Castle::KingSide),
|
||||
|
@ -73,7 +76,7 @@ impl ToFen for Position {
|
|||
" {}",
|
||||
if castling.len() > 0 { &castling } else { "-" }
|
||||
)
|
||||
.map_err(|err| FenError::FmtError(err))?;
|
||||
.map_err(|err| ToFenError::FmtError(err))?;
|
||||
|
||||
write!(
|
||||
fen_string,
|
||||
|
@ -84,17 +87,19 @@ impl ToFen for Position {
|
|||
"-".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.move_number()).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| ToFenError::FmtError(err))?;
|
||||
|
||||
Ok(fen_string)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToFen for Color {
|
||||
fn to_fen(&self) -> Result<String, FenError> {
|
||||
type Error = ToFenError;
|
||||
|
||||
fn to_fen(&self) -> Result<String, Self::Error> {
|
||||
match self {
|
||||
Color::White => Ok("w".to_string()),
|
||||
Color::Black => Ok("b".to_string()),
|
||||
|
@ -103,7 +108,9 @@ impl ToFen for Color {
|
|||
}
|
||||
|
||||
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();
|
||||
Ok(String::from(match self.color() {
|
||||
Color::White => ascii.to_ascii_uppercase(),
|
||||
|
@ -113,7 +120,9 @@ impl ToFen for Piece {
|
|||
}
|
||||
|
||||
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()?)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue