storage/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use data_model::Error as DataModelError;
use maze::Error as MazeError;
use std::{error::Error as StdError, io};

/// Represents a store error
#[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())
    }
}