Skip to content

Commit

Permalink
chore: fix more errors
Browse files Browse the repository at this point in the history
  • Loading branch information
raklaptudirm committed May 27, 2024
1 parent f5353b3 commit f417f68
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
16 changes: 4 additions & 12 deletions ataxx/src/move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::str::FromStr;

use thiserror::Error;

use crate::Square;
use crate::{Square, SquareParseError};

/// Move represents an Ataxx move which can be played on the Board.
#[derive(Copy, Clone, PartialEq, Eq, Default)]
Expand Down Expand Up @@ -143,9 +143,7 @@ pub enum MoveParseError {
#[error("length of move string should be 2 or 4, not {0}")]
BadLength(usize),
#[error("bad source square string \"{0}\"")]
BadSourceSquare(String),
#[error("bad target square string \"{0}\"")]
BadTargetSquare(String),
BadSquare(#[from] SquareParseError),
}

impl FromStr for Move {
Expand Down Expand Up @@ -179,20 +177,14 @@ impl FromStr for Move {
}

let source = &s[..2];
let source = match Square::from_str(source) {
Ok(sq) => sq,
Err(_err) => return Err(MoveParseError::BadSourceSquare(source.to_string())),
};
let source = Square::from_str(source)?;

if s.len() < 4 {
return Ok(Move::new_single(source));
}

let target = &s[2..];
let target = match Square::from_str(target) {
Ok(sq) => sq,
Err(_err) => return Err(MoveParseError::BadTargetSquare(target.to_string())),
};
let target = Square::from_str(target)?;

Ok(Move::new(source, target))
}
Expand Down
31 changes: 15 additions & 16 deletions ataxx/src/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::str::FromStr;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use strum_macros::EnumIter;
use thiserror::Error;

use crate::type_macros;

Expand Down Expand Up @@ -113,11 +114,14 @@ impl Square {

/// SquareParseError represents the various errors that can
/// be encountered while parsing a given string into a Square.
#[derive(Debug)]
#[derive(Error, Debug)]
pub enum SquareParseError {
#[error("wrong square string size")]
WrongStringSize,
FileParseError(FileParseError),
RankParseError(RankParseError),
#[error("{0}")]
FileParseError(#[from] FileParseError),
#[error("{0}")]
RankParseError(#[from] RankParseError),
}

impl FromStr for Square {
Expand All @@ -138,17 +142,8 @@ impl FromStr for Square {
return Err(SquareParseError::WrongStringSize);
}

// Parse the File specification.
let file = match File::from_str(&s[..=0]) {
Ok(file) => file,
Err(err) => return Err(SquareParseError::FileParseError(err)),
};

// Parse the Rank specification.
let rank = match Rank::from_str(&s[1..]) {
Ok(rank) => rank,
Err(err) => return Err(SquareParseError::RankParseError(err)),
};
let file = File::from_str(&s[..1])?; // Parse the File specification.
let rank = Rank::from_str(&s[1..])?; // Parse the Rank specification.

Ok(Square::new(file, rank))
}
Expand Down Expand Up @@ -213,9 +208,11 @@ impl File {

/// FileParseError represents the various errors that can
/// be encountered while parsing a given string into a File.
#[derive(Debug)]
#[derive(Error, Debug)]
pub enum FileParseError {
#[error("wrong string size for file identifier")]
WrongStringSize,
#[error("invalid file identifier string")]
InvalidFileString,
}

Expand Down Expand Up @@ -305,9 +302,11 @@ impl Rank {

/// RankParseError represents the various errors that can
/// be encountered while parsing a given string into a Rank.
#[derive(Debug)]
#[derive(Error, Debug)]
pub enum RankParseError {
#[error("wrong string size for rank identifier")]
WrongStringSize,
#[error("invalid rank identifier string")]
InvalidRankString,
}

Expand Down

0 comments on commit f417f68

Please sign in to comment.