open_pql/base/
suit_idx.rs

1use super::{Display, Hash, Suit};
2
3#[derive(
4    Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd, Hash, Display, Default,
5)]
6pub struct SuitIdx(u8);
7
8#[allow(unused)]
9impl SuitIdx {
10    /// Creates a new `SuitIdx` from a u8 value
11    pub(crate) const fn new(value: u8) -> Self {
12        Self(value)
13    }
14
15    /// Converts to a u8 value
16    pub(crate) const fn to_u8(self) -> u8 {
17        self.0
18    }
19
20    /// Converts to a usize value
21    pub(crate) const fn to_usize(self) -> usize {
22        self.0 as usize
23    }
24
25    /// Converts to a Card
26    pub(crate) fn to_suit(self) -> Suit {
27        Suit::from_u8(self.0)
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34    use crate::*;
35
36    #[test]
37    fn test_suit_idx_new() {
38        let suit_idx = SuitIdx::new(2);
39        assert_eq!(suit_idx.0, 2);
40    }
41
42    #[test]
43    fn test_suit_idx_to_u8() {
44        let suit_idx = SuitIdx::new(1);
45        assert_eq!(suit_idx.to_u8(), 1);
46    }
47
48    #[test]
49    fn test_suit_idx_to_usize() {
50        let suit_idx = SuitIdx::new(3);
51        assert_eq!(suit_idx.to_usize(), 3);
52    }
53
54    #[test]
55    fn test_suit_idx_to_suit() {
56        let suit_idx = SuitIdx::new(0);
57        let suit = suit_idx.to_suit();
58        assert_eq!(suit, Suit::from_u8(0));
59    }
60
61    #[test]
62    fn test_suit_idx_default() {
63        let suit_idx = SuitIdx::default();
64        assert_eq!(suit_idx.to_u8(), 0);
65    }
66
67    #[test]
68    fn test_suit_idx_ordering() {
69        let suit_idx1 = SuitIdx::new(0);
70        let suit_idx2 = SuitIdx::new(1);
71        let suit_idx3 = SuitIdx::new(2);
72
73        assert!(suit_idx1 < suit_idx2);
74        assert!(suit_idx2 < suit_idx3);
75        assert!(suit_idx1 < suit_idx3);
76    }
77}