From dba4d94e354cb5713047b4756c2ef0388a0170a2 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 27 Jul 2022 23:40:38 +0200 Subject: [PATCH] Add FEN en-passant target square parsing --- src/board/square.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/board/square.rs b/src/board/square.rs index 9ffa824..9fbd5ee 100644 --- a/src/board/square.rs +++ b/src/board/square.rs @@ -1,5 +1,5 @@ -use super::{Bitboard, File, Rank}; -use crate::utils::static_assert; +use super::{Bitboard, File, FromFen, Rank}; +use crate::{error::Error, utils::static_assert}; /// Represent a square on a chessboard. Defined in the same order as the /// [Bitboard] squares. @@ -107,6 +107,23 @@ impl Square { } } +/// Convert an en-passant target square segment of a FEN string to an optional [Square]. +impl FromFen for Option { + type Err = Error; + + fn from_fen(s: &str) -> Result { + let res = match s.as_bytes() { + [b'-'] => None, + [file @ b'a'..=b'h', rank @ b'1'..=b'8'] => Some(Square::new( + File::from_index((file - b'a') as usize), + Rank::from_index((rank - b'1') as usize), + )), + _ => return Err(Error::InvalidFen), + }; + Ok(res) + } +} + /// Shift the square's index left by the amount given. impl std::ops::Shl for Square { type Output = Square;