Skip to main content

turnbase/
error.rs

1//! Engine error type.
2
3use crate::player::PlayerId;
4
5/// Errors returned by the engine's checked entry points.
6///
7/// The in-place `apply` primitive assumes a legal action and does not return
8/// errors; these surface from checked helpers such as `apply_cloned`, which
9/// validate before mutating. More variants may be added, so match with a
10/// wildcard arm.
11#[derive(Clone, PartialEq, Eq, Debug)]
12#[non_exhaustive]
13pub enum Error {
14    /// The action is not legal for `player` in the current state.
15    IllegalAction {
16        /// The seat that attempted the action.
17        player: PlayerId,
18    },
19    /// `player` is not among the active players owed a decision right now.
20    NotActive {
21        /// The seat that attempted to act out of turn.
22        player: PlayerId,
23    },
24}
25
26impl core::fmt::Display for Error {
27    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
28        match self {
29            Self::IllegalAction { player } => {
30                write!(f, "illegal action for {player}")
31            }
32            Self::NotActive { player } => {
33                write!(f, "{player} is not active")
34            }
35        }
36    }
37}
38
39impl std::error::Error for Error {}
40
41#[cfg(test)]
42mod tests {
43    use super::Error;
44    use crate::player::PlayerId;
45
46    #[test]
47    fn display_mentions_the_player() {
48        let e = Error::IllegalAction {
49            player: PlayerId::new(1),
50        };
51        assert_eq!(e.to_string(), "illegal action for p1");
52    }
53}