1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//! # Pieces
//!
//! A [`Piece`] pairs a [`Role`] (pawn, knight, …) with a [`Color`] (white,
//! black). It is the unit placed on a [`Board`](crate::board::Board) square.
use crate::{color::Color, role::Role};
/// A single chess piece: a role together with the color of the player who
/// owns it.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Piece {
/// The piece type (pawn, knight, bishop, rook, queen, or king).
pub role: Role,
/// The owning side.
pub color: Color,
}
impl Piece {
/// Returns the FEN-style character for this piece: lowercase letter for
/// White, uppercase for Black.
///
/// Note that this is the inverse of the usual FEN convention; consumers
/// rendering FEN should flip the case themselves.
///
/// # Example
/// ```
/// # use ruchess::piece::Piece;
/// # use ruchess::role::Role;
/// # use ruchess::color::Color;
/// let white_king = Piece { role: Role::King, color: Color::White };
/// let black_king = Piece { role: Role::King, color: Color::Black };
/// assert_eq!(white_king.as_char(), 'k');
/// assert_eq!(black_king.as_char(), 'K');
/// ```
pub fn as_char(&self) -> char {
let c = self.role.as_ascii();
match self.color {
Color::White => c,
Color::Black => c.to_ascii_uppercase(),
}
}
}