feat!: apply C-CONV C-GETTER and C-CTOR to card and evaluator
authorMA Beaudet <ma@beaudet.xyz>
Tue, 9 Nov 2021 10:04:19 +0000 (11:04 +0100)
committerMA Beaudet <ma@beaudet.xyz>
Tue, 9 Nov 2021 10:04:19 +0000 (11:04 +0100)
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

src/card.rs
src/evaluator.rs

index b5d6c9c..befe6e5 100644 (file)
@@ -5,6 +5,12 @@ use crate::{constants::PRIMES, MyError, Rules};
 #[derive(Debug, Default, Clone)]
 pub struct Cards(pub(crate) Vec<Card>);
 
+impl Cards {
+    pub fn into_inner(self) -> Vec<u32> {
+        self.0.into_iter().map(|c| c.into_inner()).collect()
+    }
+}
+
 impl FromStr for Cards {
     type Err = MyError;
     fn from_str(s: &str) -> Result<Self, Self::Err> {
@@ -29,7 +35,7 @@ impl std::fmt::Display for Cards {
 
 impl From<Cards> for Vec<u32> {
     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<Cards, MyError> {
+    pub fn into_inner(self) -> Cards {
+        self.0
+    }
+
+    pub fn deal_with_rules(&mut self, rules: &Rules) -> Result<Cards, MyError> {
         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<Vec<Cards>, 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<Cards, MyError> {
+    pub fn deal(&mut self, n: usize) -> Result<Cards, MyError> {
         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<Card> for u32 {
-    fn from(c: Card) -> Self {
-        c.0
-    }
-}
-
 impl TryFrom<u32> for Card {
     type Error = MyError;
 
index c1a0f8a..b3bd500 100644 (file)
@@ -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<Cards>) -> &'a mut Evaluator {
         for hand in hands {
             self.hands.push(Rc::new(hand.clone()))
         }
+    pub fn hands<'a>(&'a mut self, hands: &Vec<Cards>) -> &'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
     }