simple_cards/
cards.rs

1use std::fmt;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
4pub struct Card {
5    pub suit: Suit,
6    pub value: Value,
7}
8
9impl fmt::Display for Card {
10    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11        write!(f, "{:?} of {:?}", self.value, self.suit)
12    }
13}
14
15impl Card {
16    /// Returns whether or not a card is a face card.
17    pub fn is_face(self) -> bool {
18        self.value == Value::Ace
19            || self.value == Value::Jack
20            || self.value == Value::Queen
21            || self.value == Value::King
22    }
23
24    /// Returns the suit of this card.
25    pub fn suit(self) -> Suit {
26        self.suit
27    }
28
29    /// Returns the value of this card.
30    pub fn value(self) -> Value {
31        self.value
32    }
33}
34
35#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
36pub enum Suit {
37    Hearts,
38    Clubs,
39    Spades,
40    Diamonds,
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
44pub enum Value {
45    Ace,
46    Two,
47    Three,
48    Four,
49    Five,
50    Six,
51    Seven,
52    Eight,
53    Nine,
54    Ten,
55    Jack,
56    Queen,
57    King,
58}