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

@ -14,7 +14,7 @@ use eschac::prelude::*;
// 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

@ -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());

View file

@ -18,7 +18,7 @@ fn recursive_check_aux(position: Position, depth: usize) {
.to_string()
.parse::<Setup>()
.unwrap()
.validate()
.into_position()
.unwrap(),
);
@ -50,7 +50,7 @@ fn recursive_check_aux(position: Position, depth: usize) {
.en_passant_target_square()
.map(|square| square.mirror()),
);
setup.validate().unwrap()
setup.into_position().unwrap()
};
let expected_mirror = position.mirror();
assert_eq!(computed_mirror, expected_mirror);
@ -75,7 +75,7 @@ fn recursive_check_aux(position: Position, depth: usize) {
}
}
fn recursive_check(record: &str) {
recursive_check_aux(record.parse::<Setup>().unwrap().validate().unwrap(), 4);
recursive_check_aux(record.parse::<Setup>().unwrap().into_position().unwrap(), 4);
}
#[test]
fn recursive_check_1() {
@ -199,7 +199,7 @@ fn setup() {
record
.parse::<Setup>()
.unwrap()
.validate()
.into_position()
.is_err_and(|e| e.reasons().contains(reason)),
"{record} should be invalid because of {reason:?}",
);
@ -222,18 +222,18 @@ fn mirror() {
let position = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b Kq e3"
.parse::<Setup>()
.unwrap()
.validate()
.into_position()
.unwrap();
let mirror = "rnbqkbnr/pppp1ppp/8/4p3/8/8/PPPPPPPP/RNBQKBNR w Qk e6"
.parse::<Setup>()
.unwrap()
.validate()
.into_position()
.unwrap();
assert_eq!(mirror, position.mirror());
}
fn perft_aux(record: &str, tests: &[u128]) {
let position = record.parse::<Setup>().unwrap().validate().unwrap();
let position = record.parse::<Setup>().unwrap().into_position().unwrap();
for (depth, value) in tests.iter().copied().enumerate() {
assert_eq!(
position.perft(depth),
@ -275,7 +275,7 @@ fn san() {
let position = "8/2KN1p2/5p2/3N1B1k/5PNp/7P/7P/8 w - -"
.parse::<Setup>()
.unwrap()
.validate()
.into_position()
.unwrap();
let san1 = "N7xf6#".parse::<San>().unwrap();
let m1 = san1.to_move(&position).unwrap();