Skip to content

Commit

Permalink
merge: branch ataxx-0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
raklaptudirm committed May 15, 2024
2 parents 9e631fd + 3ea9038 commit d8edeef
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
1 change: 1 addition & 0 deletions ataxx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ keywords = ["ataxx"]
categories = ["games"]

[dependencies]
thiserror = "1.0"
num-traits = "0.2"
num-derive = "0.4"
strum = "0.26"
Expand Down
46 changes: 20 additions & 26 deletions ataxx/src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use std::str::FromStr;

use strum::IntoEnumIterator;

use thiserror::Error;

#[rustfmt::skip]
use crate::{
BitBoard, Color, File, Hash, Move,
Expand Down Expand Up @@ -599,33 +601,25 @@ impl Position {

/// PositionParseErr represents an error encountered while parsing
/// the given FEN position field into a valid Position.
#[derive(Debug)]
#[derive(Error, Debug)]
pub enum PositionParseError {
TooManyFields,
#[error("expected 3 fields, found {0}")]
TooManyFields(usize),

/// A jump spec was too big for the current rank.
#[error("a jump value was too long and overshot")]
JumpTooLong,
/// Invalid character in Square spec.
InvalidColorIdent,
/// Insufficient number of Square spec entries.
FileDataIncomplete,
/// More fields in string spec than Ranks.
TooManyRanks,

BadSideToMove(ColorParseError),
BadHalfMoveClock(ParseIntError),
}

impl From<ColorParseError> for PositionParseError {
fn from(value: ColorParseError) -> Self {
Self::BadSideToMove(value)
}
}
#[error("invalid color identifier '{0}'")]
InvalidColorIdent(char),
#[error("insufficient data to fill the entire {0} file")]
FileDataIncomplete(File),
#[error("expected 7 ranks, found more")]
TooManyRanks,

impl From<ParseIntError> for PositionParseError {
fn from(value: ParseIntError) -> Self {
Self::BadHalfMoveClock(value)
}
#[error("parsing side to move: {0}")]
BadSideToMove(#[from] ColorParseError),
#[error("parsing half-move clock: {0}")]
BadHalfMoveClock(#[from] ParseIntError),
}

// FromStr implements parsing of the position field in a FEN.
Expand All @@ -636,7 +630,7 @@ impl FromStr for Position {
let parts = s.split(' ').collect::<Vec<&str>>();

if parts.len() != 3 {
return Err(PositionParseError::TooManyFields);
return Err(PositionParseError::TooManyFields(parts.len()));
}

let pos = parts[0];
Expand Down Expand Up @@ -678,7 +672,7 @@ impl FromStr for Position {
}
}

_ => return Err(PositionParseError::InvalidColorIdent),
_ => return Err(PositionParseError::InvalidColorIdent(data)),
}

// On to the next Square spec in the Rank spec.
Expand All @@ -687,8 +681,8 @@ impl FromStr for Position {

// After rank data runs out, file pointer should be
// at the last file, i.e, rank is completely filled.
if file.is_ok() {
return Err(PositionParseError::FileDataIncomplete);
if let Ok(file) = file {
return Err(PositionParseError::FileDataIncomplete(file));
}

// Switch rank pointer and reset file pointer.
Expand Down
10 changes: 7 additions & 3 deletions ataxx/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use std::str::FromStr;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;

use thiserror::Error;

use crate::type_macros;

/// Color represents all the possible colors that an ataxx piece can have,
Expand Down Expand Up @@ -60,10 +62,12 @@ impl ops::Not for Color {
}
}

#[derive(Debug)]
#[derive(Error, Debug)]
pub enum ColorParseError {
#[error("color identifier string has more than 1 character")]
StringTooLong,
StringFormatInvalid,
#[error("unknown color identifier '{0}'")]
StringFormatInvalid(String),
}

impl fmt::Display for Color {
Expand Down Expand Up @@ -105,7 +109,7 @@ impl FromStr for Color {
match s {
"x" | "X" | "b" | "B" => Ok(Color::Black),
"o" | "O" | "w" | "W" => Ok(Color::White),
_ => Err(ColorParseError::StringFormatInvalid),
_ => Err(ColorParseError::StringFormatInvalid(s.to_string())),
}
}
}

0 comments on commit d8edeef

Please sign in to comment.