diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2023-01-01 20:34:09 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2023-01-01 20:34:09 -0500 |
commit | d3fbefad9cf25786fb5f28f96eeceb65d0a8b35b (patch) | |
tree | 156a23b5c04b93d746ecf592971aefbcc127cfd2 /protocol/src/actions.rs | |
parent | bb2ed3a2926384df063e476d10613fa310cd7ffa (diff) |
Split bridge_engine into a few separate modules
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()?)), + } + } +} + |