1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use std::{
    convert::TryFrom,
    fmt::{self, Write},
};

use variter::derive_var_iter;

derive_var_iter! {
    @impl_attr {
        #[doc(hidden)]
    }
    #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
    /// An enumeration type for representing the thirteen card ranks, from two to ace.
    ///
    /// `Rank` has implemented [`Ord`] such that:
    /// - 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < T < J < Q < K < A
    pub enum Rank {
        /// The rank of two, also called a deuce.
        Two,
        /// The rank of three, also called a trey.
        Three,
        /// The rank of four.
        Four,
        /// The rank of five.
        Five,
        /// The rank of six.
        Six,
        /// The rank of seven.
        Seven,
        /// The rank of eight.
        Eight,
        /// The rank of nine.
        Nine,
        /// The rank of ten.
        Ten,
        /// The rank of jack.
        Jack,
        /// The rank of queen.
        Queen,
        /// The rank of king.
        King,
        /// The rank of ace.
        Ace,
    }
}

impl Rank {
    /// Get a textual representation of the rank. The character returned is the
    /// same character expected when parsing a rank from strings.
    ///
    /// # Example
    ///
    /// ```
    /// use poker::Rank;
    /// let ten = Rank::Ten;
    /// assert_eq!(ten.as_char(), 'T');
    /// ```
    pub const fn as_char(self) -> char {
        use Rank::*;
        match self {
            Ace => 'A',
            Two => '2',
            Three => '3',
            Four => '4',
            Five => '5',
            Six => '6',
            Seven => '7',
            Eight => '8',
            Nine => '9',
            Ten => 'T',
            Jack => 'J',
            Queen => 'Q',
            King => 'K',
        }
    }

    /// Get the integer representation of the rank, a number from 0 to 12
    /// inclusive.
    pub(super) const fn as_i32(self) -> i32 {
        use Rank::*;
        match self {
            Two => 0,
            Three => 1,
            Four => 2,
            Five => 3,
            Six => 4,
            Seven => 5,
            Eight => 6,
            Nine => 7,
            Ten => 8,
            Jack => 9,
            Queen => 10,
            King => 11,
            Ace => 12,
        }
    }

    /// Create a rank from its integer representation. As this function is
    /// private, be sure to only pass in 0 through 12 inclusive.
    pub(super) const fn from_i32(val: i32) -> Self {
        use Rank::*;
        match val {
            0 => Two,
            1 => Three,
            2 => Four,
            3 => Five,
            4 => Six,
            5 => Seven,
            6 => Eight,
            7 => Nine,
            8 => Ten,
            9 => Jack,
            10 => Queen,
            11 => King,
            // Really, this should be 12 => Ace, _ => unreachable!() but you can't panic
            // in const functions in stable Rust yet.
            _ => Ace,
        }
    }

    /// Get the string name of this rank. Used for printing hands such as "ace
    /// high".
    pub(crate) const fn as_str_name(self) -> &'static str {
        use Rank::*;
        match self {
            Two => "two",
            Three => "three",
            Four => "four",
            Five => "five",
            Six => "six",
            Seven => "seven",
            Eight => "eight",
            Nine => "nine",
            Ten => "ten",
            Jack => "jack",
            Queen => "queen",
            King => "king",
            Ace => "ace",
        }
    }

    /// Get the plural string name of this rank. Used for printing hands such as
    /// "pair of aces".
    pub(crate) const fn as_str_name_plural(self) -> &'static str {
        use Rank::*;
        match self {
            Two => "twos",
            Three => "threes",
            Four => "fours",
            Five => "fives",
            Six => "sixes",
            Seven => "sevens",
            Eight => "eights",
            Nine => "nines",
            Ten => "tens",
            Jack => "jacks",
            Queen => "queens",
            King => "kings",
            Ace => "aces",
        }
    }
}

impl TryFrom<char> for Rank {
    type Error = char;

    fn try_from(value: char) -> Result<Self, Self::Error> {
        use Rank::*;
        match value {
            'A' => Ok(Ace),
            '2' => Ok(Two),
            '3' => Ok(Three),
            '4' => Ok(Four),
            '5' => Ok(Five),
            '6' => Ok(Six),
            '7' => Ok(Seven),
            '8' => Ok(Eight),
            '9' => Ok(Nine),
            'T' => Ok(Ten),
            'J' => Ok(Jack),
            'Q' => Ok(Queen),
            'K' => Ok(King),
            x => Err(x),
        }
    }
}

impl fmt::Display for Rank {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_char(self.as_char()) }
}