diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-09-11 11:12:16 -0400 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-09-11 11:12:16 -0400 |
commit | 3fb6f32a46b0cdfc59643bee255648e1ef401116 (patch) | |
tree | 8d96d1dadae5e82b92e6d1ee7f4203724eb08609 /webapp/src/bridge_engine.rs | |
parent | 55a24a3b7d1b3fcc07f0bb8e53b00abf23e651b3 (diff) |
Refactor passed contract into Option<Contract>
Diffstat (limited to 'webapp/src/bridge_engine.rs')
-rw-r--r-- | webapp/src/bridge_engine.rs | 51 |
1 files changed, 20 insertions, 31 deletions
diff --git a/webapp/src/bridge_engine.rs b/webapp/src/bridge_engine.rs index 2161a53..ae15c7d 100644 --- a/webapp/src/bridge_engine.rs +++ b/webapp/src/bridge_engine.rs @@ -255,16 +255,13 @@ impl fmt::Display for ContractModifier { #[derive(Debug, PartialEq, Eq, Copy, Clone)] pub struct Contract { - highest_bid: Option<Raise>, + highest_bid: Raise, modifier: ContractModifier, } impl fmt::Display for Contract { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> { - match self.highest_bid { - None => write!(f, "Passed"), - Some(bid) => write!(f, "{}{}", bid, self.modifier), - } + write!(f, "{}{}", self.highest_bid, self.modifier) } } @@ -301,9 +298,18 @@ impl Bidding { passes == 3 } + fn contract(&self) -> Option<Contract> { + match self.highest_bid() { + None => None, + Some(highest_bid) => Some(Contract { + highest_bid, + modifier: ContractModifier::None, + }), + } + } + pub fn bid(mut self, bid: Bid) -> Result<BiddingResult, anyhow::Error> { // TODO: Need logic for double and redouble here. - let highest_bid = self.highest_bid(); if bid.as_raise().is_some() && bid.as_raise() <= self.highest_bid() { bail!( "bid too low: {:?} <= {:?}", @@ -313,13 +319,7 @@ impl Bidding { } self.bids.push(bid); if self.passed_out() { - Ok(BiddingResult::Contract( - Contract { - highest_bid, - modifier: ContractModifier::None, - }, - self, - )) + Ok(BiddingResult::Contract(self.contract(), self)) } else { Ok(BiddingResult::InProgress(self)) } @@ -329,7 +329,7 @@ impl Bidding { #[derive(Debug, Clone)] pub enum BiddingResult { InProgress(Bidding), - Contract(Contract, Bidding), + Contract(Option<Contract>, Bidding), } impl BiddingResult { @@ -357,7 +357,7 @@ mod tests { } } - fn as_contract(r: BiddingResult) -> Contract { + fn as_contract(r: BiddingResult) -> Option<Contract> { match r { BiddingResult::Contract(contract, _) => contract, _ => panic!("expected BiddingResult::Contract(): {:?}", r), @@ -374,10 +374,10 @@ mod tests { let bidding = as_bidding(bidding.bid(Bid::Pass).unwrap()); let contract = as_contract(bidding.bid(Bid::Pass).unwrap()); assert_eq!( - Contract { - highest_bid: Some("1♦".parse().unwrap()), + Some(Contract { + highest_bid: "1♦".parse().unwrap(), modifier: ContractModifier::None - }, + }), contract ); } @@ -413,18 +413,7 @@ mod tests { format!( "{}", Contract { - highest_bid: None, - modifier: ContractModifier::None - } - ), - "Passed" - ); - - assert_eq!( - format!( - "{}", - Contract { - highest_bid: Some("1♥".parse().unwrap()), + highest_bid: "1♥".parse().unwrap(), modifier: ContractModifier::None } ), @@ -435,7 +424,7 @@ mod tests { format!( "{}", Contract { - highest_bid: Some("1♥".parse().unwrap()), + highest_bid: "1♥".parse().unwrap(), modifier: ContractModifier::Doubled } ), |