cribbage_core/pegging/
two_card_pegging.rs

1use crate::card::Card;
2use crate::pegging::{OneCardPegging, Pegger};
3use crate::CribbageCoreError;
4
5#[derive(Clone, Copy)]
6pub struct TwoCardPegging {
7    cards: [Card; 2],
8}
9
10impl TwoCardPegging {
11    pub(in crate::pegging) fn new(cards: [Card; 2]) -> TwoCardPegging {
12        TwoCardPegging { cards }
13    }
14
15    pub fn play_card(
16        self,
17        card: Card,
18        pegger: &mut Pegger,
19    ) -> Result<(u8, OneCardPegging), (Self, CribbageCoreError)> {
20        let mut cards = self.cards[..].to_vec();
21        let index = cards[..]
22            .iter()
23            .position(|&c| c == card)
24            .ok_or((self, CribbageCoreError::InvalidCard))?;
25        cards.swap_remove(index);
26
27        match pegger.play_card(card) {
28            Ok(points) => Ok((points, OneCardPegging::new(cards[0]))),
29            Err(error) => Err((self, error)),
30        }
31    }
32}