diff --git a/src/board/piece.rs b/src/board/piece.rs index 58f989a..6c35288 100644 --- a/src/board/piece.rs +++ b/src/board/piece.rs @@ -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 { + 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::*;