From 7c896d5dba03f6cfa13f8532d44315fb8b3dbcc3 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 27 Jul 2022 23:40:55 +0200 Subject: [PATCH] Add FEN piece type parsing --- src/fen.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/fen.rs b/src/fen.rs index 4118b59..f058e84 100644 --- a/src/fen.rs +++ b/src/fen.rs @@ -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 { Ok(res) } } + +/// Convert a piece in FEN notation to a [Piece]. +impl FromFen for Piece { + type Err = FenError; + + 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(FenError::InvalidFen), + }; + Ok(res) + } +}