standard_card 1.4.0

A Lightweight Library for Efficient Card Representation
Documentation
/// Create the card from given rank and suit
///
/// # Arguments
/// * `rank` - Rank of the card. Valid value \[0, 12\]
/// * `suit` - Suit of the card. Valid value \[0, 3\]
///
/// # Returns
/// Card number if the arguments are valid, otherwise 0
///
/// # Example
/// ```
/// use standard_card::card::create;
///
/// let card = create(3, 1);
/// assert_eq!(541511, card);
/// ```
pub fn create(rank: i32, suit: i32) -> i32 {
    if let 0..=12 = rank {
        if let 0..=3 = suit {
            let primes: [i32; 13] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41];
            let prime: i32 = primes[rank as usize];

            return prime | (suit << 6) | (rank << 8) | (1 << (15 - suit)) | (1 << (16 + rank));
        }
    }

    return 0;
}

#[cfg(test)]
mod create {
    use crate::card::create;

    #[test]
    fn ace_of_clubs() {
        // Act
        let actual = create(12, 0);

        // Assert
        assert_eq!(actual, 268471337);
    }

    #[test]
    fn ace_of_diamonds() {
        // Act
        let actual = create(12, 1);

        // Assert
        assert_eq!(actual, 268455017);
    }

    #[test]
    fn ace_of_hearts() {
        // Act
        let actual = create(12, 2);

        // Assert
        assert_eq!(actual, 268446889);
    }

    #[test]
    fn ace_of_spades() {
        // Act
        let actual = create(12, 3);

        // Assert
        assert_eq!(actual, 268442857);
    }
}