diff --git a/board/src/piece.rs b/board/src/piece.rs index 50db325..4997050 100644 --- a/board/src/piece.rs +++ b/board/src/piece.rs @@ -1,5 +1,7 @@ // Eryn Wells +use std::fmt; + use crate::Square; #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] @@ -45,6 +47,49 @@ impl Shape { } } +pub struct TryFromCharError; + +impl TryFrom for Shape { + type Error = TryFromCharError; + + fn try_from(value: char) -> Result { + match value { + 'p' => Ok(Shape::Pawn), + 'N' => Ok(Shape::Knight), + 'B' => Ok(Shape::Bishop), + 'R' => Ok(Shape::Rook), + 'Q' => Ok(Shape::Queen), + 'K' => Ok(Shape::King), + _ => Err(TryFromCharError), + } + } +} + +impl Into for &Shape { + fn into(self) -> char { + self.clone().into() + } +} + +impl Into for Shape { + fn into(self) -> char { + match self { + Shape::Pawn => 'p', + Shape::Knight => 'N', + Shape::Bishop => 'B', + Shape::Rook => 'R', + Shape::Queen => 'Q', + Shape::King => 'K', + } + } +} + +impl fmt::Display for Shape { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} + #[derive(Debug, Eq, PartialEq)] pub enum PiecePlacementError { ExistsOnSquare,