1
0
Fork 0

Setup::validate -> Setup::into_position

This commit is contained in:
Paul-Nicolas Madelaine 2025-10-23 23:34:31 +02:00
parent 0d22c59cc3
commit 6f409799db
5 changed files with 19 additions and 18 deletions

View file

@ -28,7 +28,7 @@
//!
//! // read a position from a text record
//! let setup = "7k/4P1rp/5Q2/5p2/1Pp1bP2/8/r4K1P/6R1 w - -".parse::<Setup>()?;
//! let position = setup.validate()?;
//! let position = setup.into_position()?;
//!
//! // read a move in algebraic notation
//! let san = "Ke1".parse::<San>()?;

View file

@ -82,12 +82,14 @@ impl Position {
/// ```
/// # use eschac::setup::Setup;
/// # |s: &str| -> Option<eschac::position::Position> {
/// s.parse::<Setup>().ok().and_then(|pos| pos.validate().ok())
/// s.parse::<Setup>().ok().and_then(|pos| pos.into_position().ok())
/// # };
/// ```
#[inline]
pub fn from_text_record(s: &str) -> Option<Self> {
s.parse::<Setup>().ok().and_then(|pos| pos.validate().ok())
s.parse::<Setup>()
.ok()
.and_then(|pos| pos.into_position().ok())
}
/// Returns all the legal moves on the position.
@ -196,8 +198,7 @@ impl Position {
&self.0
}
/// Converts a position to a [`Setup`], allowing to edit the position without enforcing its
/// legality.
/// Converts the position into the [`Setup`] type, allowing to edit it without enforcing its legality.
#[inline]
pub fn into_setup(self) -> Setup {
self.0

View file

@ -10,7 +10,7 @@ use crate::position::*;
/// **A builder type for chess positions.**
///
/// This type is useful to edit a position without having to ensure it stays legal at every step.
/// It must be validated and converted to a [`Position`] using the [`Setup::validate`] method
/// It must be validated and converted to a [`Position`] using the [`Setup::into_position`] method
/// before generating moves.
///
/// This type implements [`FromStr`](std::str::FromStr) and [`Display`](std::fmt::Display) to parse
@ -245,10 +245,10 @@ impl Setup {
}
}
/// Tries to validate the position, i.e. converting it to a [`Position`].
/// Tries to convert the position into the [`Position`] type.
///
/// See [`IllegalPositionReason`] for details.
pub fn validate(self) -> Result<Position, IllegalPosition> {
/// Some unreachable positions are rejected, see [`IllegalPositionReason`] for details.
pub fn into_position(self) -> Result<Position, IllegalPosition> {
debug_assert!((self.w & !(self.p_b_q | self.n_b_k | self.r_q_k)).is_empty());
debug_assert!((self.p_b_q & self.n_b_k & self.r_q_k).is_empty());