sudoku_variants/
error.rs

1//! This module contains some error and result definitions used in this crate.
2
3use std::num::ParseIntError;
4
5/// Miscellaneous errors that can occur on some methods in the
6/// [root module](crate). This does not exclude errors that occur when parsing
7/// Sudoku, see [SudokuParseError] for that.
8#[derive(Debug, Eq, PartialEq)]
9pub enum SudokuError {
10
11    /// Indicates that the dimensions specified for a created Sudoku are
12    /// invalid. This is the case if they are less than 1.
13    InvalidDimensions,
14
15    /// Indicates that some number is invalid for the size of the grid in
16    /// question. This is the case if it is less than 1 or greater than the
17    /// size.
18    InvalidNumber,
19
20    /// Indicates that the specified coordinates (column and row) lie outside
21    /// the Sudoku grid in question. This is the case if they are greater than
22    /// or equal to the size.
23    OutOfBounds,
24
25    /// An error that is raised whenever it is attempted to generate a Sudoku
26    /// with a constraint that is not satisfied by any Sudoku with the given
27    /// parameters.
28    UnsatisfiableConstraint
29}
30
31/// Syntactic sugar for `Result<V, SudokuError>`.
32pub type SudokuResult<V> = Result<V, SudokuError>;
33
34/// An enumeration of the errors that may occur when parsing a
35/// [Sudoku](crate::Sudoku) or [SudokuGrid](crate::SudokuGrid).
36#[derive(Debug, Eq, PartialEq)]
37pub enum SudokuParseError {
38
39    /// Indicates that the code has the wrong number of parts, which are
40    /// separated by semicolons. The code should have two parts: dimensions and
41    /// cells (separated by ';'), so if the code does not contain exactly one
42    /// semicolon, this error will be returned.
43    WrongNumberOfParts,
44
45    /// Indicates that the number of cells (which are separated by commas) does
46    /// not equal the number deduced from the dimensions.
47    WrongNumberOfCells,
48
49    /// Indicates that the dimensions have the wrong format. They should be of
50    /// the form `<block_width>x<block_height>`, so if the amount of 'x's in
51    /// the dimension string is not exactly one, this error will be raised.
52    MalformedDimensions,
53
54    /// Indicates that the provided dimensions are invalid (i.e. at least one
55    /// is zero).
56    InvalidDimensions,
57
58    /// Indicates that one of the numbers (dimension or cell content) could not
59    /// be parsed.
60    NumberFormatError,
61
62    /// Indicates that a cell is filled with an invalid number (0 or more than
63    /// the grid size).
64    InvalidNumber
65}
66
67impl From<ParseIntError> for SudokuParseError {
68    fn from(_: ParseIntError) -> Self {
69        SudokuParseError::NumberFormatError
70    }
71}
72
73/// Syntactic sugar for `Result<V, SudokuParseError>`.
74pub type SudokuParseResult<V> = Result<V, SudokuParseError>;