chess_notation_parser/flag.rs
1//! Flag bitmask indicates special information about the turn
2
3/// Bitmask flag depicts special capabilities of a chess turn (*check*,
4/// *checkmate*, *capture*)
5///
6/// *Note:* `CHECK` and `CHECKMATE` are mutually exclusive flags.
7#[allow(non_snake_case)]
8pub mod Flag {
9 /// Turn will result in a check
10 pub const NONE: u8 = 0;
11
12 /// Turn will result in a check
13 pub const CHECK: u8 = 0x01 << 0;
14
15 /// Turn will result in a checkmate
16 pub const CHECKMATE: u8 = 0x01 << 1;
17
18 /// Turn captures a piece of the opponent
19 pub const CAPTURE: u8 = 0x01 << 2;
20}
21
22/// Implements function to easily check *chess turn* flags
23pub trait FlagCheck {
24 /// Getter function for `flags`
25 fn get_flags(&self) -> u8;
26
27 /// Returns true if flag is present
28 ///
29 /// # Example
30 /// ```
31 /// use chess_notation_parser::{Turn, Move, Square, Piece, Flag, FlagCheck};
32 ///
33 /// let r#move = Move {
34 /// who: Piece::King,
35 /// dst: Square::D5,
36 /// flags: Flag::CAPTURE,
37 /// src: None,
38 /// promotion: None
39 /// };
40 ///
41 /// assert!(r#move.check_flag(Flag::CAPTURE));
42 /// assert!(!r#move.check_flag(Flag::CHECK));
43 /// ```
44 fn check_flag(&self, flag: u8) -> bool {
45 self.get_flags() & flag != 0
46 }
47}