2022-07-27 23:40:15 +02:00
|
|
|
use crate::board::Color;
|
|
|
|
|
2022-07-27 23:25:35 +02:00
|
|
|
/// A trait to mark items that can be converted from a FEN input.
|
|
|
|
pub trait FromFen: Sized {
|
|
|
|
type Err;
|
|
|
|
|
|
|
|
fn from_fen(s: &str) -> Result<Self, Self::Err>;
|
|
|
|
}
|
2022-07-27 23:24:24 +02:00
|
|
|
|
|
|
|
/// A singular type for all errors that could happen during FEN parsing.
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub enum FenError {
|
|
|
|
/// Invalid FEN input.
|
|
|
|
InvalidFen,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for FenError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let error_msg = match self {
|
|
|
|
Self::InvalidFen => "Invalid FEN input",
|
|
|
|
};
|
|
|
|
write!(f, "{}", error_msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for FenError {}
|
2022-07-27 23:40:15 +02:00
|
|
|
|
|
|
|
/// Convert a side to move segment of a FEN string to a [Color].
|
|
|
|
impl FromFen for Color {
|
|
|
|
type Err = FenError;
|
|
|
|
|
|
|
|
fn from_fen(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let res = match s {
|
|
|
|
"w" => Color::White,
|
|
|
|
"b" => Color::Black,
|
|
|
|
_ => return Err(FenError::InvalidFen),
|
|
|
|
};
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|