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, side,
}; };
if !res.is_valid() { if let Err(err) = res.validate() {
return Err(FenError::InvalidPosition); return Err(FenError::InvalidPosition(err));
} }
Ok(res) 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. /// A trait to mark items that can be converted from a FEN input.
pub trait FromFen: Sized { pub trait FromFen: Sized {
@ -13,16 +13,15 @@ pub enum FenError {
/// Invalid FEN input. /// Invalid FEN input.
InvalidFen, InvalidFen,
/// Invalid chess position. /// Invalid chess position.
InvalidPosition, InvalidPosition(InvalidError),
} }
impl std::fmt::Display for FenError { impl std::fmt::Display for FenError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let error_msg = match self { match self {
Self::InvalidFen => "Invalid FEN input", Self::InvalidFen => write!(f, "Invalid FEN input"),
Self::InvalidPosition => "Invalid chess position", Self::InvalidPosition(err) => write!(f, "Invalid chess position: {}", err),
}; }
write!(f, "{}", error_msg)
} }
} }