Add validation error detail in 'FenError'

This commit is contained in:
Bruno BELANYI 2024-04-01 20:28:16 +01:00
parent 714feedbd2
commit 127dea25b4
2 changed files with 8 additions and 9 deletions

View file

@ -485,8 +485,8 @@ impl FromFen for ChessBoard {
side,
};
if !res.is_valid() {
return Err(FenError::InvalidPosition);
if let Err(err) = res.validate() {
return Err(FenError::InvalidPosition(err));
}
Ok(res)

View file

@ -1,4 +1,4 @@
use crate::board::{CastleRights, Color, File, Piece, Rank, Square};
use crate::board::{CastleRights, Color, File, InvalidError, Piece, Rank, Square};
/// A trait to mark items that can be converted from a FEN input.
pub trait FromFen: Sized {
@ -13,16 +13,15 @@ pub enum FenError {
/// Invalid FEN input.
InvalidFen,
/// Invalid chess position.
InvalidPosition,
InvalidPosition(InvalidError),
}
impl std::fmt::Display for FenError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let error_msg = match self {
Self::InvalidFen => "Invalid FEN input",
Self::InvalidPosition => "Invalid chess position",
};
write!(f, "{}", error_msg)
match self {
Self::InvalidFen => write!(f, "Invalid FEN input"),
Self::InvalidPosition(err) => write!(f, "Invalid chess position: {}", err),
}
}
}