From 127dea25b41a1ac8b78fc29f4db6bae316090cc3 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 1 Apr 2024 20:28:16 +0100 Subject: [PATCH] Add validation error detail in 'FenError' --- src/board/chess_board/mod.rs | 4 ++-- src/fen.rs | 13 ++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/board/chess_board/mod.rs b/src/board/chess_board/mod.rs index d51eabb..d1d3751 100644 --- a/src/board/chess_board/mod.rs +++ b/src/board/chess_board/mod.rs @@ -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) diff --git a/src/fen.rs b/src/fen.rs index d8af180..3034003 100644 --- a/src/fen.rs +++ b/src/fen.rs @@ -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), + } } }