Skip to main content

sashite_qi/
error.rs

1//! The error type returned by fallible `Qi` operations.
2
3/// An error from constructing or transforming a [`Qi`](crate::Qi) position.
4///
5/// Construction errors come from an invalid shape; transformation errors come
6/// from an out-of-range index, an impossible hand removal, or a piece count that
7/// would exceed the board.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9#[non_exhaustive]
10pub enum Error {
11    /// The shape had no dimensions.
12    EmptyShape,
13    /// The shape had more than [`MAX_DIMENSIONS`](crate::MAX_DIMENSIONS).
14    TooManyDimensions,
15    /// A dimension size was zero.
16    DimensionTooSmall,
17    /// A dimension size exceeded [`MAX_DIMENSION_SIZE`](crate::MAX_DIMENSION_SIZE).
18    DimensionTooLarge,
19    /// The total number of squares exceeded
20    /// [`MAX_SQUARE_COUNT`](crate::MAX_SQUARE_COUNT).
21    TooManySquares,
22    /// A board index was not a valid square on this board.
23    IndexOutOfRange,
24    /// A hand removal asked to remove more copies of a piece than were held.
25    HandUnderflow,
26    /// The number of pieces (board plus both hands) would exceed the number of
27    /// squares.
28    TooManyPieces,
29}
30
31impl core::fmt::Display for Error {
32    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
33        let message = match self {
34            Self::EmptyShape => "shape must have at least one dimension",
35            Self::TooManyDimensions => "shape exceeds the maximum number of dimensions",
36            Self::DimensionTooSmall => "a dimension size must be at least 1",
37            Self::DimensionTooLarge => "a dimension size exceeds the maximum",
38            Self::TooManySquares => "the board exceeds the maximum number of squares",
39            Self::IndexOutOfRange => "a board index is out of range",
40            Self::HandUnderflow => "cannot remove a piece that is not in the hand",
41            Self::TooManyPieces => "the number of pieces exceeds the number of squares",
42        };
43        f.write_str(message)
44    }
45}
46
47impl core::error::Error for Error {}