open_pql/base/
card_idx.rs

1use super::*;
2
3/// Card index representation (0-51)
4#[derive(Copy, Clone, Debug, PartialEq, Eq)]
5pub struct CardIdx(u8);
6
7impl CardIdx {
8    /// Converts to a u8 value
9    pub const fn to_u8(self) -> u8 {
10        self.0
11    }
12
13    /// Converts to a usize value
14    pub const fn to_usize(self) -> usize {
15        self.0 as usize
16    }
17
18    /// Converts to a Card
19    pub fn to_card(self) -> Card {
20        let rank = self.0 / 4;
21        let suit = self.0 % 4;
22        Card::from_indices(rank, suit)
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29    use crate::*;
30
31    #[test]
32    fn test_to_card() {
33        for i in 0..N_CARDS {
34            assert_eq!(CardIdx(i).to_card(), Card::ARR_ALL[i as usize]);
35        }
36    }
37}