use core::fmt; use std::str::FromStr; use serde::{Deserialize, Serialize}; use crate::contract::LevelAndSuit; #[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] pub enum Bid { Pass, Double, Redouble, Raise(LevelAndSuit), } impl Bid { pub fn as_raise(&self) -> Option { match self { Bid::Raise(raise) => Some(*raise), _ => None, } } } impl fmt::Display for Bid { fn fmt( &self, f: &mut std::fmt::Formatter<'_>, ) -> std::result::Result<(), std::fmt::Error> { match self { Bid::Pass => write!(f, "Pass"), Bid::Double => write!(f, "Double"), Bid::Redouble => write!(f, "Redouble"), Bid::Raise(x) => write!(f, "{}", x), } } } impl fmt::Debug for Bid { fn fmt( &self, f: &mut std::fmt::Formatter<'_>, ) -> std::result::Result<(), std::fmt::Error> { match self { Bid::Pass => write!(f, "Pass"), Bid::Double => write!(f, "Double"), Bid::Redouble => write!(f, "Redouble"), Bid::Raise(x) => write!(f, "Raise({})", x), } } } impl FromStr for Bid { type Err = anyhow::Error; fn from_str( s: &str, ) -> std::result::Result::Err> { match s.trim().to_ascii_lowercase().as_str() { "pass" => Ok(Bid::Pass), "double" => Ok(Bid::Double), "redouble" => Ok(Bid::Redouble), x => Ok(Bid::Raise(x.parse()?)), } } }