Created
August 11, 2019 06:59
-
-
Save reillysiemens/17f3733dbde7d49d7af4e42c3353c8e2 to your computer and use it in GitHub Desktop.
Pretty Rust Cards
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fmt; | |
use std::str; | |
#[derive(Debug)] | |
pub enum Rank { | |
Ace, | |
Two, | |
Three, | |
Four, | |
Five, | |
Six, | |
Seven, | |
Eight, | |
Nine, | |
Ten, | |
Jack, | |
Queen, | |
King, | |
} | |
#[derive(Debug)] | |
pub enum Suit { | |
Clubs, // ♣ | |
Diamonds, // ♦ | |
Hearts, // ♥ | |
Spades, // ♠ | |
} | |
#[derive(Debug)] | |
pub struct Card { | |
pub suit: Suit, | |
pub rank: Rank, | |
} | |
impl fmt::Display for Card { | |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
let mut card = vec![0xf0, 0x9f]; | |
match self.suit { | |
Suit::Clubs => card.extend([0x83, 0x90].iter().cloned()), | |
Suit::Diamonds => card.extend([0x83, 0x80].iter().cloned()), | |
Suit::Hearts => card.extend([0x82, 0xb0].iter().cloned()), | |
Suit::Spades => card.extend([0x82, 0xa0].iter().cloned()), | |
} | |
card[3] += match self.rank { | |
Rank::Ace => 0x01, | |
Rank::Two => 0x02, | |
Rank::Three => 0x03, | |
Rank::Four => 0x04, | |
Rank::Five => 0x05, | |
Rank::Six => 0x06, | |
Rank::Seven => 0x07, | |
Rank::Eight => 0x08, | |
Rank::Nine => 0x09, | |
Rank::Ten => 0x0a, | |
Rank::Jack => 0x0b, | |
Rank::Queen => 0x0d, | |
Rank::King => 0x0e, | |
}; | |
write!(f, "{}", str::from_utf8(&card).unwrap()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment