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;