From 8dfad57d950f2c7d5f49aaaebf2486a9c2b5e4f5 Mon Sep 17 00:00:00 2001 From: Geoffrey Allott Date: Thu, 8 Jun 2023 00:23:53 +0100 Subject: [PATCH] run cargo fmt --- src/card.rs | 7 +++---- src/game/cribbage/mod.rs | 33 +++++++++++++++++++++++++-------- src/game/cribbage/score.rs | 9 +++++++-- src/rng.rs | 16 +++++++++------- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/card.rs b/src/card.rs index 042b99f..e38c928 100644 --- a/src/card.rs +++ b/src/card.rs @@ -134,9 +134,7 @@ pub struct CardSet { impl Debug for CardSet { fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { - fmt.debug_struct("CardSet") - .field("repr", &format_args!("{:#015x} /*{}*/", self.repr, itertools::join(*self, ", "))) - .finish() + fmt.debug_struct("CardSet").field("repr", &format_args!("{:#015x} /*{}*/", self.repr, itertools::join(*self, ", "))).finish() } } @@ -220,7 +218,8 @@ impl Iterator for CardSetIntoIter { impl FromIterator for CardSet { fn from_iter(iter: T) -> Self - where T: IntoIterator + where + T: IntoIterator, { let mut set = CardSet::new(); for card in iter { diff --git a/src/game/cribbage/mod.rs b/src/game/cribbage/mod.rs index 5f1ff8a..1dcd1ce 100644 --- a/src/game/cribbage/mod.rs +++ b/src/game/cribbage/mod.rs @@ -10,7 +10,7 @@ use super::{Action, ActionError, DealerAction, Game, StartCondition, UserAction, mod score; -use self::score::{PeggingScore, score_4_card_cribbage_hand, score_pegging, value}; +use self::score::{score_4_card_cribbage_hand, score_pegging, value, PeggingScore}; #[derive(Copy, Clone, Debug, PartialEq, Eq)] enum State { @@ -113,8 +113,9 @@ impl Cribbage { } fn next_player_still_in(&self) -> Option { - self.active.and_then(|player| self.seats.player_after_where(player, |player| self.players_still_in.contains(&player)) - .or_else(|| self.players_still_in.get(&player).copied())) + self.active.and_then(|player| { + self.seats.player_after_where(player, |player| self.players_still_in.contains(&player)).or_else(|| self.players_still_in.get(&player).copied()) + }) } fn last_pegging_score(&self) -> Option<(Username, PeggingScore)> { @@ -341,7 +342,7 @@ impl Game for Cribbage { self.state = State::ScoringPegging; self.used_pegging_cards.append(&mut self.pegging_cards); self.players_still_in = self.hands.iter().filter(|(_, cards)| !cards.is_empty()).map(|(&username, _)| username).collect(); - }, + } active => self.active = active, } Ok(()) @@ -453,7 +454,11 @@ impl Game for Cribbage { } State::ScoringTurnUp => { if let Some(username) = self.dealer { - DealerAction::TakeAction(ValidatedUserAction(UserAction { timestamp, username, action: Action::Score { points: 2, reason: "Two for his heels".to_string() } })) + DealerAction::TakeAction(ValidatedUserAction(UserAction { + timestamp, + username, + action: Action::Score { points: 2, reason: "Two for his heels".to_string() }, + })) } else { error!("Expected to score the turn-up but there was no dealer"); DealerAction::Leave @@ -464,7 +469,11 @@ impl Game for Cribbage { if let Some(username) = self.winner() { DealerAction::TakeAction(ValidatedUserAction(UserAction { timestamp, username, action: Action::WinGame })) } else if let Some((username, score)) = self.last_pegging_score() { - DealerAction::TakeAction(ValidatedUserAction(UserAction { timestamp, username, action: Action::Score { points: score.points() as u64, reason: format!("{}", score) } })) + DealerAction::TakeAction(ValidatedUserAction(UserAction { + timestamp, + username, + action: Action::Score { points: score.points() as u64, reason: format!("{}", score) }, + })) } else { error!("Expected a pegging score"); DealerAction::Leave @@ -478,7 +487,11 @@ impl Game for Cribbage { DealerAction::TakeAction(ValidatedUserAction(UserAction { timestamp, username, action: Action::RevealCard { card } })) } else if let (Some(hand), Some(turn_up)) = (self.four_card_hand(username), self.turn_up) { let score = score_4_card_cribbage_hand(hand, turn_up, false); - DealerAction::TakeAction(ValidatedUserAction(UserAction { timestamp, username, action: Action::Score { points: score.points() as u64, reason: format!("{}", score) } })) + DealerAction::TakeAction(ValidatedUserAction(UserAction { + timestamp, + username, + action: Action::Score { points: score.points() as u64, reason: format!("{}", score) }, + })) } else { error!("Found no 4-card hand for scoring user"); DealerAction::Leave @@ -498,7 +511,11 @@ impl Game for Cribbage { DealerAction::TakeAction(ValidatedUserAction(UserAction { timestamp, username, action: Action::RevealCard { card } })) } else if let (Some(hand), Some(turn_up)) = (self.four_card_box(), self.turn_up) { let score = score_4_card_cribbage_hand(hand, turn_up, true); - DealerAction::TakeAction(ValidatedUserAction(UserAction { timestamp, username, action: Action::Score { points: score.points() as u64, reason: format!("{}", score) } })) + DealerAction::TakeAction(ValidatedUserAction(UserAction { + timestamp, + username, + action: Action::Score { points: score.points() as u64, reason: format!("{}", score) }, + })) } else { error!("Found no 4-card box for scoring user"); DealerAction::Leave diff --git a/src/game/cribbage/score.rs b/src/game/cribbage/score.rs index 98657c8..515122e 100644 --- a/src/game/cribbage/score.rs +++ b/src/game/cribbage/score.rs @@ -148,7 +148,9 @@ impl Display for PeggingScore { PeggingScore { fifteen: 0, thirty_one: 2, pair: 0, run: 6, go: false } => f.write_str("Thirty-one for two and a run of six is eight"), PeggingScore { fifteen: 0, thirty_one: 2, pair: 6, run: 0, go: false } => f.write_str("Thirty-one for two and three-of-a-kind for six is eight"), PeggingScore { fifteen: 0, thirty_one: 2, pair: 0, run: 7, go: false } => f.write_str("Thirty-one for two and a run of seven is nine"), - PeggingScore { fifteen: 0, thirty_one: 2, pair: 12, run: 0, go: false } => f.write_str("Thirty-one for two and four-of-a-kind for twelve is fourteen"), + PeggingScore { fifteen: 0, thirty_one: 2, pair: 12, run: 0, go: false } => { + f.write_str("Thirty-one for two and four-of-a-kind for twelve is fourteen") + } PeggingScore { fifteen: 0, thirty_one: 0, pair: 2, run: 0, go: false } => f.write_str("A pair"), PeggingScore { fifteen: 0, thirty_one: 0, pair: 2, run: 0, go: true } => f.write_str("A pair and a go is three"), PeggingScore { fifteen: 0, thirty_one: 0, pair: 0, run: 3, go: false } => f.write_str("A run of three"), @@ -290,7 +292,10 @@ mod test { let score = score_4_card_cribbage_hand([ACE_OF_HEARTS, SEVEN_OF_SPADES, SEVEN_OF_CLUBS, SEVEN_OF_HEARTS], SEVEN_OF_DIAMONDS, false); assert_eq!(24, score.points()); - assert_eq!("Fifteen two, fifteen four, fifteen six, fifteen eight, fifteen ten, fifteen twelve and four-of-a-kind for twelve is 24", format!("{}", score)); + assert_eq!( + "Fifteen two, fifteen four, fifteen six, fifteen eight, fifteen ten, fifteen twelve and four-of-a-kind for twelve is 24", + format!("{}", score) + ); let score = score_4_card_cribbage_hand([FIVE_OF_CLUBS, FIVE_OF_DIAMONDS, FIVE_OF_HEARTS, JACK_OF_SPADES], TEN_OF_SPADES, false); assert_eq!(21, score.points()); diff --git a/src/rng.rs b/src/rng.rs index 5f1a604..e52c306 100644 --- a/src/rng.rs +++ b/src/rng.rs @@ -2,7 +2,7 @@ use std::cmp::{Eq, Ord}; use getrandom::getrandom; use rand::{CryptoRng, Error, Rng, RngCore, SeedableRng}; -use rand_chacha::{ChaCha8Rng, ChaCha20Rng}; +use rand_chacha::{ChaCha20Rng, ChaCha8Rng}; #[derive(Copy, Clone, Debug, Serialize, Deserialize)] #[serde(tag = "rng")] @@ -54,9 +54,9 @@ impl WaveRng { } pub fn choose_from(&mut self, elements: I) -> Option - where - T: Eq + Ord, - I: IntoIterator, + where + T: Eq + Ord, + I: IntoIterator, { let mut vec = Vec::from_iter(elements); let len = vec.len() as u32; @@ -75,10 +75,12 @@ impl WaveRng { // anyway since advance() only moves forward by 4. for _ in 0..4 { i = rng.next_u32(); - if i <= zone { break; } + if i <= zone { + break; + } } i %= len - }, + } WaveRng::ChaCha20(rng) => { // Legacy implementation to match the `choose_stable` // function on a sorted iterator. This calls rng.gen @@ -113,7 +115,7 @@ impl WaveRng { } } } - }, + } } vec.select_nth_unstable(i as usize); Some(vec.swap_remove(i as usize)) -- 2.34.1