use data_model::Error as DataModelError;
use maze::Error as MazeError;
use std::{error::Error as StdError, io};
#[derive(Debug)]
pub enum Error {
UserEmailExists(),
UserEmailInvalid(),
UserIdExists(String),
UserIdMissing(),
UserIdNotFound(String),
UserNameExists(),
UserNameMissing(),
UserNotFound(),
UserPasswordMissing(),
MazeError(MazeError),
MazeIdMissing(),
MazeIdNotFound(String),
MazeIdExists(String),
MazeNameMissing(),
MazeNameNotFound(String),
MazeNameAlreadyExists(String),
DataModelError(DataModelError),
Io(std::io::Error),
SerdeJson(serde_json::Error),
Other(String),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::UserEmailExists() => write!(f, "The email is already taken"),
Error::UserEmailInvalid() => write!(f, "The email address is invalid"),
Error::UserIdExists(id) => write!(f, "A user with id '{}' already exists", id),
Error::UserIdMissing() => write!(f, "No id provided for the user"),
Error::UserIdNotFound(id) => write!(f, "A user with id '{}' was not found", id),
Error::UserNameExists() => write!(f, "The username is already taken"),
Error::UserNameMissing() => write!(f, "No username provided for the user"),
Error::UserNotFound() => write!(f, "User not found"),
Error::UserPasswordMissing() => write!(f, "No password provided for the user"),
Error::MazeError(e) => write!(f, "Maze error: {}", e),
Error::MazeIdMissing() => write!(f, "No id provided for the maze"),
Error::MazeIdNotFound(id) => write!(f, "A maze with id '{}' was not found", id),
Error::MazeIdExists(id) => write!(f, "A maze with id '{}' already exists", id),
Error::MazeNameMissing() => write!(f, "No name provided for the maze"),
Error::MazeNameNotFound(name) => write!(f, "A maze with the name '{}' was not found", name),
Error::MazeNameAlreadyExists(name) => {
write!(f, "A maze with the name '{}' already exists", name)
}
Error::DataModelError(e) => write!(f, "Data model error: {}", e),
Error::Io(e) => write!(f, "I/O error: {}", e),
Error::SerdeJson(ref error) => write!(f, "{}", error),
Error::Other(msg) => write!(f, "Error: {}", msg),
}
}
}
impl StdError for Error {}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Error {
Error::Io(err)
}
}
impl From<MazeError> for Error {
fn from(err: MazeError) -> Error {
Error::MazeError(err)
}
}
impl From<DataModelError> for Error {
fn from(err: DataModelError) -> Error {
Error::DataModelError(err)
}
}
impl From<serde_json::Error> for Error {
fn from(error: serde_json::Error) -> Self {
Error::SerdeJson(error)
}
}
impl From<Error> for io::Error {
fn from(err: Error) -> Self {
io::Error::new(io::ErrorKind::Other, err.to_string())
}
}