1use std::fmt;
2use serde_json;
3use crate::cell::{ Coordinates, MazeType };
4use crate::direction::Direction;
5use crate::algorithms::MazeAlgorithm;
6
7#[derive(Debug)]
8pub enum Error {
9 InvalidCellForDeltaMaze { cell_maze_type: MazeType },
10 InvalidCellForNonDeltaMaze { cell_maze_type: MazeType },
11 AlgorithmUnavailableForMazeType { algorithm: MazeAlgorithm, maze_type: MazeType },
12 FlattenedVectorDimensionsMismatch { vector_size: usize, maze_width: usize, maze_height: usize },
13 OutOfBoundsCoordinates { coordinates: Coordinates, maze_width: usize, maze_height: usize },
14 MissingCoordinates { coordinates: Coordinates },
15 NoValidNeighbor { coordinates: Coordinates },
16 MultipleActiveCells { count: usize },
17 NoActiveCells,
18 InvalidDirection { direction: String },
19 MoveUnavailable { attempted_move: Direction, available_moves: Vec<Direction>},
20 GridDimensionsExceedLimitForCaptureSteps { width: usize, height: usize },
21 NoCellAtCoordinates { coordinates: Coordinates },
22 InvalidCellCoordinates { coordinates: Coordinates },
23 InvalidStartCoordinates { coordinates: Coordinates },
24 InvalidGoalCoordinates { coordinates: Coordinates },
25 SerializationError(serde_json::Error),
26 EmptyList,
27}
28
29impl fmt::Display for Error {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 match self {
32 Error::InvalidCellForDeltaMaze { cell_maze_type } => {
33 write!(f, "Cannot generate non-triangle cells for Delta maze_type {:?}", cell_maze_type)
34 }
35 Error::InvalidCellForNonDeltaMaze { cell_maze_type } => {
36 write!(f, "Cannot generate triangle cells for non-Delta maze_type {:?}", cell_maze_type)
37 }
38 Error::AlgorithmUnavailableForMazeType{ algorithm, maze_type } => {
39 write!(f, "MazeAlgorithm {:?} is unavailable for MazeType {:?}", algorithm, maze_type)
40 }
41 Error::FlattenedVectorDimensionsMismatch { vector_size, maze_width, maze_height } => {
42 write!(f, "Flattened vector size mismatch: expected size {} ({} x {}), but got {}", maze_width * maze_height, maze_width, maze_height, vector_size)
43 }
44 Error::OutOfBoundsCoordinates { coordinates, maze_width, maze_height } => {
45 write!(f, "Out of bounds coordinates: coordinates {:?} exceed maze dimensions {} x {}", coordinates, maze_width, maze_height)
46 }
47 Error::MissingCoordinates { coordinates } => {
48 write!(f, "Missing coordinates: {:?}", coordinates )
49 }
50 Error::NoValidNeighbor { coordinates } => {
51 write!(f, "No valid neighbors: {:?}", coordinates )
52 }
53 Error::MultipleActiveCells { count } => {
54 write!(f, "{:?} active cells, there should only ever be exactly 1 active cell", count )
55 }
56 Error::NoActiveCells => {
57 write!(f, "No active cells, there should always be exactly 1 active cell" )
58 }
59 Error::MoveUnavailable { attempted_move, available_moves } => {
60 write!(f, "Cannot make move {:?} because it is unavailable. Available moves are: {:?}", attempted_move, available_moves.into_iter().map(|dir| dir.to_string()).collect::<Vec<_>>().join(", ") )
61 }
62 Error::InvalidDirection { direction } => {
63 write!(f, "Invalid Direction: {:?}", direction )
64 }
65 Error::GridDimensionsExceedLimitForCaptureSteps { width, height } => {
66 write!(f, "Grid dimensions {:?},{:?} exceed limit for enabling capture_steps. Neither width nor height can exceed 100 for this feature to be enabled", width, height )
67 }
68 Error::NoCellAtCoordinates{ coordinates } => {
69 write!(f, "No cell exists at coordinates {:?}", coordinates )
70 }
71 Error::InvalidCellCoordinates{ coordinates } => {
72 write!(f, "Invalid cell coordinates {:?}", coordinates )
73 }
74 Error::InvalidStartCoordinates{ coordinates } => {
75 write!(f, "Invalid start coordinates {:?}", coordinates )
76 }
77 Error::InvalidGoalCoordinates{ coordinates } => {
78 write!(f, "Invalid goal coordinates {:?}", coordinates )
79 }
80 Error::SerializationError(e) => {
81 write!(f, "Serialization error: {}", e)
82 }
83 Error::EmptyList => {
84 write!(f, "Attempted operation on an empty list")
85 }
86 }
87 }
88}
89
90impl std::error::Error for Error {
91 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
92 match self {
93 Error::SerializationError(e) => Some(e), _ => None,
95 }
96 }
97}
98
99impl From<serde_json::Error> for Error {
100 fn from(e: serde_json::Error) -> Self {
101 Error::SerializationError(e)
102 }
103}