Display for Suit and Card
This commit is contained in:
parent
cf4f15f509
commit
ebaa596a2d
1 changed files with 23 additions and 2 deletions
25
src/card.rs
25
src/card.rs
|
@ -1,12 +1,14 @@
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Card {
|
pub struct Card {
|
||||||
pub suit: Suit,
|
pub suit: Suit,
|
||||||
pub value: u8,
|
pub value: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
#[derive(Clone, Copy, Serialize, Deserialize)]
|
||||||
pub enum Suit {
|
pub enum Suit {
|
||||||
Hearts,
|
Hearts,
|
||||||
Diamonds,
|
Diamonds,
|
||||||
|
@ -30,3 +32,22 @@ impl Card {
|
||||||
self.value
|
self.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Display for Suit {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let suit = match self {
|
||||||
|
Suit::Diamonds => String::from("♦"),
|
||||||
|
Suit::Hearts => String::from("♥"),
|
||||||
|
Suit::Spades => String::from("♠"),
|
||||||
|
Suit::Clubs => String::from("♣"),
|
||||||
|
};
|
||||||
|
|
||||||
|
write!(f, "{}", suit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Card {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "[{}{}]", self.value, self.suit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue