diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-12-28 07:03:09 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-12-28 07:03:09 -0500 |
commit | f9e7e857a1718faa6163d565f890677ee8c39d29 (patch) | |
tree | f4aa7d142e1fc742cdd8f622f26ccff98a4f309d | |
parent | 75bb8ff15d724aafde443c36730ca7cb48c70104 (diff) |
Fix some clippy warnings
-rw-r--r-- | protocol/src/bridge_engine.rs | 52 |
1 files changed, 23 insertions, 29 deletions
diff --git a/protocol/src/bridge_engine.rs b/protocol/src/bridge_engine.rs index 0bc3434..7d69b82 100644 --- a/protocol/src/bridge_engine.rs +++ b/protocol/src/bridge_engine.rs @@ -93,7 +93,7 @@ pub struct Trick { impl Trick { pub fn suit(&self) -> Option<Suit> { - self.cards_played.iter().next().map(|&Card(suit, _)| suit) + self.cards_played.first().map(|&Card(suit, _)| suit) } // TODO: This should be moved somewhere we can guarantee that cards are non-empty. @@ -135,11 +135,7 @@ impl TurnInPlay { } pub fn suit(&self) -> Option<Suit> { - self.trick - .cards_played - .iter() - .next() - .map(|&Card(suit, _)| suit) + self.trick.cards_played.first().map(|&Card(suit, _)| suit) } pub fn leader(&self) -> Player { @@ -210,7 +206,7 @@ impl DealInPlay { } pub fn play( - mut self: Self, + mut self, card: Card, ) -> Result<MoveResult<DealInPlay, Vec<Trick>>, anyhow::Error> { let player = self.current_player(); @@ -218,7 +214,7 @@ impl DealInPlay { if let Some(suit) = self.in_progress.suit() { if card.suit() != suit - && player_cards.iter().find(|c| c.suit() == suit).is_some() + && player_cards.iter().any(|c| c.suit() == suit) { return Err(anyhow!("Must follow {suit} suit")); } @@ -228,11 +224,10 @@ impl DealInPlay { "Next player is {:?}, playing card {} from {:?}", player, card, player_cards ); - let i = player_cards.iter().position(|&c| c == card).ok_or(anyhow!( - "{:?} does not have {}", - player, - card - ))?; + let i = player_cards + .iter() + .position(|&c| c == card) + .ok_or_else(|| anyhow!("{:?} does not have {}", player, card))?; player_cards.remove(i); Ok(match self.in_progress.play(card) { @@ -405,15 +400,15 @@ impl PartialOrd<Self> for Raise { return self.level.partial_cmp(&o.level); } if self.suit != o.suit { - if self.suit == None { + if self.suit.is_none() { return Some(Ordering::Greater); } - if o.suit == None { + if o.suit.is_none() { return Some(Ordering::Less); } return self.suit.partial_cmp(&o.suit); } - return Some(Ordering::Equal); + Some(Ordering::Equal) } } @@ -455,7 +450,9 @@ impl FromStr for Raise { lazy_static::lazy_static! { static ref RE: Regex = Regex::new(r#"\s*(.[0-9]*)\s*(.*)"#).unwrap(); }; - let caps = RE.captures(s).ok_or(anyhow!("invalid raise: {}", s))?; + let caps = RE + .captures(s) + .ok_or_else(|| anyhow!("invalid raise: {}", s))?; info!("caps: {:?}", caps); let level = caps[1].parse()?; let suit = match caps[2].to_ascii_uppercase().as_str() { @@ -564,14 +561,11 @@ impl Bidding { } fn contract(&self) -> Option<Contract> { - match self.highest_bid() { - None => None, - Some(highest_bid) => Some(Contract { - declarer: self.declarer(), - highest_bid, - modifier: ContractModifier::None, - }), - } + self.highest_bid().map(|highest_bid| Contract { + declarer: self.declarer(), + highest_bid, + modifier: ContractModifier::None, + }) } pub fn bid(mut self, bid: Bid) -> Result<BiddingResult, anyhow::Error> { @@ -704,9 +698,9 @@ pub enum GameState { Play(PlayState), } -impl Into<GameState> for PlayState { - fn into(self) -> GameState { - GameState::Play(self) +impl From<PlayState> for GameState { + fn from(val: PlayState) -> Self { + GameState::Play(val) } } @@ -722,7 +716,7 @@ impl GameState { pub fn deal(&self) -> &Deal { match self { Self::Bidding(BiddingState { deal, .. }) => deal, - Self::Play(PlayState { playing_deal, .. }) => &playing_deal.deal(), + Self::Play(PlayState { playing_deal, .. }) => playing_deal.deal(), } } |