1use std::cmp::Ordering;
2use std::slice::Iter;
3
4use self::Suit::*;
5
6#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Debug)]
8pub enum Suit {
9 Spades,
10 Hearts,
11 Diamonds,
12 Clubs,
13}
14
15impl Ord for Suit {
16 fn cmp(&self, other: &Suit) -> Ordering {
17 let o1 = self.ordinal();
18 let o2 = other.ordinal();
19 if o1 < o2 {
20 return Ordering::Less;
21 } else if o1 > o2 {
22 return Ordering::Greater;
23 }
24 Ordering::Equal
25 }
26}
27
28impl Suit {
29 pub fn iterator() -> Iter<'static, Suit> {
31 Suit::suits().into_iter()
32 }
33
34 pub fn ordinal(&self) -> usize {
36 match *self {
37 Spades => 0,
38 Hearts => 1,
39 Diamonds => 2,
40 Clubs => 3,
41 }
42 }
43
44 pub fn from_char(ch: char) -> Result<Suit, &'static str> {
46 match ch {
47 'S' => Ok(Spades),
48 'H' => Ok(Hearts),
49 'D' => Ok(Diamonds),
50 'C' => Ok(Clubs),
51 _ => Err("Invalid suit")
52 }
53 }
54
55 pub fn to_char(&self) -> char {
57 match self {
58 Spades => 'S',
59 Hearts => 'H',
60 Diamonds => 'D',
61 Clubs => 'C',
62 }
63 }
64
65 pub fn to_str(&self) -> &'static str {
67 match *self {
68 Spades => "Spades",
69 Hearts => "Hearts",
70 Diamonds => "Diamonds",
71 Clubs => "Clubs",
72 }
73 }
74
75 pub fn suits() -> &'static [Suit] {
77 static SUITS: [Suit; 4] = [Spades, Hearts, Diamonds, Clubs];
78 &SUITS[..]
79 }
80}