Skip to content

Commit

Permalink
Read from UTF-8 suit symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
pashadia committed Dec 19, 2023
1 parent 48c8502 commit 51adfa4
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/core/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ impl Suit {
/// Returns `None` for characters not representing a Suit. The input is case-insensitive.
pub fn from_char(c: char) -> Option<Suit> {
match c.to_ascii_lowercase() {
'h' => Some(Self::Heart),
'c' => Some(Self::Club),
'd' => Some(Self::Diamond),
's' => Some(Self::Spade),
'h' | '♥' => Some(Self::Heart),
'c' | '♣' => Some(Self::Club),
'd' | '♦' => Some(Self::Diamond),
's' | '♠' => Some(Self::Spade),
_ => None,
}
}
Expand Down Expand Up @@ -299,7 +299,7 @@ impl From<i32> for Card {
impl TryFrom<String> for Card {
type Error = String;
fn try_from(s: String) -> Result<Self, Self::Error> {
if s.len() != 2 {
if s.chars().count() != 2 {
return Err(format!(
r#"Card string "{}" is not exactly a length of 2"#,
s
Expand Down Expand Up @@ -405,6 +405,14 @@ mod tests {
}
}

#[test]
fn conversion_from_suit_symbols() {
for index in 1..=52 {
let card = Card::from(index);
assert_eq!(Card::from_str(&card.to_string()).unwrap(), card);
}
}

#[test]
fn conversion_error() {
assert_eq!(
Expand Down

0 comments on commit 51adfa4

Please sign in to comment.