Make grid axes dumb enums

This commit is contained in:
2025-02-18 21:27:12 +01:00
parent c7f82769e3
commit 98645f01d0
2 changed files with 170 additions and 101 deletions

View File

@@ -30,30 +30,70 @@ trait GridAxisIO: std::fmt::Display
where
Self: GridAxis,
{
const ALL_CHARS: [char; 8];
fn parse(c: char) -> Result<Self, ()> {
let index = Self::ALL_CHARS.iter().position(|p| p == &c).ok_or(())?;
Ok(Self::ALL_VALUES[index].clone())
}
fn parse(c: char) -> Result<Self, ()>;
}
impl GridAxisIO for board::Rank {
const ALL_CHARS: [char; 8] = ['1', '2', '3', '4', '5', '6', '7', '8'];
fn parse(c: char) -> Result<Self, ()> {
match c {
'1' => Ok(board::Rank::_1),
'2' => Ok(board::Rank::_2),
'3' => Ok(board::Rank::_3),
'4' => Ok(board::Rank::_4),
'5' => Ok(board::Rank::_5),
'6' => Ok(board::Rank::_6),
'7' => Ok(board::Rank::_7),
'8' => Ok(board::Rank::_8),
_ => Err(()),
}
}
}
impl std::fmt::Display for board::Rank {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", Self::ALL_CHARS[self.get_index() as usize])
let c = match self {
board::Rank::_1 => '1',
board::Rank::_2 => '2',
board::Rank::_3 => '3',
board::Rank::_4 => '4',
board::Rank::_5 => '5',
board::Rank::_6 => '6',
board::Rank::_7 => '7',
board::Rank::_8 => '8',
};
write!(f, "{}", c)
}
}
impl GridAxisIO for board::File {
const ALL_CHARS: [char; 8] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
fn parse(c: char) -> Result<Self, ()> {
match c {
'a' => Ok(board::File::A),
'b' => Ok(board::File::B),
'c' => Ok(board::File::C),
'd' => Ok(board::File::D),
'e' => Ok(board::File::E),
'f' => Ok(board::File::F),
'g' => Ok(board::File::G),
'h' => Ok(board::File::H),
_ => Err(()),
}
}
}
impl std::fmt::Display for board::File {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", Self::ALL_CHARS[self.get_index() as usize])
let c = match self {
board::File::A => 'a',
board::File::B => 'b',
board::File::C => 'c',
board::File::D => 'd',
board::File::E => 'e',
board::File::F => 'f',
board::File::G => 'g',
board::File::H => 'h',
};
write!(f, "{}", c)
}
}