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
/// This module contains the error definition for the Backgammon game.
use std::fmt;

/// Holds all possible errors that can occur during a Backgammon game.
#[derive(Debug)]
pub enum Error {
    /// Game has already started
    GameStarted,
    /// Game has already ended
    GameEnded,
    /// Opponent offered doubling cube. Need to react on this event first.
    CubeReceived,
    /// Doubling not permitted
    DoublingNotPermitted,
    /// Invalid cube value
    CubeValueInvalid,
    /// Invalid player
    PlayerInvalid,
    /// Field blocked
    FieldBlocked,
    /// Invalid field
    FieldInvalid,
    /// Not your turn
    NotYourTurn,
    /// Invalid move
    MoveInvalid,
    /// Move first
    MoveFirst,
}

// implement Error trait
impl std::error::Error for Error {}

// implement Display trait
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::GameStarted => write!(f, "Game has already started"),
            Error::GameEnded => write!(f, "Game has already ended"),
            Error::PlayerInvalid => write!(f, "Invalid player"),
            Error::CubeReceived => {
                write!(
                    f,
                    "Opponent offered dice. Need to first accept or decline the doubling dice."
                )
            }
            Error::CubeValueInvalid => write!(f, "Invalid cube value"),
            Error::DoublingNotPermitted => write!(f, "Doubling not permitted"),
            Error::FieldBlocked => write!(f, "Field blocked"),
            Error::FieldInvalid => write!(f, "Invalid field"),
            Error::NotYourTurn => write!(f, "Not your turn"),
            Error::MoveInvalid => write!(f, "Invalid move"),
            Error::MoveFirst => write!(f, "Move first"),
        }
    }
}