Skip to main content

evo/
error.rs

1use rusqlite::Error as SqlError;
2use std::fmt;
3use std::io;
4
5pub type Result<T> = std::result::Result<T, EvoError>;
6
7#[derive(Debug)]
8pub enum EvoError {
9    Io(io::Error),
10    Json(serde_json::Error),
11    Sqlite(SqlError),
12    Message(String),
13}
14
15impl fmt::Display for EvoError {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        match self {
18            Self::Io(err) => write!(f, "{err}"),
19            Self::Json(err) => write!(f, "{err}"),
20            Self::Sqlite(err) => write!(f, "{err}"),
21            Self::Message(message) => f.write_str(message),
22        }
23    }
24}
25
26impl std::error::Error for EvoError {}
27
28impl From<io::Error> for EvoError {
29    fn from(value: io::Error) -> Self {
30        Self::Io(value)
31    }
32}
33
34impl From<serde_json::Error> for EvoError {
35    fn from(value: serde_json::Error) -> Self {
36        Self::Json(value)
37    }
38}
39
40impl From<SqlError> for EvoError {
41    fn from(value: SqlError) -> Self {
42        Self::Sqlite(value)
43    }
44}
45
46impl From<String> for EvoError {
47    fn from(value: String) -> Self {
48        Self::Message(value)
49    }
50}
51
52impl From<&str> for EvoError {
53    fn from(value: &str) -> Self {
54        Self::Message(value.to_string())
55    }
56}