1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
use crate::error::ParseError; use std::str; use std::fmt; #[derive(Clone, Debug, Eq, PartialEq, Hash)] pub enum Suit { Hearts, Diamonds, Clubs, Spades, } impl Suit { pub fn is_red(&self) -> bool { match *self { Self::Hearts => true, Self::Diamonds => true, _ => false, } } pub fn is_black(&self) -> bool { !self.is_red() } } impl fmt::Display for Suit { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let s = match self { Self::Hearts => "H", Self::Diamonds => "D", Self::Clubs => "C", Self::Spades => "S", }; write!(f, "{}", s) } } impl str::FromStr for Suit { type Err = ParseError; fn from_str(s: &str) -> Result<Self, Self::Err> { match s { "H" => Ok(Self::Hearts), "D" => Ok(Self::Diamonds), "C" => Ok(Self::Clubs), "S" => Ok(Self::Spades), _ => Err(ParseError::new(s, "Invalid suit initial")), } } } #[cfg(test)] mod tests { use super::*; use std::str::FromStr; #[test] fn hearts_colour() { assert!(Suit::Hearts.is_red()); assert!(!Suit::Hearts.is_black()); } #[test] fn from_str() { assert_eq!(Suit::from_str("C").unwrap(), Suit::Clubs); Suit::from_str("A").expect_err("Invalid suit"); } }