diff --git a/README.md b/README.md index 5fae89b..7b5071c 100644 --- a/README.md +++ b/README.md @@ -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::()?; -let position = setup.validate()?; +let position = setup.into_position()?; // read a move in algebraic notation let san = "Ke1".parse::()?; diff --git a/src/lib.rs b/src/lib.rs index 8a0a4e7..19f553b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,7 +28,7 @@ //! //! // read a position from a text record //! let setup = "7k/4P1rp/5Q2/5p2/1Pp1bP2/8/r4K1P/6R1 w - -".parse::()?; -//! let position = setup.validate()?; +//! let position = setup.into_position()?; //! //! // read a move in algebraic notation //! let san = "Ke1".parse::()?; diff --git a/src/position.rs b/src/position.rs index 6132df7..1d4e9d2 100644 --- a/src/position.rs +++ b/src/position.rs @@ -82,12 +82,14 @@ impl Position { /// ``` /// # use eschac::setup::Setup; /// # |s: &str| -> Option { - /// s.parse::().ok().and_then(|pos| pos.validate().ok()) + /// s.parse::().ok().and_then(|pos| pos.into_position().ok()) /// # }; /// ``` #[inline] pub fn from_text_record(s: &str) -> Option { - s.parse::().ok().and_then(|pos| pos.validate().ok()) + s.parse::() + .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 diff --git a/src/setup.rs b/src/setup.rs index e0b0899..fe5f559 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -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 { + /// Some unreachable positions are rejected, see [`IllegalPositionReason`] for details. + pub fn into_position(self) -> Result { 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()); diff --git a/tests/tests.rs b/tests/tests.rs index e010b81..da5a1d1 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -18,7 +18,7 @@ fn recursive_check_aux(position: Position, depth: usize) { .to_string() .parse::() .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::().unwrap().validate().unwrap(), 4); + recursive_check_aux(record.parse::().unwrap().into_position().unwrap(), 4); } #[test] fn recursive_check_1() { @@ -199,7 +199,7 @@ fn setup() { record .parse::() .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::() .unwrap() - .validate() + .into_position() .unwrap(); let mirror = "rnbqkbnr/pppp1ppp/8/4p3/8/8/PPPPPPPP/RNBQKBNR w Qk e6" .parse::() .unwrap() - .validate() + .into_position() .unwrap(); assert_eq!(mirror, position.mirror()); } fn perft_aux(record: &str, tests: &[u128]) { - let position = record.parse::().unwrap().validate().unwrap(); + let position = record.parse::().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::() .unwrap() - .validate() + .into_position() .unwrap(); let san1 = "N7xf6#".parse::().unwrap(); let m1 = san1.to_move(&position).unwrap();