u16::try_from(a).unwrap() ^ HASH_ADJUST[b as usize]
}
-/// Wrapper over both five-card and seven-card hand evaluator from string.
+// #[wasm_bindgen]
+// pub fn eval_from_str(s: &str) -> u16 {
+// let cards = s.split(' ').map(|c| card_from_str(c)).collect::<Vec<_>>();
+// match cards.len() {
+// 7 => eval_7hand(&cards.try_into().unwrap()),
+// 5 => eval_5hand(&cards.try_into().unwrap()),
+// _ => 9999,
+// }
+// }
+
+#[wasm_bindgen]
+pub fn parse_cards(s: &str) -> Vec<u32> {
+ s.trim()
+ .split_whitespace()
+ .map(|c| parse_card(c).expect("Could not parse card from string"))
+ .collect::<Vec<_>>()
+}
+
+/// Wrapper over both five-card and seven-card hand evaluator from a vector of cards.
///
/// The evaluator orders hands from 1 to 7462.
///
/// # Example
///
/// ```
-/// use poker_eval::{eval_from_str};
+/// use poker_eval::{eval_hand, parse_cards};
///
-/// assert_eq!(eval_from_str("4s 4d 4h 4c 5d"), 140);
-/// assert_eq!(eval_from_str("Ts 9s 8s 7s 6s 7h 6d"), 5);
+/// assert_eq!(eval_hand(parse_cards("4s 4d 4h 4c 5d")), 140);
+/// assert_eq!(eval_hand(parse_cards("Ts 9s 8s 7s 6s 7h 6d")), 5);
/// ```
#[wasm_bindgen]
-pub fn eval_from_str(s: &str) -> u16 {
- let cards = s.split(' ').map(|c| card_from_str(c)).collect::<Vec<_>>();
+pub fn eval_hand(cards: Vec<u32>) -> u16 {
match cards.len() {
7 => eval_7hand(&cards.try_into().unwrap()),
5 => eval_5hand(&cards.try_into().unwrap()),
/// # Example
///
/// ```
-/// use poker_eval::{eval_from_str, hand_rank, constants::FLUSH};
+/// use poker_eval::{eval_hand, hand_rank, parse_cards, constants::FLUSH};
///
-/// let eval = eval_from_str("Js 9s 7s 3s 2s");
+/// let eval = eval_hand(parse_cards("Js 9s 7s 3s 2s"));
/// assert_eq!(eval, 1433);
/// assert_eq!(hand_rank(eval), FLUSH);
/// ```
);
}
#[test]
- fn eval_hand() {
- assert_eq!(eval_from_str("Js 9s 7s 3s 2s"), 1433);
- assert_eq!(eval_from_str("6s 6d 6h Qs Qd"), 265);
- assert_eq!(eval_from_str("4s 4d 4h 4c 5d"), 140);
- assert_eq!(eval_from_str("Ts 9s 8s 7s 6s 7h 6d"), 5);
+ fn eval_cards() {
+ assert_eq!(eval_hand(parse_cards("Js 9s 7s 3s 2s")), 1433);
+ assert_eq!(eval_hand(parse_cards("6s 6d 6h Qs Qd")), 265);
+ assert_eq!(eval_hand(parse_cards("4s 4d 4h 4c 5d")), 140);
+ assert_eq!(eval_hand(parse_cards("Ts 9s 8s 7s 6s 7h 6d")), 5);
}
#[test]