<commit_before>//! This module represents a basic, rule-agnostic 32-cards system.

use rand::{thread_rng, Rng, IsaacRng, SeedableRng};
use std::str::FromStr;
use std::num::Wrapping;
use std::string::ToString;
use rustc_serialize;

/// One of the four Suits: Heart, Spade, Diamond, Club.
#[derive(PartialEq,Clone,Copy,Debug)]
#[repr(u32)]
pub enum Suit {
    Heart = 1 << 0,
    Spade = 1 << 8,
    Diamond = 1 << 16,
    Club = 1 << 24,
}

impl rustc_serialize::Encodable for Suit {
    fn encode<S: rustc_serialize::Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
        (*self as u32).encode(s)
    }
}

impl rustc_serialize::Decodable for Suit {
    fn decode<D: rustc_serialize::Decoder>(d: &mut D) -> Result<Self, D::Error> {
        match try!(d.read_u32()) {
            0x00000001 => Ok(Suit::Heart),
            0x00000100 => Ok(Suit::Spade),
            0x00010000 => Ok(Suit::Diamond),
            0x01000000 => Ok(Suit::Club),
            other => Err(d.error(&format!("unknown suit: {}", other))),
        }
    }
}

impl Suit {
    /// Returns the suit corresponding to the number:
    ///
    /// * `0`: Heart
    /// * `1`: Spade
    /// * `2`: Diamond
    /// * `3`: Club
    ///
    /// # Panics
    ///
    /// If `n >= 4`.
    pub fn from_n(n: u32) -> Self {
        match n {
            0 => Suit::Heart,
            1 => Suit::Spade,
            2 => Suit::Diamond,
            3 => Suit::Club,
            other => panic!("bad suit number: {}", other),
        }
    }

    /// Returns a UTF-8 character representing the suit.
    pub fn to_string(self) -> String {
        match self {
            Suit::Heart => "♥",
            Suit::Spade => "♠",
            Suit::Diamond => "♦",
            Suit::Club => "♣",
        }
        .to_string()
    }
}

impl FromStr for Suit {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, String> {
        match s {
            "H" | "h" | "heart" | "Suit::Heart" | "Heart" => Ok(Suit::Heart),
            "C" | "c" | "club" | "Suit::Club" | "Club" => Ok(Suit::Club),
            "S" | "s" | "spade" | "Suit::Spade" | "Spade" => Ok(Suit::Spade),
            "D" | "d" | "diamond" | "Suit::Diamond" | "Diamond" => Ok(Suit::Diamond),
            _ => Err(format!("invalid suit: {}", s)),
        }
    }
}


/// Rank of a card in a suit.
#[derive(PartialEq,Clone,Copy,Debug)]
#[repr(u32)]
pub enum Rank {
    Rank7 = 1 << 0,
    Rank8 = 1 << 1,
    Rank9 = 1 << 2,
    RankJ = 1 << 3,
    RankQ = 1 << 4,
    RankK = 1 << 5,
    RankX = 1 << 6,
    RankA = 1 << 7,
}

/// Bit RANK_MASK over all ranks
const RANK_MASK: u32 = 255;

impl Rank {
    /// Returns the rank corresponding to the given number:
    ///
    /// * `0`: 7
    /// * `1`: 8
    /// * `2`: 9
    /// * `3`: Jack
    /// * `4`: Queen
    /// * `5`: King
    /// * `6`: 10
    /// * `7`: Ace
    ///
    /// # Panics
    ///
    /// If `n >= 8`.
    pub fn from_n(n: u32) -> Self {
        match n {
            0 => Rank::Rank7,
            1 => Rank::Rank8,
            2 => Rank::Rank9,
            3 => Rank::RankJ,
            4 => Rank::RankQ,
            5 => Rank::RankK,
            6 => Rank::RankX,
            7 => Rank::RankA,
            other => panic!("invalid rank number: {}", other),
        }
    }

