[board] Implement TryFrom<char> and Into<char> for piece::Shape

This commit is contained in:
Eryn Wells 2023-12-26 13:57:36 -07:00
parent a963cee1e7
commit 371d37f688

View file

@ -1,5 +1,7 @@
// Eryn Wells <eryn@erynwells.me>
use std::fmt;
use crate::Square;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
@ -45,6 +47,49 @@ impl Shape {
}
}
pub struct TryFromCharError;
impl TryFrom<char> for Shape {
type Error = TryFromCharError;
fn try_from(value: char) -> Result<Self, Self::Error> {
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<char> for &Shape {
fn into(self) -> char {
self.clone().into()
}
}
impl Into<char> 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,