Skip to main content

sokoban_elements/piece/
errors.rs

1use std::error::Error;
2use std::fmt::{Display, Formatter};
3
4/// Variants of [`PieceIdError`].
5#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
6pub enum PieceIdErrorKind {
7    InvalidBoxID,
8    InvalidGoalID,
9    InvalidPusherID,
10    InvalidPlusID,
11}
12
13/// Errors when working with [`BoxID`](crate::BoxID), [`GoalID`](crate::GoalID) and
14/// [`PusherID`](crate::PusherID).
15#[derive(Debug, Clone, PartialEq, Eq, Hash)]
16pub struct PieceIdError {
17    pub kind:  PieceIdErrorKind,
18    pub value: String,
19}
20
21impl PieceIdError {
22    pub fn kind(&self) -> PieceIdErrorKind { self.kind }
23}
24
25impl Error for PieceIdError {}
26
27impl Display for PieceIdError {
28    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
29        match &self.kind {
30            PieceIdErrorKind::InvalidBoxID => {
31                write!(
32                    f,
33                    "invalid value {} for BoxID, must be in range 0 .. BoxID::MAX",
34                    self.value
35                )
36            }
37
38            PieceIdErrorKind::InvalidGoalID => {
39                write!(
40                    f,
41                    "invalid value {} for GoalID, must be in range 0 .. GoalID::MAX",
42                    self.value
43                )
44            }
45
46            PieceIdErrorKind::InvalidPusherID => {
47                write!(
48                    f,
49                    "invalid value {} for PusherID, must be in range 0 .. PusherID::MAX",
50                    self.value
51                )
52            }
53
54            PieceIdErrorKind::InvalidPlusID => {
55                write!(
56                    f,
57                    "invalid value {} for PlusID, must be in range 1 .. u16::MAX",
58                    self.value
59                )
60            }
61        }
62    }
63}