    fn from_discriminant(rank: u32) -> Self {
        match rank {
            1 => Rank::Rank7,
            2 => Rank::Rank8,<commit_msg>Add minimal comments to Suit / Rank<commit_after>//! This module represents a basic, rule-agnostic 32-cards system.

use rand::{thread_rng, Rng, IsaacRng, SeedableRng};
use std::str::FromStr;
use std::num::Wrapping;
use std::string::ToString;
use rustc_serialize;

/// One of the four Suits: Heart, Spade, Diamond, Club.
#[derive(PartialEq,Clone,Copy,Debug)]
#[repr(u32)]
pub enum Suit {
    /// The suit of hearts
    Heart = 1 << 0,
    /// The suit of spades
    Spade = 1 << 8,
    /// The suit of diamonds
    Diamond = 1 << 16,
    /// The suit of clubs
    Club = 1 << 24,
}

impl rustc_serialize::Encodable for Suit {
    fn encode<S: rustc_serialize::Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
        (*self as u32).encode(s)
    }
}

impl rustc_serialize::Decodable for Suit {
    fn decode<D: rustc_serialize::Decoder>(d: &mut D) -> Result<Self, D::Error> {
        match try!(d.read_u32()) {
            0x00000001 => Ok(Suit::Heart),
            0x00000100 => Ok(Suit::Spade),
            0x00010000 => Ok(Suit::Diamond),
            0x01000000 => Ok(Suit::Club),
            other => Err(d.error(&format!("unknown suit: {}", other))),
        }
    }
}

impl Suit {
    /// Returns the suit corresponding to the number:
    ///
    /// * `0`: Heart
    /// * `1`: Spade
    /// * `2`: Diamond
    /// * `3`: Club
    ///
    /// # Panics
    ///
    /// If `n >= 4`.
    pub fn from_n(n: u32) -> Self {
        match n {
            0 => Suit::Heart,
            1 => Suit::Spade,
            2 => Suit::Diamond,
            3 => Suit::Club,
            other => panic!("bad suit number: {}", other),
        }
    }

    /// Returns a UTF-8 character representing the suit.
    pub fn to_string(self) -> String {
        match self {
            Suit::Heart => "♥",
            Suit::Spade => "♠",
            Suit::Diamond => "♦",
            Suit::Club => "♣",
        }
        .to_string()
    }
}

impl FromStr for Suit {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, String> {
        match s {
            "H" | "h" | "heart" | "Suit::Heart" | "Heart" => Ok(Suit::Heart),
            "C" | "c" | "club" | "Suit::Club" | "Club" => Ok(Suit::Club),
            "S" | "s" | "spade" | "Suit::Spade" | "Spade" => Ok(Suit::Spade),
            "D" | "d" | "diamond" | "Suit::Diamond" | "Diamond" => Ok(Suit::Diamond),
            _ => Err(format!("invalid suit: {}", s)),
        }
    }
}


/// Rank of a card in a suit.
#[derive(PartialEq,Clone,Copy,Debug)]
#[repr(u32)]
pub enum Rank {
    /// 7
    Rank7 = 1 << 0,
    /// 8
    Rank8 = 1 << 1,
    /// 9
    Rank9 = 1 << 2,
    /// Jack
    RankJ = 1 << 3,
    /// Queen
    RankQ = 1 << 4,
    /// King
    RankK = 1 << 5,
    /// 10
    RankX = 1 << 6,
    /// Ace
    RankA = 1 << 7,
}

/// Bit RANK_MASK over all ranks
const RANK_MASK: u32 = 255;

impl Rank {
    /// Returns the rank corresponding to the given number:
    ///
    /// * `0`: 7
    /// * `1`: 8
    /// * `2`: 9
    /// * `3`: Jack
    /// * `4`: Queen
    /// * `5`: King
    /// * `6`: 10
    /// * `7`: Ace
    ///
    /// # Panics
    ///
    /// If `n >= 8`.
    pub fn from_n(n: u32) -> Self {
        match n {
            0 => Rank::Rank7,
            1 => Rank::Rank8,
            2 => Rank::Rank9,
            3 => Rank::RankJ,
            4 => Rank::RankQ,
            5 => Rank::RankK,
            6 => Rank::RankX,
            7 => Rank::RankA,
            other => panic!("invalid rank number: {}", other),
        }
    }

    // Return the enum by its discriminant.
    fn from_discriminant(rank: u32) -> Self {
        match rank {
            1 => Rank::Rank7,
            2 => Rank::Rank8,
{'fraction_non_alphanumeric': 0.19328442437923252, 'fraction_numerical': 0.03597629796839729, 'mean_word_length': 1.6710625470987188, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6383839', 'n_tokens_mistral': 2700, 'n_tokens_neox': 2496, 'n_words': 644}