Add FEN piece type parsing

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

View file

@ -1,3 +1,6 @@
use super::FromFen;
use crate::error::Error;
/// An enum representing the type of a piece.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Piece {
@ -52,6 +55,24 @@ impl Piece {
}
}
/// Convert a piece in FEN notation to a [Piece].
impl FromFen for Piece {
type Err = Error;
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(Error::InvalidFen),
};
Ok(res)
}
}
#[cfg(test)]
mod test {
use super::*;