card: implement display methods for rank, suit, card

This commit is contained in:
2026-04-01 01:01:04 +01:00
committed by oreodave
parent 160500d969
commit be7ea8ba24

View File

@@ -1,7 +1,3 @@
use std::cmp::Ordering;
use std::convert::TryFrom;
use std::hash::{Hash, Hasher};
#[derive(PartialEq, Debug, Copy, Clone)] #[derive(PartialEq, Debug, Copy, Clone)]
pub enum Rank { pub enum Rank {
Three = 0, Three = 0,
@@ -61,8 +57,63 @@ pub fn make_decks(number_of_decks: usize) -> Vec<Card> {
} }
mod impls { mod impls {
use std::cmp::Ordering;
use std::convert::TryFrom;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use super::*; 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 { impl TryFrom<i64> for Rank {
type Error = (); type Error = ();