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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::ops::Not;
use crate::rank::Rank;
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Hash, Default)]
pub enum Color {
#[default]
White,
Black,
}
pub const COLORS: [Color; 2] = [Color::White, Color::Black];
impl Not for Color {
type Output = Color;
/// Get the opposite color.
#[inline]
fn not(self) -> Self::Output {
match self {
Color::White => Color::Black,
Color::Black => Color::White,
}
}
}
impl Color {
/// Convert [`Color`] to a [`usize`].
#[inline]
pub fn to_index(self) -> usize {
self as usize
}
/// Get the back [`Rank`] for the current [`Color`].
///
/// # Example
/// ```
/// use chessframe::{color::Color, rank::Rank};
///
/// assert_eq!(Color::White.to_backrank(), Rank::First);
/// assert_eq!(Color::Black.to_backrank(), Rank::Eighth);
/// ```
#[inline]
pub fn to_backrank(self) -> Rank {
match self {
Color::White => Rank::First,
Color::Black => Rank::Eighth,
}
}
/// Get the second [`Rank`] for the current [`Color`].
///
/// # Example
/// ```
/// use chessframe::{color::Color, rank::Rank};
///
/// assert_eq!(Color::White.to_second_rank(), Rank::Second);
/// assert_eq!(Color::Black.to_second_rank(), Rank::Seventh);
/// ```
#[inline]
pub fn to_second_rank(self) -> Rank {
match self {
Color::White => Rank::Second,
Color::Black => Rank::Seventh,
}
}
/// Get the fourth [`Rank`] for the current [`Color`].
///
/// # Example
/// ```
/// use chessframe::{color::Color, rank::Rank};
///
/// assert_eq!(Color::White.to_fourth_rank(), Rank::Fourth);
/// assert_eq!(Color::Black.to_fourth_rank(), Rank::Fifth);
/// ```
#[inline]
pub fn to_fourth_rank(self) -> Rank {
match self {
Color::White => Rank::Fourth,
Color::Black => Rank::Fifth,
}
}
/// Get the offset for the current [`Color`].
#[inline]
pub fn to_offset(self) -> usize {
match self {
Color::White => 0,
Color::Black => 6,
}
}
}