mod board;
pub mod errors;
mod fen;
mod game_result;
mod helpers;
#[cfg(feature = "img")]
pub mod img;
mod move_;
#[cfg(feature = "pgn")]
pub mod pgn;
mod piece;
mod position;
pub use board::*;
pub(crate) use errors::*;
pub use fen::Fen;
pub use game_result::*;
pub use move_::*;
pub use piece::*;
pub use position::*;
use std::{fmt, ops::Not};
pub fn idx_to_sq(idx: usize) -> Result<(char, char), InvalidSquareIndexError> {
if !(0..64).contains(&idx) {
return Err(InvalidSquareIndexError(idx));
}
Ok(helpers::idx_to_sq(idx))
}
pub fn sq_to_idx(file: char, rank: char) -> Result<usize, InvalidSquareNameError> {
if !(('a'..='h').contains(&file) && ('1'..='8').contains(&rank)) {
return Err(InvalidSquareNameError(file, rank));
}
Ok(helpers::sq_to_idx(file, rank))
}
#[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)]
pub enum Color {
White,
Black,
}
impl Color {
pub fn is_white(&self) -> bool {
matches!(self, Self::White)
}
pub fn is_black(&self) -> bool {
matches!(self, Self::Black)
}
}
impl TryFrom<&str> for Color {
type Error = InvalidColorCharacterError;
fn try_from(string: &str) -> Result<Self, Self::Error> {
match string {
"w" => Ok(Self::White),
"b" => Ok(Self::Black),
_ => Err(InvalidColorCharacterError(string.to_string())),
}
}
}
impl From<Color> for char {
fn from(c: Color) -> char {
match c {
Color::White => 'w',
Color::Black => 'b',
}
}
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", char::from(*self))
}
}
impl Not for Color {
type Output = Self;
fn not(self) -> Self {
match self {
Self::White => Self::Black,
Self::Black => Self::White,
}
}
}
#[cfg(test)]
mod test;