[board, core] Update error types to use thiserror::Error
This commit is contained in:
parent
539b1fca6e
commit
b229049e27
6 changed files with 83 additions and 15 deletions
|
@ -5,6 +5,7 @@ use chessfriend_core::{
|
|||
coordinates::ParseSquareError, piece, Color, File, Piece, PlacedPiece, Rank, Square,
|
||||
};
|
||||
use std::fmt::Write;
|
||||
use thiserror::Error;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! fen {
|
||||
|
@ -13,18 +14,24 @@ macro_rules! fen {
|
|||
};
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
#[derive(Clone, Debug, Error, Eq, PartialEq)]
|
||||
pub enum ToFenStrError {
|
||||
FmtError(std::fmt::Error),
|
||||
#[error("{0}")]
|
||||
FmtError(#[from] std::fmt::Error),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
#[derive(Clone, Debug, Error, Eq, PartialEq)]
|
||||
pub enum FromFenStrError {
|
||||
#[error("missing {0} field")]
|
||||
MissingField(Field),
|
||||
#[error("missing piece placement")]
|
||||
MissingPlacement,
|
||||
#[error("invalid value")]
|
||||
InvalidValue,
|
||||
ParseIntError(std::num::ParseIntError),
|
||||
ParseSquareError(ParseSquareError),
|
||||
#[error("{0}")]
|
||||
ParseIntError(#[from] std::num::ParseIntError),
|
||||
#[error("{0}")]
|
||||
ParseSquareError(#[from] ParseSquareError),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
|
@ -37,6 +44,19 @@ pub enum Field {
|
|||
FullMoveCounter,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Field {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Field::Placements => write!(f, "Placements"),
|
||||
Field::PlayerToMove => write!(f, "Player To Move"),
|
||||
Field::CastlingRights => write!(f, "Castling Rights"),
|
||||
Field::EnPassantSquare => write!(f, "En Passant Square"),
|
||||
Field::HalfMoveClock => write!(f, "Half Move Clock"),
|
||||
Field::FullMoveCounter => write!(f, "Full move Counter"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ToFenStr {
|
||||
type Error;
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ mod board;
|
|||
mod piece_sets;
|
||||
|
||||
pub use board::Board;
|
||||
pub use piece_sets::{PlacePieceError, PlacePieceStrategy};
|
||||
|
||||
use castle::Castle;
|
||||
use en_passant::EnPassant;
|
||||
|
|
|
@ -6,6 +6,7 @@ use self::mailbox::Mailbox;
|
|||
use chessfriend_bitboard::{BitBoard, IterationDirection};
|
||||
use chessfriend_core::{Color, Piece, Shape, Square};
|
||||
use std::ops::BitOr;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
||||
pub enum PlacePieceStrategy {
|
||||
|
@ -14,9 +15,10 @@ pub enum PlacePieceStrategy {
|
|||
PreserveExisting,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
#[derive(Debug, Error, Eq, PartialEq)]
|
||||
pub enum PlacePieceError {
|
||||
ExisitingPiece(PlacedPiece),
|
||||
#[error("cannot place piece on {square} with existing {piece}")]
|
||||
ExisitingPiece { piece: Piece, square: Square },
|
||||
}
|
||||
|
||||
/// The internal data structure of a [Board] that efficiently manages the
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue