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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::num::ParseIntError;
use crate::error::ParseError::BadInt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// Errors that may be encountered when parsing a string.
#[derive(Debug, Eq, PartialEq)]
pub enum ParseError {
/// Tried to parse a string, but it was not the expected length. The given `usize` is the
/// actual length.
BadStringLen(usize),
/// Tried to parse a multi-line string but encountered a line that was not the expected length.
/// The given `usize` is the actual length.
BadLineLen(usize),
/// Encountered an unexpected character in a string.
BadChar(char),
/// Tried to parse an empty string.
EmptyString,
/// Could not parse an integer from a string. This variant wraps the [`ParseIntError`] that was
/// returned when trying to parse.
BadInt(ParseIntError),
/// Tried to parse a string which represents an invalid [`Play`].
BadPlay(PlayError),
/// A generic error type where the given string could not be parsed for some reason.
BadString(String)
}
impl From<ParseIntError> for ParseError {
fn from(value: ParseIntError) -> Self {
BadInt(value)
}
}
/// Errors that may be encountered when constructing a [`Play`].
#[derive(Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum PlayError {
DisjointTiles
}
/// Errors relating to the board.
#[derive(Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum BoardError {
/// Coordinates are out of bounds, ie, not on board.
OutOfBounds,
/// There is no piece at the given tile, where one is expected.
NoPiece
}
/// Different ways a [`Play`] can be invalid.
#[derive(Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum PlayInvalid {
/// The piece being moved does not belong to the player whose turn it is.
WrongPlayer,
/// There is no piece to move at the given tile.
NoPiece,
/// The destination tile would be outside the board.
OutOfBounds,
/// The start and end tiles do not share an axis (ie, they are not on the same row or column).
NoCommonAxis,
/// Another piece is blocking the move.
BlockedByPiece,
/// The move is blocked by a special tile which, according to the game rules, is not passable
/// by this piece.
MoveThroughBlockedTile,
/// This move would end on a special tile which, according to the game rules, this piece may not
/// occupy.
MoveOntoBlockedTile,
/// The move is further than this piece is permitted to move in one go.
TooFar,
/// Game is already over.
GameOver
}