From 0d8b9cc9cfa2eb098ec8ddf9be4e29a82f0afb41 Mon Sep 17 00:00:00 2001 From: Paul-Nicolas Madelaine Date: Thu, 23 Oct 2025 23:34:31 +0200 Subject: [PATCH] update interface for illegal positions --- src/setup.rs | 38 ++++++++++++++------------------------ tests/tests.rs | 2 +- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/src/setup.rs b/src/setup.rs index d4ce6cf..8978d24 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -542,8 +542,8 @@ impl std::error::Error for ParseSetupError {} /// This is an illegal position that can't be represented with the [`Position`] type. #[derive(Debug)] pub struct IllegalPosition { - setup: Setup, - reasons: IllegalPositionReasons, + pub setup: Setup, + pub reasons: IllegalPositionReasons, } impl std::fmt::Display for IllegalPosition { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -556,30 +556,14 @@ impl std::fmt::Display for IllegalPosition { f.write_char(',')?; } first = false; - write!(f, " {reason}")?; + f.write_char(' ')?; + f.write_str(reason.to_str())?; } Ok(()) } } 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. #[derive(Clone, Copy)] pub struct IllegalPositionReasons(u8); @@ -650,9 +634,9 @@ pub enum IllegalPositionReason { ImpossibleEnPassantPin = 128, } -impl std::fmt::Display for IllegalPositionReason { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str(match self { +impl IllegalPositionReason { + fn to_str(self) -> &'static str { + match self { Self::MissingKing => "missing king", Self::TooManyKings => "too many kings", Self::HangingKing => "hanging king", @@ -661,6 +645,12 @@ impl std::fmt::Display for IllegalPositionReason { Self::InvalidEnPassant => "invalid en passant", Self::TooMuchMaterial => "too much material", 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()) } } diff --git a/tests/tests.rs b/tests/tests.rs index da5a1d1..e8b8799 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -200,7 +200,7 @@ fn setup() { .parse::() .unwrap() .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:?}", ); }