openpql_prelude/card/
suit_idx.rs

1use super::{Display, Hash, Idx, Suit};
2
3/// Suit index representation.
4///
5/// Converts suits to numeric indices (0-3).
6#[derive(
7    Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd, Hash, Display, Default,
8)]
9pub struct SuitIdx(pub(crate) Idx);
10
11impl SuitIdx {
12    pub const fn to_suit(self) -> Option<Suit> {
13        match self.0 {
14            0 => Some(Suit::S),
15            1 => Some(Suit::H),
16            2 => Some(Suit::D),
17            3 => Some(Suit::C),
18            _ => None,
19        }
20    }
21}
22
23impl From<Suit> for SuitIdx {
24    fn from(suit: Suit) -> Self {
25        Self(suit as Idx)
26    }
27}
28
29#[cfg(test)]
30#[cfg_attr(coverage_nightly, coverage(off))]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn test_from_suit() {
36        assert_eq!(SuitIdx::from(Suit::S).0, 0);
37        assert_eq!(SuitIdx::from(Suit::H).0, 1);
38        assert_eq!(SuitIdx::from(Suit::D).0, 2);
39        assert_eq!(SuitIdx::from(Suit::C).0, 3);
40    }
41
42    #[test]
43    fn test_to_suit() {
44        assert_eq!(SuitIdx(0).to_suit(), Some(Suit::S));
45        assert_eq!(SuitIdx(1).to_suit(), Some(Suit::H));
46        assert_eq!(SuitIdx(2).to_suit(), Some(Suit::D));
47        assert_eq!(SuitIdx(3).to_suit(), Some(Suit::C));
48
49        assert_eq!(SuitIdx(-1).to_suit(), None);
50        assert_eq!(SuitIdx(5).to_suit(), None);
51    }
52}