Merge branch 'many-crates'

This commit is contained in:
Eryn Wells 2024-01-24 17:14:30 -08:00
commit d27c455ce4
25 changed files with 235 additions and 227 deletions

View file

@ -4,13 +4,12 @@ pub mod fen;
mod display;
mod r#move;
#[macro_use]
mod macros;
mod move_generator;
pub mod piece;
mod position;
mod sight;
pub use piece::{Color, Piece};
#[macro_use]
mod macros;
pub use position::{MoveBuilder as MakeMoveBuilder, Position, PositionBuilder};
pub use r#move::{Castle, MakeMoveError, Move, MoveBuilder};

View file

@ -1,15 +1,5 @@
// Eryn Wells <eryn@erynwells.me>
#[macro_export]
macro_rules! piece {
($color:ident $shape:ident) => {
$crate::piece::Piece::new($crate::piece::Color::$color, $crate::piece::Shape::$shape)
};
($color:ident $shape:ident on $square:ident) => {
$crate::piece::PlacedPiece::new(piece!($color $shape), chessfriend_core::Square::$square)
}
}
#[macro_export]
macro_rules! position {
[$($color:ident $shape:ident on $square:ident),* $(,)?] => {

View file

@ -1,7 +1,6 @@
// Eryn Wells <eryn@erynwells.me>
use crate::piece::{Piece, PlacedPiece, Shape};
use chessfriend_core::{Rank, Square};
use chessfriend_core::{Piece, PlacedPiece, Rank, Shape, Square};
use std::fmt;
pub use castle::Castle;
@ -17,8 +16,7 @@ pub enum MakeMoveError {
}
mod castle {
use crate::Color;
use chessfriend_core::Square;
use chessfriend_core::{Color, Square};
#[repr(u16)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
@ -305,7 +303,8 @@ impl MoveBuilder {
mod move_formatter {
use super::{Castle, Move};
use crate::{piece::Shape, Position};
use crate::Position;
use chessfriend_core::Shape;
use std::fmt;
enum Style {
@ -410,14 +409,15 @@ mod move_formatter {
#[cfg(test)]
mod tests {
use super::{AlgebraicMoveFormatter, Style};
use crate::{piece, position};
use crate::position;
use chessfriend_core::piece;
macro_rules! chess_move {
($color:ident $shape:ident $from_square:ident - $to_square:ident) => {
$crate::MoveBuilder::new(
$crate::piece::Piece::new(
$crate::piece::Color::$color,
$crate::piece::Shape::$shape,
chessfriend_core::Piece::new(
chessfriend_core::Color::$color,
chessfriend_core::Shape::$shape,
),
chessfriend_core::Square::$from_square,
chessfriend_core::Square::$to_square,
@ -426,17 +426,17 @@ mod move_formatter {
};
($color:ident $shape:ident $from_square:ident x $to_square:ident, $captured_color:ident $captured_shape:ident) => {
$crate::MoveBuilder::new(
$crate::piece::Piece::new(
$crate::piece::Color::$color,
$crate::piece::Shape::$shape,
chessfriend_core::Piece::new(
chessfriend_core::Color::$color,
chessfriend_core::Shape::$shape,
),
chessfriend_core::Square::$from_square,
chessfriend_core::Square::$to_square,
)
.capturing($crate::piece::PlacedPiece::new(
$crate::piece::Piece::new(
$crate::piece::Color::$captured_color,
$crate::piece::Shape::$captured_shape,
.capturing(chessfriend_core::PlacedPiece::new(
chessfriend_core::Piece::new(
chessfriend_core::Color::$captured_color,
chessfriend_core::Shape::$captured_shape,
),
chessfriend_core::Square::$to_square,
))
@ -488,7 +488,7 @@ mod move_formatter {
#[cfg(test)]
mod tests {
use super::*;
use crate::piece;
use chessfriend_core::piece;
macro_rules! assert_flag {
($move:expr, $left:expr, $right:expr) => {

View file

@ -1,12 +1,9 @@
// Eryn Wells <eryn@erynwells.me>
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
use crate::{
piece::{Color, Piece, PlacedPiece},
MoveBuilder, Position,
};
use crate::{MoveBuilder, Position};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::Direction;
use chessfriend_core::{Color, Direction, Piece, PlacedPiece};
move_generator_declaration!(ClassicalMoveGenerator);
@ -61,8 +58,9 @@ impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> {
#[cfg(test)]
mod tests {
use super::*;
use crate::{piece, piece::Color, position, position::DiagramFormatter};
use crate::{position, position::DiagramFormatter};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::{piece, Color};
#[test]
fn classical_single_bishop_bitboard() {

View file

@ -4,12 +4,9 @@
//! generating the possible moves for the king in the given position.
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
use crate::{
piece::{Color, Piece, PlacedPiece},
r#move::Castle,
Move, MoveBuilder, Position,
};
use crate::{ r#move::Castle, Move, MoveBuilder, Position};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Color, Piece, PlacedPiece};
move_generator_declaration!(KingMoveGenerator, struct);
move_generator_declaration!(KingMoveGenerator, new);
@ -77,8 +74,9 @@ impl<'pos> MoveGeneratorInternal for KingMoveGenerator<'pos> {
#[cfg(test)]
mod tests {
use super::*;
use crate::position;
use chessfriend_bitboard::bitboard;
use chessfriend_core::Square;
use chessfriend_core::{piece, Square};
use std::collections::HashSet;
#[test]

View file

@ -1,11 +1,9 @@
// Eryn Wells <eryn@erynwells.me>
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
use crate::{
piece::{Color, Piece, PlacedPiece},
MoveBuilder, Position,
};
use crate::{MoveBuilder, Position};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Color, Piece, PlacedPiece};
move_generator_declaration!(KnightMoveGenerator);
@ -41,8 +39,8 @@ impl<'pos> MoveGeneratorInternal for KnightMoveGenerator<'pos> {
#[cfg(test)]
mod tests {
use super::*;
use crate::{piece, position, Move};
use chessfriend_core::Square;
use crate::{position, Move};
use chessfriend_core::{piece, Square};
use std::collections::HashSet;
#[test]

View file

@ -12,11 +12,8 @@ mod rook;
pub use move_generator::Moves;
pub(crate) use move_set::MoveSet;
use crate::{
piece::{Color, Piece, PlacedPiece},
Move, Position,
};
use chessfriend_core::Square;
use crate::{ Move, Position};
use chessfriend_core::{Color, Piece, PlacedPiece, Square};
use std::collections::BTreeMap;
trait MoveGenerator {
@ -32,9 +29,10 @@ macro_rules! move_generator_declaration {
move_generator_declaration!($name, getters);
};
($name:ident, struct) => {
#[derive(Clone, Debug)]
pub(super) struct $name<'pos> {
position: &'pos $crate::Position,
color: $crate::piece::Color,
color: chessfriend_core::Color,
move_sets: std::collections::BTreeMap<
chessfriend_core::Square,
$crate::move_generator::MoveSet,
@ -43,7 +41,10 @@ macro_rules! move_generator_declaration {
};
($name:ident, new) => {
impl<'pos> $name<'pos> {
pub(super) fn new(position: &$crate::Position, color: $crate::piece::Color) -> $name {
pub(super) fn new(
position: &$crate::Position,
color: chessfriend_core::Color,
) -> $name {
$name {
position,
color,
@ -58,6 +59,13 @@ macro_rules! move_generator_declaration {
self.move_sets.values().map(|set| set.moves()).flatten()
}
pub(crate) fn moves_for_piece(
&self,
piece: &chessfriend_core::PlacedPiece,
) -> Option<&$crate::move_generator::move_set::MoveSet> {
self.move_sets.get(&piece.square())
}
fn bitboard(&self) -> chessfriend_bitboard::BitBoard {
self.move_sets.values().fold(
chessfriend_bitboard::BitBoard::empty(),

View file

@ -2,12 +2,14 @@
use super::{
bishop::ClassicalMoveGenerator as BishopMoveGenerator, king::KingMoveGenerator,
knight::KnightMoveGenerator, pawn::PawnMoveGenerator,
knight::KnightMoveGenerator, move_set::MoveSet, pawn::PawnMoveGenerator,
queen::ClassicalMoveGenerator as QueenMoveGenerator,
rook::ClassicalMoveGenerator as RookMoveGenerator,
};
use crate::{piece::Color, Move, Position};
use crate::{Move, Position};
use chessfriend_core::{Color, PlacedPiece, Shape};
#[derive(Clone, Debug)]
pub struct Moves<'pos> {
pawn_moves: PawnMoveGenerator<'pos>,
knight_moves: KnightMoveGenerator<'pos>,
@ -29,6 +31,17 @@ impl<'a> Moves<'a> {
}
}
pub(crate) fn moves_for_piece(&self, piece: &PlacedPiece) -> Option<&MoveSet> {
match piece.shape() {
Shape::Pawn => self.pawn_moves.moves_for_piece(piece),
Shape::Knight => self.knight_moves.moves_for_piece(piece),
Shape::Bishop => self.bishop_moves.moves_for_piece(piece),
Shape::Rook => self.rook_moves.moves_for_piece(piece),
Shape::Queen => self.queen_moves.moves_for_piece(piece),
Shape::King => self.king_moves.moves_for_piece(piece),
}
}
fn iter(&self) -> impl Iterator<Item = Move> + '_ {
self.pawn_moves
.iter()
@ -43,8 +56,8 @@ impl<'a> Moves<'a> {
#[cfg(test)]
mod tests {
use crate::{piece, position, r#move::AlgebraicMoveFormatter, Move, MoveBuilder};
use chessfriend_core::Square;
use crate::{position, r#move::AlgebraicMoveFormatter, Move, MoveBuilder};
use chessfriend_core::{piece, Square};
use std::collections::HashSet;
#[test]

View file

@ -1,5 +1,8 @@
use crate::{piece::PlacedPiece, Move};
// Eryn Wells <eryn@erynwells.me>
use crate::Move;
use chessfriend_bitboard::BitBoard;
use chessfriend_core::PlacedPiece;
#[derive(Clone, Debug, Eq, PartialEq)]
struct BitBoardSet {

View file

@ -1,10 +1,9 @@
// Eryn Wells <eryn@erynwells.me>
use crate::{
piece::{Color, Piece, Shape},
Move, MoveBuilder, Position,
};
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
use crate::{Move, MoveBuilder, Position};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Color, Piece, PlacedPiece, Shape};
enum MoveList {
Quiet = 0,

View file

@ -1,12 +1,9 @@
// Eryn Wells <eryn@erynwells.me>
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
use crate::{
piece::{Color, Piece, PlacedPiece},
MoveBuilder, Position,
};
use crate::{MoveBuilder, Position};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::Direction;
use chessfriend_core::{Color, Direction, Piece, PlacedPiece};
move_generator_declaration!(ClassicalMoveGenerator);
@ -67,8 +64,9 @@ impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> {
#[cfg(test)]
mod tests {
use super::*;
use crate::{piece, position, position::DiagramFormatter, Color};
use crate::{position, position::DiagramFormatter};
use chessfriend_bitboard::{bitboard, BitBoard};
use chessfriend_core::{piece, Color};
#[test]
fn classical_single_queen_bitboard() {

View file

@ -1,12 +1,9 @@
// Eryn Wells <eryn@erynwells.me>
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
use crate::{
piece::{Color, Piece, PlacedPiece},
MoveBuilder, Position,
};
use crate::{ MoveBuilder, Position};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::Direction;
use chessfriend_core::{Color, Direction, Piece, PlacedPiece};
move_generator_declaration!(ClassicalMoveGenerator);
@ -63,9 +60,9 @@ impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> {
#[cfg(test)]
mod tests {
use super::*;
use crate::{piece::Piece, position, position::DiagramFormatter, Color, Position};
use crate::{position, position::DiagramFormatter};
use chessfriend_bitboard::{bitboard, BitBoard};
use chessfriend_core::Square;
use chessfriend_core::Color;
#[test]
fn classical_single_rook_bitboard() {

View file

@ -1,13 +1,10 @@
// Eryn Wells <eryn@erynwells.me>
use crate::{
piece::{PlacedPiece, Shape},
position::flags::Flags,
r#move::Castle,
Color, MakeMoveError, Move, Piece, Position,
position::flags::Flags, r#move::Castle, sight::SightExt, MakeMoveError, Move, Position,
};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Direction, Square};
use chessfriend_core::{Color, Direction, Piece, PlacedPiece, Shape, Square};
/// A position builder that builds a new position by making a move.
#[derive(Clone)]
@ -249,7 +246,8 @@ impl<'p> From<&'p Position> for Builder<'p, NoMove> {
#[cfg(test)]
mod tests {
use super::*;
use crate::{r#move::Castle, MoveBuilder, PositionBuilder};
use crate::{position, r#move::Castle, MoveBuilder, PositionBuilder};
use chessfriend_core::piece;
#[test]
fn move_white_pawn_one_square() -> Result<(), MakeMoveError> {

View file

@ -1,12 +1,11 @@
// Eryn Wells <eryn@erynwells.me>
use crate::{
piece::{PlacedPiece, Shape},
position::{flags::Flags, piece_sets::PieceBitBoards},
r#move::Castle,
Color, MakeMoveError, Move, Piece, Position,
MakeMoveError, Move, Position,
};
use chessfriend_core::{Rank, Square};
use chessfriend_core::{piece, Color, Piece, PlacedPiece, Rank, Shape, Square};
use std::collections::BTreeMap;
#[derive(Clone)]
@ -130,6 +129,7 @@ impl Default for Builder {
#[cfg(test)]
mod tests {
use crate::PositionBuilder;
use chessfriend_core::piece;
#[test]
fn place_piece() {

View file

@ -40,7 +40,8 @@ impl<'a> fmt::Display for DiagramFormatter<'a> {
#[cfg(test)]
mod tests {
use super::*;
use crate::Position;
use crate::{position, Position};
use chessfriend_core::piece;
#[test]
#[ignore]

View file

@ -1,6 +1,7 @@
// Eryn Wells <eryn@erynwells.me>
use crate::{r#move::Castle, Color};
use crate::r#move::Castle;
use chessfriend_core::Color;
use std::fmt;
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
@ -40,7 +41,8 @@ impl Default for Flags {
#[cfg(test)]
mod tests {
use super::*;
use crate::{r#move::Castle, Color};
use crate::r#move::Castle;
use chessfriend_core::Color;
#[test]
fn castle_flags() {

View file

@ -1,11 +1,7 @@
// Eryn Wells <eryn@erynwells.me>
use crate::{
piece::{Piece, PlacedPiece},
Color,
};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::Square;
use chessfriend_core::{Color, Piece, PlacedPiece, Square};
#[derive(Debug, Eq, PartialEq)]
pub enum PlacePieceStrategy {

View file

@ -1,9 +1,8 @@
// Eryn Wells <eryn@erynwells.me>
use super::Position;
use crate::piece::{Color, Piece, PlacedPiece, Shape};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::Square;
use chessfriend_core::{Color, Piece, PlacedPiece, Shape, Square};
pub struct Pieces<'a> {
color: Color,
@ -74,9 +73,8 @@ impl<'a> Iterator for Pieces<'a> {
#[cfg(test)]
mod tests {
use super::*;
use crate::piece::{Color, Piece, Shape};
use crate::{Position, PositionBuilder};
use chessfriend_core::{piece, Color};
use std::collections::HashSet;
#[test]

View file

@ -2,15 +2,14 @@
use super::{flags::Flags, piece_sets::PieceBitBoards, Pieces};
use crate::{
move_generator::Moves,
piece::{Color, Piece, PlacedPiece, Shape},
move_generator::{MoveSet, Moves},
position::DiagramFormatter,
r#move::Castle,
sight::Sight,
sight::SightExt,
Move,
};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::Square;
use chessfriend_core::{Color, Piece, PlacedPiece, Shape, Square};
use std::{cell::OnceCell, fmt};
#[derive(Clone, Debug, Eq, PartialEq)]
@ -268,8 +267,8 @@ impl fmt::Display for Position {
#[cfg(test)]
mod tests {
use crate::{position, Castle, Color, Position};
use chessfriend_core::Square;
use crate::{position, test_position, Castle, Position};
use chessfriend_core::{piece, Color, Square};
#[test]
fn piece_on_square() {

View file

@ -1,17 +1,22 @@
// Eryn Wells <eryn@erynwells.me>
use crate::{
piece::{Color, PlacedPiece, Shape},
Position,
};
use crate::Position;
use chessfriend_bitboard::BitBoard;
use chessfriend_core::Direction;
use chessfriend_core::{Color, Direction, PlacedPiece, Shape};
pub(crate) trait Sight {
pub(crate) trait SightExt {
fn sight_in_position(&self, position: &Position) -> BitBoard;
fn white_pawn_sight_in_position(&self, position: &Position) -> BitBoard;
fn black_pawn_sight_in_position(&self, position: &Position) -> BitBoard;
fn knight_sight_in_position(&self, position: &Position) -> BitBoard;
fn bishop_sight_in_position(&self, position: &Position) -> BitBoard;
fn rook_sight_in_position(&self, position: &Position) -> BitBoard;
fn queen_sight_in_position(&self, position: &Position) -> BitBoard;
fn king_sight_in_position(&self, position: &Position) -> BitBoard;
}
impl Sight for PlacedPiece {
impl SightExt for PlacedPiece {
fn sight_in_position(&self, position: &Position) -> BitBoard {
match self.shape() {
Shape::Pawn => match self.color() {
@ -25,9 +30,7 @@ impl Sight for PlacedPiece {
Shape::King => self.king_sight_in_position(position),
}
}
}
impl PlacedPiece {
fn white_pawn_sight_in_position(&self, position: &Position) -> BitBoard {
let pawn: BitBoard = self.square().into();
let pawn = pawn.shift_north_west_one() | pawn.shift_north_east_one();
@ -175,9 +178,9 @@ mod tests {
}
mod pawn {
use crate::sight::Sight;
use crate::{sight::SightExt, test_position};
use chessfriend_bitboard::{bitboard, BitBoard};
use chessfriend_core::Square;
use chessfriend_core::{piece, Square};
sight_test!(e4_pawn, piece!(White Pawn on E4), bitboard!(D5, F5));
@ -235,8 +238,9 @@ mod tests {
#[macro_use]
mod knight {
use crate::sight::Sight;
use crate::sight::SightExt;
use chessfriend_bitboard::bitboard;
use chessfriend_core::piece;
sight_test!(
f6_knight,
@ -246,8 +250,9 @@ mod tests {
}
mod bishop {
use crate::sight::Sight;
use crate::sight::SightExt;
use chessfriend_bitboard::bitboard;
use chessfriend_core::piece;
sight_test!(
c2_bishop,
@ -257,8 +262,9 @@ mod tests {
}
mod rook {
use crate::sight::Sight;
use crate::sight::SightExt;
use chessfriend_bitboard::bitboard;
use chessfriend_core::piece;
sight_test!(
g3_rook,

62
core/src/colors.rs Normal file
View file

@ -0,0 +1,62 @@
// Eryn Wells <eryn@erynwells.me>
use crate::try_from_string;
use std::fmt;
#[derive(Debug, Eq, PartialEq)]
pub enum TryFromError {
InvalidCharacter,
ZeroLengthString,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Color {
White = 0,
Black = 1,
}
impl Color {
pub fn iter() -> impl Iterator<Item = Color> {
[Color::White, Color::Black].into_iter()
}
pub fn other(&self) -> Color {
match self {
Color::White => Color::Black,
Color::Black => Color::White,
}
}
}
impl Default for Color {
fn default() -> Self {
Color::White
}
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Color::White => "White",
Color::Black => "Black",
},
)
}
}
impl TryFrom<char> for Color {
type Error = TryFromError;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'w' | 'W' => Ok(Color::White),
'b' | 'B' => Ok(Color::Black),
_ => Err(TryFromError::InvalidCharacter),
}
}
}
try_from_string!(Color);

7
core/src/errors.rs Normal file
View file

@ -0,0 +1,7 @@
// Eryn Wells <eryn@erynwells.me>
#[derive(Debug, Eq, PartialEq)]
pub struct TryFromCharError;
#[derive(Debug, Eq, PartialEq)]
pub struct TryFromStrError;

View file

@ -1,3 +1,12 @@
mod coordinates;
// Eryn Wells <eryn@erynwells.me>
pub mod colors;
pub mod coordinates;
pub mod errors;
pub mod pieces;
mod macros;
pub use colors::Color;
pub use coordinates::{Direction, File, Rank, Square};
pub use pieces::{Piece, PlacedPiece, Shape};

32
core/src/macros.rs Normal file
View file

@ -0,0 +1,32 @@
// Eryn Wells <eryn@erynwells.me>
#[macro_export]
macro_rules! try_from_string {
($type:ty) => {
try_from_string!($type, &str);
try_from_string!($type, &String);
};
($type:ty, $from_type:ty) => {
impl TryFrom<$from_type> for $type {
type Error = $crate::errors::TryFromStrError;
fn try_from(value: $from_type) -> Result<Self, Self::Error> {
let first_char = value
.chars()
.nth(0)
.ok_or($crate::errors::TryFromStrError)?;
Self::try_from(first_char).map_err(|_| $crate::errors::TryFromStrError)
}
}
};
}
#[macro_export]
macro_rules! piece {
($color:ident $shape:ident) => {
$crate::Piece::new($crate::Color::$color, $crate::Shape::$shape)
};
($color:ident $shape:ident on $square:ident) => {
$crate::PlacedPiece::new(piece!($color $shape), $crate::Square::$square)
}
}

View file

@ -1,84 +1,7 @@
// Eryn Wells <eryn@erynwells.me>
use crate::display::{ASCIIDisplay, FENDisplay, UnicodeDisplay};
use chessfriend_core::Square;
use std::fmt;
use std::slice::Iter;
#[derive(Debug, Eq, PartialEq)]
pub enum TryFromError {
InvalidCharacter,
ZeroLengthString,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Color {
White = 0,
Black = 1,
}
impl Color {
pub fn iter() -> impl Iterator<Item = Color> {
[Color::White, Color::Black].into_iter()
}
pub fn other(&self) -> Color {
match self {
Color::White => Color::Black,
Color::Black => Color::White,
}
}
}
impl Default for Color {
fn default() -> Self {
Color::White
}
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Color::White => "White",
Color::Black => "Black",
},
)
}
}
impl TryFrom<char> for Color {
type Error = TryFromError;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'w' | 'W' => Ok(Color::White),
'b' | 'B' => Ok(Color::Black),
_ => Err(TryFromError::InvalidCharacter),
}
}
}
macro_rules! try_from_string {
($type:ty) => {
try_from_string!($type, &str);
try_from_string!($type, &String);
};
($type:ty, $from_type:ty) => {
impl TryFrom<$from_type> for $type {
type Error = TryFromError;
fn try_from(value: $from_type) -> Result<Self, Self::Error> {
let first_char = value.chars().nth(0).ok_or(TryFromError::ZeroLengthString)?;
Self::try_from(first_char)
}
}
};
}
try_from_string!(Color);
use crate::{errors::TryFromCharError, try_from_string, Color, Square};
use std::{fmt, slice::Iter};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Shape {
@ -124,7 +47,7 @@ impl Shape {
}
impl TryFrom<char> for Shape {
type Error = TryFromError;
type Error = TryFromCharError;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
@ -134,7 +57,7 @@ impl TryFrom<char> for Shape {
'R' | 'r' => Ok(Shape::Rook),
'Q' | 'q' => Ok(Shape::Queen),
'K' | 'k' => Ok(Shape::King),
_ => Err(TryFromError::InvalidCharacter),
_ => Err(TryFromCharError),
}
}
}
@ -219,18 +142,6 @@ impl Piece {
impl fmt::Display for Piece {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
UnicodeDisplay::fmt(self, f)
}
}
impl ASCIIDisplay for Piece {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", Into::<char>::into(self.shape()))
}
}
impl UnicodeDisplay for Piece {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
@ -252,20 +163,6 @@ impl UnicodeDisplay for Piece {
}
}
impl FENDisplay for Piece {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let ascii = self.shape().to_ascii();
write!(
f,
"{}",
match self.color {
Color::White => ascii.to_ascii_uppercase(),
Color::Black => ascii.to_ascii_lowercase(),
}
)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct PlacedPiece {
piece: Piece,