From 51adfa4cdfe61dcc777aa65c75c2897dbf3c8cbe Mon Sep 17 00:00:00 2001 From: pashadia Date: Tue, 19 Dec 2023 17:04:59 +0100 Subject: [PATCH] Read from UTF-8 suit symbols --- src/core/card.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/core/card.rs b/src/core/card.rs index 350e799..1439c71 100644 --- a/src/core/card.rs +++ b/src/core/card.rs @@ -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 { 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, } } @@ -299,7 +299,7 @@ impl From for Card { impl TryFrom for Card { type Error = String; fn try_from(s: String) -> Result { - if s.len() != 2 { + if s.chars().count() != 2 { return Err(format!( r#"Card string "{}" is not exactly a length of 2"#, s @@ -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!(