Skip to content

Commit

Permalink
merge: branch 'ataxx-0.2.0' into uai
Browse files Browse the repository at this point in the history
  • Loading branch information
raklaptudirm committed Apr 9, 2024
2 parents 3c85caf + 0c530b0 commit ae29f9e
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions ataxx/src/move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use std::fmt;
use std::mem::MaybeUninit;
use std::str::FromStr;

use crate::Square;

Expand Down Expand Up @@ -136,6 +137,62 @@ impl Move {
}
}

#[derive(Debug)]
pub enum MoveParseError {
BadLength(usize),
BadSourceSquare(String),
BadTargetSquare(String),
}

impl FromStr for Move {
type Err = MoveParseError;

/// from_str converts the given string representation of a Move into a [Move].
/// The formats supported are '0000' for a [Move::PASS], <target> for a singular
/// Move, and <source><target> for a jump Move. For how <source> and <target>
/// are parsed, take a look at [`Square::FromStr`](Square::from_str). This
/// function can be treated as the inverse of [`Move::Display`].
/// ```
/// use ataxx::*;
/// use std::str::FromStr;
///
/// let pass = Move::PASS;
/// let sing = Move::new_single(Square::A1);
/// let jump = Move::new(Square::A1, Square::A3);
///
/// assert_eq!(Move::from_str(&pass.to_string()).unwrap(), pass);
/// assert_eq!(Move::from_str(&sing.to_string()).unwrap(), sing);
/// assert_eq!(Move::from_str(&jump.to_string()).unwrap(), jump);
/// ```
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == "0000" {
return Ok(Move::PASS);
};

if s.len() != 2 && s.len() != 4 {
return Err(MoveParseError::BadLength(s.len()));
}

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

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())),
};

Ok(Move::new(source, target))
}
}

impl fmt::Display for Move {
/// Display formats the given Move in a human-readable manner. The format used
/// for displaying jump moves is `<source><target>`, while a singular Move is
Expand Down

0 comments on commit ae29f9e

Please sign in to comment.