card: implement display methods for rank, suit, card
This commit is contained in:
59
src/card.rs
59
src/card.rs
@@ -1,7 +1,3 @@
|
||||
use std::cmp::Ordering;
|
||||
use std::convert::TryFrom;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
#[derive(PartialEq, Debug, Copy, Clone)]
|
||||
pub enum Rank {
|
||||
Three = 0,
|
||||
@@ -61,8 +57,63 @@ pub fn make_decks(number_of_decks: usize) -> Vec<Card> {
|
||||
}
|
||||
|
||||
mod impls {
|
||||
use std::cmp::Ordering;
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
use super::*;
|
||||
|
||||
impl Display for Rank {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
match self {
|
||||
Rank::Jack => "J",
|
||||
Rank::Queen => "Q",
|
||||
Rank::King => "K",
|
||||
Rank::Ace => "A",
|
||||
Rank::Two => "2",
|
||||
Rank::Three => "3",
|
||||
Rank::Four => "4",
|
||||
Rank::Five => "5",
|
||||
Rank::Six => "6",
|
||||
Rank::Seven => "7",
|
||||
Rank::Eight => "8",
|
||||
Rank::Nine => "9",
|
||||
Rank::Ten => "10",
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Suit {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
match self {
|
||||
Suit::Diamond => "♦",
|
||||
Suit::Club => "♣",
|
||||
Suit::Heart => "♥",
|
||||
Suit::Spade => "♠",
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Card {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Card::Joker(_) => write!(f, "Joker"),
|
||||
Card::PlayingCard { rank, suit, .. } => {
|
||||
write!(f, "{}[{}]", rank, suit)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<i64> for Rank {
|
||||
type Error = ();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user