diff --git a/position/src/fen.rs b/position/src/fen.rs index 26cdd0c..f21de15 100644 --- a/position/src/fen.rs +++ b/position/src/fen.rs @@ -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; + type Error; + fn to_fen(&self) -> Result; } impl ToFen for Position { - fn to_fen(&self) -> Result { + type Error = ToFenError; + + fn to_fen(&self) -> Result { 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 { + type Error = ToFenError; + + fn to_fen(&self) -> Result { 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 { + type Error = ToFenError; + + fn to_fen(&self) -> Result { 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 { + type Error = ToFenError; + + fn to_fen(&self) -> Result { Ok(self.piece().to_fen()?) } }