Skip to main content

games/solitaire/
errors.rs

1/// Alias for solitaire
2pub type SError<T> = Result<T, SolitaireError>;
3
4use crate::errors::{BASE_SOLITAIRE_ERROR_CODE, ErrorCode};
5use core::fmt::{self, Display};
6
7/// Solitaire Errors
8#[repr(C)]
9#[derive(Copy, Clone, Debug, serde::Serialize, serde::Deserialize, Hash, PartialEq, Eq)]
10pub enum SolitaireError {
11    /// Invalid movement (3001)
12    IllegalMovementError,
13    /// Out of range movement (3002)
14    OutOfRangeError,
15    /// Out of cards, should be impossible (3003)
16    OutOfCardsError,
17    /// Out of moves, exceeded u32 limit (3004)
18    OutOfMoves,
19}
20
21impl Display for SolitaireError {
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        use SolitaireError::*;
24        match *self {
25            IllegalMovementError => f.write_str("Illegal Movement"),
26            OutOfRangeError => {
27                f.write_str("Requested movement extends beyond any position cards lay")
28            }
29            OutOfCardsError => f.write_str("Out of cards"),
30            OutOfMoves => f.write_str("Out of moves"),
31        }
32    }
33}
34
35impl ErrorCode for SolitaireError {
36    fn error_code(&self) -> i32 {
37        BASE_SOLITAIRE_ERROR_CODE
38            + match *self {
39                SolitaireError::IllegalMovementError => 1,
40                SolitaireError::OutOfRangeError => 2,
41                SolitaireError::OutOfCardsError => 3,
42                SolitaireError::OutOfMoves => 4,
43            }
44    }
45}