From: MA Beaudet Date: Tue, 9 Nov 2021 10:04:19 +0000 (+0100) Subject: feat!: apply C-CONV C-GETTER and C-CTOR to card and evaluator X-Git-Url: https://git.beaudet.xyz/?a=commitdiff_plain;h=53bcedda00fd7a3288c57bb7fe34010297149528;p=poker-eval.git feat!: apply C-CONV C-GETTER and C-CTOR to card and evaluator C-CONV recommends accessing inner values with `into_inner` C-GETTER recommends using `first` instead of `get_first` or `get` C-CTOR recommends using `with_` for builders, I might create a Dealer struct that has access to Deck and Rules or simply remove associated methods in Deck --- diff --git a/src/card.rs b/src/card.rs index b5d6c9c..befe6e5 100644 --- a/src/card.rs +++ b/src/card.rs @@ -5,6 +5,12 @@ use crate::{constants::PRIMES, MyError, Rules}; #[derive(Debug, Default, Clone)] pub struct Cards(pub(crate) Vec); +impl Cards { + pub fn into_inner(self) -> Vec { + self.0.into_iter().map(|c| c.into_inner()).collect() + } +} + impl FromStr for Cards { type Err = MyError; fn from_str(s: &str) -> Result { @@ -29,7 +35,7 @@ impl std::fmt::Display for Cards { impl From for Vec { fn from(c: Cards) -> Self { - c.0.into_iter().map(|c| u32::from(c)).collect() + c.0.into_iter().map(|c| c.into_inner()).collect() } } @@ -41,27 +47,31 @@ impl Deck { Self::default() } - pub fn get_with_rules(&mut self, rules: &Rules) -> Result { + pub fn into_inner(self) -> Cards { + self.0 + } + + pub fn deal_with_rules(&mut self, rules: &Rules) -> Result { let v = match rules { - Rules::Classic => self.get(5)?, + Rules::Classic => self.deal(5)?, - Rules::Holdem => self.get(2)?, + Rules::Holdem => self.deal(2)?, }; Ok(v) } - pub fn get_with_rules_and_player_nb( + pub fn deal_with_rules_and_player_nb( &mut self, rules: &Rules, player_nb: usize, ) -> Result, MyError> { (0..player_nb) .into_iter() - .map(|_| Ok(self.get_with_rules(rules)?)) + .map(|_| Ok(self.deal_with_rules(rules)?)) .collect() } - pub fn get(&mut self, n: usize) -> Result { + pub fn deal(&mut self, n: usize) -> Result { if self.0 .0.len() < n { Err(MyError::IndexError) } else { @@ -108,7 +118,7 @@ impl Default for Deck { pub struct Card(pub(crate) u32); impl Card { - pub fn get(&self) -> u32 { + pub fn into_inner(self) -> u32 { self.0 } @@ -149,12 +159,6 @@ impl Card { } } -impl From for u32 { - fn from(c: Card) -> Self { - c.0 - } -} - impl TryFrom for Card { type Error = MyError; diff --git a/src/evaluator.rs b/src/evaluator.rs index c1a0f8a..b3bd500 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -30,30 +30,30 @@ impl Evaluator { Self::default() } - pub fn with_rules<'a>(&'a mut self, rules: &Rules) -> &'a mut Evaluator { + pub fn rules<'a>(&'a mut self, rules: &Rules) -> &'a mut Evaluator { self.rules = *rules; self } - pub fn with_algorithm<'a>(&'a mut self, algorithm: &Algorithm) -> &'a mut Evaluator { + pub fn algorithm<'a>(&'a mut self, algorithm: &Algorithm) -> &'a mut Evaluator { self.algorithm = *algorithm; self } - pub fn with_hand<'a>(&'a mut self, hand: &Cards) -> &'a mut Evaluator { self.hands.push(Rc::new(hand.clone())); + pub fn hand<'a>(&'a mut self, hand: &Cards) -> &'a mut Evaluator { self } - pub fn with_hands<'a>(&'a mut self, hands: &Vec) -> &'a mut Evaluator { for hand in hands { self.hands.push(Rc::new(hand.clone())) } + pub fn hands<'a>(&'a mut self, hands: &Vec) -> &'a mut Evaluator { self } - pub fn with_table<'a>(&'a mut self, table: &Cards) -> &'a mut Evaluator { self.table = Rc::new(table.clone()); + pub fn table<'a>(&'a mut self, table: &Cards) -> &'a mut Evaluator { self }