From d3fbefad9cf25786fb5f28f96eeceb65d0a8b35b Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Sun, 1 Jan 2023 20:34:09 -0500 Subject: Split bridge_engine into a few separate modules --- protocol/src/actions.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 protocol/src/actions.rs (limited to 'protocol/src/actions.rs') 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 { + 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()?)), + } + } +} + -- cgit v1.2.3