Skip to main content

games/blackjack/
errors.rs

1use crate::errors::BASE_BLACKJACK_ERROR_CODE;
2use core::fmt;
3
4/// Blackjack errors
5#[repr(C)]
6#[derive(Debug, Copy, Clone, serde::Serialize, serde::Deserialize, Eq, PartialEq, Hash)]
7pub enum BlackJackError {
8    /// Player has lost the game, no other actions can be made. (2001)
9    PlayerLostError,
10    /// Player has won the game, no other actions can be made. (2002)
11    PlayerWonError,
12    /// Game Ran out of cards (2003)
13    OutOfCardsError,
14}
15
16impl fmt::Display for BlackJackError {
17    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
18        use BlackJackError::*;
19        match *self {
20            PlayerLostError => {
21                f.write_str("Player has lost the game, no other actions can be made.")
22            }
23            PlayerWonError => f.write_str("Player has won the game, no other actions can be made."),
24            OutOfCardsError => f.write_str("The game has run out of cards in the deck."),
25        }
26    }
27}
28
29impl crate::errors::ErrorCode for BlackJackError {
30    fn error_code(&self) -> i32 {
31        BASE_BLACKJACK_ERROR_CODE
32            + match *self {
33                BlackJackError::PlayerLostError => 1,
34                BlackJackError::PlayerWonError => 2,
35                BlackJackError::OutOfCardsError => 3,
36            }
37    }
38}