Add validation error detail in 'FenError'
This commit is contained in:
parent
714feedbd2
commit
127dea25b4
|
@ -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)
|
||||
|
|
13
src/fen.rs
13
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue