Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/core/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,14 @@ impl Suit {

/// Parses a character, returning the corresponding Suit if valid.
///
/// Returns `None` for characters not representing a Suit. The input is case-insensitive.
/// The input is either a case-insensitive letter, or the UTF-8 character representing the suit.
/// Returns `None` for characters not representing a Suit.
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 +300,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 @@ -332,7 +333,7 @@ impl TryFrom<String> for Card {

impl From<Card> for String {
fn from(c: Card) -> Self {
format!("{}{}", c.value.get_char(), c.suit.get_char())
c.to_string()
}
}

Expand Down Expand Up @@ -405,6 +406,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