Add FEN en-passant target square parsing

This commit is contained in:
Bruno BELANYI 2022-07-27 23:40:38 +02:00
parent 6f0e2f732b
commit dba4d94e35

View file

@ -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<Square> {
type Err = Error;
fn from_fen(s: &str) -> Result<Self, Self::Err> {
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<usize> for Square {
type Output = Square;