refactor: use Rust idiomatic traits for Square to string conversion
This commit is contained in:
parent
23f4ff68b4
commit
23fec6541b
@ -267,7 +267,7 @@ impl ToFen for BoardState {
|
|||||||
let turn = char::from(self.turn);
|
let turn = char::from(self.turn);
|
||||||
let castle = self.castle.to_string();
|
let castle = self.castle.to_string();
|
||||||
let ep_square = match self.ep_square {
|
let ep_square = match self.ep_square {
|
||||||
Some(sqr) => sqr.to_algebraic(),
|
Some(sqr) => sqr.to_string(),
|
||||||
None => "-".to_string(),
|
None => "-".to_string(),
|
||||||
};
|
};
|
||||||
let half_move = self.half_moves.to_string();
|
let half_move = self.half_moves.to_string();
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#![deny(rust_2018_idioms)]
|
#![deny(rust_2018_idioms)]
|
||||||
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
pub mod fen;
|
pub mod fen;
|
||||||
pub mod movegen;
|
pub mod movegen;
|
||||||
@ -174,14 +175,16 @@ impl Square {
|
|||||||
assert!(rem <= 7);
|
assert!(rem <= 7);
|
||||||
(div, rem)
|
(div, rem)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Square {
|
||||||
/// Convert square to typical human-readable form (e.g. `e4`).
|
/// Convert square to typical human-readable form (e.g. `e4`).
|
||||||
fn to_algebraic(self) -> String {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
|
let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
|
||||||
let (row, col) = self.to_row_col();
|
let (row, col) = self.to_row_col();
|
||||||
let rank = (row + 1).to_string();
|
let rank = (row + 1).to_string();
|
||||||
let file = letters[col];
|
let file = letters[col];
|
||||||
format!("{file}{rank}")
|
write!(f, "{}{}", file, rank)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -496,7 +499,7 @@ mod tests {
|
|||||||
fn test_to_from_algebraic() {
|
fn test_to_from_algebraic() {
|
||||||
let test_cases = [("a1", 0), ("a8", 56), ("h1", 7), ("h8", 63)];
|
let test_cases = [("a1", 0), ("a8", 56), ("h1", 7), ("h8", 63)];
|
||||||
for (sqr, idx) in test_cases {
|
for (sqr, idx) in test_cases {
|
||||||
assert_eq!(Square::try_from(idx).unwrap().to_algebraic(), sqr);
|
assert_eq!(Square::try_from(idx).unwrap().to_string(), sqr);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
sqr.parse::<Square>().unwrap(),
|
sqr.parse::<Square>().unwrap(),
|
||||||
Square::try_from(idx).unwrap()
|
Square::try_from(idx).unwrap()
|
||||||
|
Loading…
Reference in New Issue
Block a user