[board] Implement TryFrom<char> and Into<char> for piece::Shape
This commit is contained in:
parent
a963cee1e7
commit
371d37f688
1 changed files with 45 additions and 0 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue