Add FEN piece type parsing

This commit is contained in:
Bruno BELANYI 2022-07-27 23:40:55 +02:00
parent b5365f8a82
commit 7c896d5dba

View file

@ -1,4 +1,4 @@
use crate::board::{Color, File, Rank, Square};
use crate::board::{Color, File, Piece, Rank, Square};
/// A trait to mark items that can be converted from a FEN input.
pub trait FromFen: Sized {
@ -55,3 +55,21 @@ impl FromFen for Option<Square> {
Ok(res)
}
}
/// Convert a piece in FEN notation to a [Piece].
impl FromFen for Piece {
type Err = FenError;
fn from_fen(s: &str) -> Result<Self, Self::Err> {
let res = match s {
"p" | "P" => Self::Pawn,
"n" | "N" => Self::Knight,
"b" | "B" => Self::Bishop,
"r" | "R" => Self::Rook,
"q" | "Q" => Self::Queen,
"k" | "K" => Self::King,
_ => return Err(FenError::InvalidFen),
};
Ok(res)
}
}