1
0
Fork 0

update interface for illegal positions

This commit is contained in:
Paul-Nicolas Madelaine 2025-10-23 23:34:31 +02:00
parent d8f9be3b2c
commit 0d8b9cc9cf
2 changed files with 15 additions and 25 deletions

View file

@ -542,8 +542,8 @@ impl std::error::Error for ParseSetupError {}
/// This is an illegal position that can't be represented with the [`Position`] type. /// This is an illegal position that can't be represented with the [`Position`] type.
#[derive(Debug)] #[derive(Debug)]
pub struct IllegalPosition { pub struct IllegalPosition {
setup: Setup, pub setup: Setup,
reasons: IllegalPositionReasons, pub reasons: IllegalPositionReasons,
} }
impl std::fmt::Display for IllegalPosition { impl std::fmt::Display for IllegalPosition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -556,30 +556,14 @@ impl std::fmt::Display for IllegalPosition {
f.write_char(',')?; f.write_char(',')?;
} }
first = false; first = false;
write!(f, " {reason}")?; f.write_char(' ')?;
f.write_str(reason.to_str())?;
} }
Ok(()) Ok(())
} }
} }
impl std::error::Error for IllegalPosition {} impl std::error::Error for IllegalPosition {}
impl IllegalPosition {
/// Returns an iterator over the reasons why the position is rejected.
pub fn reasons(&self) -> IllegalPositionReasons {
self.reasons
}
/// Returns the [`Setup`] that failed validation.
pub fn as_setup(&self) -> &Setup {
&self.setup
}
/// Returns the [`Setup`] that failed validation.
pub fn into_setup(self) -> Setup {
self.setup
}
}
/// A set of [`IllegalPositionReason`]s. /// A set of [`IllegalPositionReason`]s.
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct IllegalPositionReasons(u8); pub struct IllegalPositionReasons(u8);
@ -650,9 +634,9 @@ pub enum IllegalPositionReason {
ImpossibleEnPassantPin = 128, ImpossibleEnPassantPin = 128,
} }
impl std::fmt::Display for IllegalPositionReason { impl IllegalPositionReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn to_str(self) -> &'static str {
f.write_str(match self { match self {
Self::MissingKing => "missing king", Self::MissingKing => "missing king",
Self::TooManyKings => "too many kings", Self::TooManyKings => "too many kings",
Self::HangingKing => "hanging king", Self::HangingKing => "hanging king",
@ -661,6 +645,12 @@ impl std::fmt::Display for IllegalPositionReason {
Self::InvalidEnPassant => "invalid en passant", Self::InvalidEnPassant => "invalid en passant",
Self::TooMuchMaterial => "too much material", Self::TooMuchMaterial => "too much material",
Self::ImpossibleEnPassantPin => "illegal en passant", Self::ImpossibleEnPassantPin => "illegal en passant",
}) }
}
}
impl std::fmt::Display for IllegalPositionReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.to_str())
} }
} }

View file

@ -200,7 +200,7 @@ fn setup() {
.parse::<Setup>() .parse::<Setup>()
.unwrap() .unwrap()
.into_position() .into_position()
.is_err_and(|e| e.reasons().contains(reason)), .is_err_and(|e| e.reasons.contains(reason)),
"{record} should be invalid because of {reason:?}", "{record} should be invalid because of {reason:?}",
); );
} }