1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::error;
use std::fmt;

mod board;
mod card;
mod deck;
mod hand;
mod pegging;

pub use crate::board::{
    custom_board, standard_four_player_board, standard_three_player_board,
    standard_two_player_board, Board, FourPlayerScore, FourPlayers, ThreePlayerScore, ThreePlayers,
    TwoPlayerScore, TwoPlayers,
};
pub use crate::card::{Card, Rank, Suit};
pub use crate::deck::Deck;
pub use crate::hand::{
    deal_four_player_hand, deal_three_player_hand, deal_two_player_hand, CribCards,
    FourPlayerCribPart, FourPlayerDeal, Hand, KeptCards, ThreePlayerCribPart, ThreePlayerDeal,
    TwoPlayerCribPart, TwoPlayerDeal,
};
pub use crate::pegging::{
    FourCardPegging, OneCardPegging, Pegger, ThreeCardPegging, TwoCardPegging,
};

#[derive(Debug, Eq, PartialEq)]
pub enum CribbageCoreError {
    InvalidCard,
    InvalidCardString,
    InvalidScoreId,
    NotEnoughCards,
    WinnerExists,
}

impl fmt::Display for CribbageCoreError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            CribbageCoreError::InvalidCard
            | CribbageCoreError::InvalidCardString
            | CribbageCoreError::InvalidScoreId
            | CribbageCoreError::NotEnoughCards
            | CribbageCoreError::WinnerExists => write!(f, "{:?}", self),
        }
    }
}

impl error::Error for CribbageCoreError {
    fn description(&self) -> &str {
        match *self {
            CribbageCoreError::InvalidCard => "Invalid card played",
            CribbageCoreError::InvalidCardString => "Invalid string representation of card",
            CribbageCoreError::InvalidScoreId => "Invalid score ID",
            CribbageCoreError::NotEnoughCards => "Not enough cards in deck",
            CribbageCoreError::WinnerExists => "Winner already exists",
        }
    }

    fn cause(&self) -> Option<&dyn error::Error> {
        match *self {
            CribbageCoreError::InvalidCard
            | CribbageCoreError::InvalidCardString
            | CribbageCoreError::InvalidScoreId
            | CribbageCoreError::NotEnoughCards
            | CribbageCoreError::WinnerExists => None,
        }
    }
}