use std::{
convert::From,
error::Error,
fmt::{self, Display},
};
#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
pub enum GridParseError {
BadSize(GridSizeError),
UnexpectedCharacter(char),
}
impl Error for GridParseError {}
impl Display for GridParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
GridParseError::BadSize(e) => write!(f, "bad grid size: {}", e),
GridParseError::UnexpectedCharacter(c) => {
write!(f, "found unexpected character `{}`", c)
}
}
}
}
impl From<GridSizeError> for GridParseError {
fn from(err: GridSizeError) -> Self {
GridParseError::BadSize(err)
}
}
#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
pub enum GridError {
Illegal,
}
impl Error for GridError {}
impl Display for GridError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
GridError::Illegal => write!(f, "grid is illegal"),
}
}
}
#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
pub enum GridSizeError {
EmptyGrid,
NotASquare {
line: usize,
found: usize,
expected: usize,
},
OddNumberSize(usize),
}
impl Error for GridSizeError {}
impl Display for GridSizeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
GridSizeError::EmptyGrid => write!(f, "grid is empty"),
GridSizeError::NotASquare { line, found, expected } => {
write!(
f,
"grid is not a square (line {}, expected {} characters, found {})",
line, expected, found
)
}
GridSizeError::OddNumberSize(n) => {
write!(f, "grid size is an odd number ({} lines found)", n)
}
}
}
}