diff options
Diffstat (limited to 'protocol/src/actions.rs')
-rw-r--r-- | protocol/src/actions.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/protocol/src/actions.rs b/protocol/src/actions.rs new file mode 100644 index 0000000..defe1f9 --- /dev/null +++ b/protocol/src/actions.rs @@ -0,0 +1,66 @@ +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<LevelAndSuit> { + 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<Self, <Self as std::str::FromStr>::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()?)), + } + } +} + |