}
}
+#[derive(Debug, PartialEq, Eq)]
+pub enum EvaluateCardError {
+ NoHand,
+ UnmatchedArgs,
+ HandSize,
+}
+
+impl std::error::Error for EvaluateCardError {}
+
+impl std::fmt::Display for EvaluateCardError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ EvaluateCardError::NoHand => write!(f, "No hand provided for evaluation"),
+ EvaluateCardError::UnmatchedArgs => write!(f, "Wrong set of arguments provided"),
+ EvaluateCardError::HandSize => write!(f, "Provided hand has wrong size"),
+ }
+ // write!(f, "Invalid number of cards")
+ }
+}
+
#[test]
fn test_send() {
fn assert_send<T: Send>() {}
use crate::{
card::Cards,
constants::{FLUSHES, HASH_ADJUST, HASH_VALUES, PERM7, UNIQUE5},
- Algorithm, MyError, Rules,
+ errors::EvaluateCardError,
+ Algorithm, Rules,
};
#[cfg(feature = "serde")]
self
}
- pub fn eval(&self) -> Result<HashMap<usize, u16>, MyError> {
- fn parse_args(evaluator: &Evaluator) -> Result<(), MyError> {
+ pub fn eval(&self) -> Result<HashMap<usize, u16>, EvaluateCardError> {
+ fn parse_args(evaluator: &Evaluator) -> Result<(), EvaluateCardError> {
let hands = &evaluator.hands;
let table = &evaluator.table;
if hands.is_empty() {
- return Err(MyError::NoHands);
+ return Err(EvaluateCardError::NoHand);
}
for hand in hands {
if hand.len() == 5 && matches!(evaluator.rules, Rules::Holdem) {
- println!("Warning: card number in hands does not match the given Poker rules");
- return Err(MyError::UnmatchedCardsTable);
+ eprintln!("Warning: card number in hands does not match the given Poker rules");
+ return Err(EvaluateCardError::UnmatchedArgs);
}
if hand.len() == 2 && matches!(evaluator.rules, Rules::Classic) {
- println!("Warning: card number in hands does not match the given Poker rules");
- return Err(MyError::UnmatchedCardsTable);
+ eprintln!("Warning: card number in hands does not match the given Poker rules");
+ return Err(EvaluateCardError::UnmatchedArgs);
}
}
if matches!(evaluator.rules, Rules::Holdem) && table.is_empty() {
- return Err(MyError::NoTable);
+ return Err(EvaluateCardError::UnmatchedArgs);
}
Ok(())
}
Ok(result)
}
- pub fn eval_5hand(cards: &[u32]) -> Result<u16, MyError> {
+ pub fn eval_5hand(cards: &[u32]) -> Result<u16, EvaluateCardError> {
if let [c1, c2, c3, c4, c5] = cards[..5] {
let q = (c1 | c2 | c3 | c4 | c5) >> 16;
if c1 & c2 & c3 & c4 & c5 & 0xF000 != 0 {
Ok(HASH_VALUES[Evaluator::find_fast(q) as usize])
}
} else {
- Err(MyError::ParseArrayError)
+ Err(EvaluateCardError::HandSize)
}
}
/// Non-optimized method of determining the best five-card hand possible of seven cards.
- pub fn eval_7hand(cards: &[u32]) -> Result<u16, MyError> {
+ pub fn eval_7hand(cards: &[u32]) -> Result<u16, EvaluateCardError> {
let mut subhand: [u32; 5] = [0; 5];
let mut best = 9999;