Add FEN en-passant target square parsing

This commit is contained in:
Bruno BELANYI 2022-07-27 23:40:38 +02:00
parent 3c2a5a412e
commit b5365f8a82

View file

@ -1,4 +1,4 @@
use crate::board::Color;
use crate::board::{Color, File, Rank, Square};
/// A trait to mark items that can be converted from a FEN input.
pub trait FromFen: Sized {
@ -38,3 +38,20 @@ impl FromFen for Color {
Ok(res)
}
}
/// Convert an en-passant target square segment of a FEN string to an optional [Square].
impl FromFen for Option<Square> {
type Err = FenError;
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(FenError::InvalidFen),
};
Ok(res)
}
}