use anyhow::anyhow;
use std::fmt;
use std::str::FromStr;
#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Copy, Debug)]
pub enum Piece {
Rabbit,
Cat,
Dog,
Horse,
Camel,
Elephant,
}
impl fmt::Display for Piece {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let piece = match self {
Piece::Elephant => 'e',
Piece::Camel => 'm',
Piece::Horse => 'h',
Piece::Dog => 'd',
Piece::Cat => 'c',
Piece::Rabbit => 'r',
};
write!(f, "{}", piece)
}
}
impl FromStr for Piece {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let piece = match s.chars().next() {
Some(c) if s.len() == 1 => match c {
'E' | 'e' => Some(Piece::Elephant),
'M' | 'm' => Some(Piece::Camel),
'H' | 'h' => Some(Piece::Horse),
'D' | 'd' => Some(Piece::Dog),
'C' | 'c' => Some(Piece::Cat),
'R' | 'r' => Some(Piece::Rabbit),
_ => None,
},
_ => None,
};
piece.ok_or_else(|| anyhow!("Invalid value for piece"))
}
}