diff options
Diffstat (limited to 'webapp/src/card.rs')
| -rw-r--r-- | webapp/src/card.rs | 26 |
1 files changed, 10 insertions, 16 deletions
diff --git a/webapp/src/card.rs b/webapp/src/card.rs index 3b2fc24..248b9b5 100644 --- a/webapp/src/card.rs +++ b/webapp/src/card.rs @@ -3,6 +3,7 @@ use rand::Rng; use std::fmt; use strum::IntoEnumIterator; use strum_macros::EnumIter; +use anyhow::anyhow; #[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, EnumIter)] pub enum Suit { @@ -40,17 +41,10 @@ impl fmt::Display for Suit { } } -#[derive(Debug, PartialEq, Eq)] -pub enum ParseError { - InvalidSuit(String), - InvalidRank(String), - MissingParts(String), -} - impl std::str::FromStr for Suit { - type Err = ParseError; + type Err = anyhow::Error; - fn from_str(s: &str) -> std::result::Result<Self, ParseError> { + fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { match s.trim() { "♣" => Ok(Suit::Club), "C" => Ok(Suit::Club), @@ -62,7 +56,7 @@ impl std::str::FromStr for Suit { "H" => Ok(Suit::Heart), "♠" => Ok(Suit::Spade), "S" => Ok(Suit::Spade), - _ => Err(ParseError::InvalidSuit(s.to_string())), + _ => Err(anyhow!("invalid suit: {}", s)), } } } @@ -100,9 +94,9 @@ impl fmt::Debug for Rank { } impl std::str::FromStr for Rank { - type Err = ParseError; + type Err = anyhow::Error; - fn from_str(s: &str) -> std::result::Result<Self, ParseError> { + fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { match s.trim().to_ascii_uppercase().as_str() { "A" => Ok(Rank::Ace), "K" => Ok(Rank::King), @@ -118,7 +112,7 @@ impl std::str::FromStr for Rank { "4" => Ok(Rank::Four), "3" => Ok(Rank::Three), "2" => Ok(Rank::Two), - _ => Err(ParseError::InvalidRank(s.to_string())), + _ => Err(anyhow!("invalid rank: {}", s)), } } } @@ -140,14 +134,14 @@ impl fmt::Debug for Card { } impl std::str::FromStr for Card { - type Err = ParseError; + type Err = anyhow::Error; - fn from_str(s: &str) -> std::result::Result<Self, ParseError> { + fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { let stripped = s.replace(" ", ""); let mut chars = stripped.chars(); let suit = chars .next() - .ok_or(ParseError::MissingParts(s.to_string()))? + .ok_or(anyhow!("missing parts: {}", s))? .to_string() .parse()?; let rank = chars.collect::<String>().parse()?; |
