cribbage_core/
lib.rs

1use std::error;
2use std::fmt;
3
4mod board;
5mod card;
6mod deck;
7mod hand;
8mod pegging;
9
10pub use crate::board::{
11    custom_board, standard_four_player_board, standard_three_player_board,
12    standard_two_player_board, Board, FourPlayerScore, FourPlayers, ThreePlayerScore, ThreePlayers,
13    TwoPlayerScore, TwoPlayers,
14};
15pub use crate::card::{Card, Rank, Suit};
16pub use crate::deck::Deck;
17pub use crate::hand::{
18    deal_four_player_hand, deal_three_player_hand, deal_two_player_hand, CribCards,
19    FourPlayerCribPart, FourPlayerDeal, Hand, KeptCards, ThreePlayerCribPart, ThreePlayerDeal,
20    TwoPlayerCribPart, TwoPlayerDeal,
21};
22pub use crate::pegging::{
23    FourCardPegging, OneCardPegging, Pegger, ThreeCardPegging, TwoCardPegging,
24};
25
26#[derive(Debug, Eq, PartialEq)]
27pub enum CribbageCoreError {
28    InvalidCard,
29    InvalidCardString,
30    InvalidScoreId,
31    NotEnoughCards,
32    WinnerExists,
33}
34
35impl fmt::Display for CribbageCoreError {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match *self {
38            CribbageCoreError::InvalidCard
39            | CribbageCoreError::InvalidCardString
40            | CribbageCoreError::InvalidScoreId
41            | CribbageCoreError::NotEnoughCards
42            | CribbageCoreError::WinnerExists => write!(f, "{:?}", self),
43        }
44    }
45}
46
47impl error::Error for CribbageCoreError {
48    fn description(&self) -> &str {
49        match *self {
50            CribbageCoreError::InvalidCard => "Invalid card played",
51            CribbageCoreError::InvalidCardString => "Invalid string representation of card",
52            CribbageCoreError::InvalidScoreId => "Invalid score ID",
53            CribbageCoreError::NotEnoughCards => "Not enough cards in deck",
54            CribbageCoreError::WinnerExists => "Winner already exists",
55        }
56    }
57
58    fn cause(&self) -> Option<&dyn error::Error> {
59        match *self {
60            CribbageCoreError::InvalidCard
61            | CribbageCoreError::InvalidCardString
62            | CribbageCoreError::InvalidScoreId
63            | CribbageCoreError::NotEnoughCards
64            | CribbageCoreError::WinnerExists => None,
65        }
66    }
67}