crossword_puzzle/error.rs
1//! Defines the error types used throughout the crossword puzzle generator.
2//! This module provides a structured way to handle various issues that can arise
3//! during word processing, grid generation, and other application-level operations.
4
5use std::fmt;
6
7/// `WordError` represents specific errors that can occur when creating, validating, or manipulating a `Word`.
8/// These errors typically arise from invalid input or attempts to create words that do not conform to expected rules.
9#[derive(Debug)]
10pub enum WordError {
11 /// Indicates that a word segment (prefix, crossed character, or suffix) is empty or contains only whitespace.
12 EmptyOrWhitespaceSegment,
13 /// Indicates that a word segment contains lowercase characters, which are not allowed.
14 LowercaseCharactersInSegment,
15}
16
17/// Implements the `Display` trait for `WordError`, allowing errors to be formatted as user-friendly strings.
18impl fmt::Display for WordError {
19 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20 match self {
21 WordError::EmptyOrWhitespaceSegment => {
22 write!(f, "Segment cannot be empty or contain only whitespace.")
23 }
24 WordError::LowercaseCharactersInSegment => {
25 write!(f, "Segment cannot contain lowercase characters.")
26 }
27 }
28 }
29}
30
31/// Implements the `Error` trait for `WordError`, providing a common interface for error handling.
32impl std::error::Error for WordError {}
33
34/// `GridError` represents errors that can occur during operations on the crossword `Grid`.
35/// These errors typically relate to invalid directions, word placement issues, or underlying `WordError`s.
36#[derive(Debug)]
37pub enum GridError {
38 /// Indicates that an invalid or unsupported direction was provided for a grid operation.
39 InvalidDirection(String),
40 /// Wraps a `WordError` that occurred during a grid operation, providing more context.
41 WordError(WordError),
42}
43
44/// Implements the `Display` trait for `GridError`, allowing errors to be formatted as user-friendly strings.
45impl fmt::Display for GridError {
46 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47 match self {
48 GridError::InvalidDirection(msg) => write!(f, "Invalid direction: {msg}"),
49 GridError::WordError(e) => write!(f, "Word error: {e}"),
50 }
51 }
52}
53
54/// Implements the `Error` trait for `GridError`, providing a common interface for error handling.
55impl std::error::Error for GridError {}
56
57/// Implements conversion from `WordError` to `GridError`.
58/// This allows `WordError`s to be easily wrapped within `GridError`s.
59impl From<WordError> for GridError {
60 fn from(err: WordError) -> Self {
61 GridError::WordError(err)
62 }
63}
64
65/// `Error` represents general application errors, encompassing `WordError` and `GridError`,
66/// as well as custom error messages.
67#[derive(Debug)]
68pub enum Error {
69 /// Wraps a `WordError` that occurred within the application.
70 WordError(WordError),
71 /// Wraps a `GridError` that occurred within the application.
72 GridError(GridError),
73 /// Represents a custom error message, useful for general application-level failures.
74 Custom(String),
75}
76
77/// Implements the `Display` trait for `Error`, allowing general application errors to be formatted as user-friendly strings.
78impl fmt::Display for Error {
79 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80 match self {
81 Error::WordError(e) => write!(f, "Word error: {e}"),
82 Error::GridError(e) => write!(f, "Grid error: {e}"),
83 Error::Custom(msg) => write!(f, "Application error: {msg}"),
84 }
85 }
86}
87
88/// Implements the `Error` trait for `Error`, providing a common interface for general application error handling.
89impl std::error::Error for Error {}
90
91/// Implements conversion from `WordError` to `Error`.
92/// This allows `WordError`s to be easily wrapped within the top-level `Error` type.
93impl From<WordError> for Error {
94 fn from(err: WordError) -> Self {
95 Error::WordError(err)
96 }
97}
98
99/// Implements conversion from `GridError` to `Error`.
100/// This allows `GridError`s to be easily wrapped within the top-level `Error` type.
101impl From<GridError> for Error {
102 fn from(err: GridError) -> Self {
103 Error::GridError(err)
104 }
105}
106