Skip to main content

sql_reverse_error/
result.rs

1use mysql;
2use quicli;
3use serde_yaml;
4use tera;
5
6#[allow(dead_code)]
7pub type Result<T> = std::result::Result<T, Error>;
8
9#[derive(Debug)]
10pub enum Error {
11    RegexError(regex::Error),
12    SerdeYamlError(serde_yaml::Error),
13    SerdeJsonError(serde_json::Error),
14    IoError(std::io::Error),
15    QuiCliError(quicli::prelude::Error),
16    TeraError(tera::Error),
17    MysqlUrlError(mysql::UrlError),
18    MysqlError(mysql::error::Error),
19    PostgresError(tokio_postgres::Error),
20    CustomError(String),
21}
22
23#[derive(Debug, Clone, Default)]
24pub struct CustomError {
25    pub message: String,
26}
27
28impl From<String> for CustomError {
29    fn from(message: String) -> CustomError {
30        CustomError { message }
31    }
32}
33
34impl<'a> From<CustomError> for &'a dyn std::error::Error {
35    fn from(error: CustomError) -> &'a dyn std::error::Error {
36        error.into()
37    }
38}
39
40impl std::error::Error for CustomError {}
41
42impl std::fmt::Display for CustomError {
43    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
44        write!(f, "{}", self.message)
45    }
46}
47
48impl std::error::Error for Error {
49    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
50        match &self {
51            Error::RegexError(ref err) => Some(err),
52            Error::SerdeYamlError(ref error) => Some(error),
53            Error::SerdeJsonError(ref error) => Some(error),
54            Error::IoError(ref error) => Some(error),
55            Error::QuiCliError(ref error) => {
56                let s: CustomError = From::from(error.to_string());
57                Some(s.into())
58            }
59            Error::TeraError(ref error) => Some(error),
60            Error::MysqlUrlError(ref error) => Some(error),
61            Error::MysqlError(ref error) => Some(error),
62            Error::PostgresError(ref error) => Some(error),
63            Error::CustomError(ref error) => {
64                let s: CustomError = From::from(error.to_string());
65                Some(s.into())
66            }
67        }
68    }
69}
70
71impl std::fmt::Display for Error {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        match &self {
74            Error::RegexError(ref error) => error.fmt(f),
75            Error::SerdeYamlError(ref error) => error.fmt(f),
76            Error::SerdeJsonError(ref error) => error.fmt(f),
77            Error::IoError(ref error) => error.fmt(f),
78            Error::QuiCliError(ref error) => error.fmt(f),
79            Error::TeraError(ref error) => error.fmt(f),
80            Error::MysqlUrlError(ref error) => error.fmt(f),
81            Error::MysqlError(ref error) => error.fmt(f),
82            Error::PostgresError(ref error) => error.fmt(f),
83            Error::CustomError(ref error) => error.fmt(f),
84        }
85    }
86}
87
88impl From<String> for Error {
89    fn from(error: String) -> Self {
90        Error::CustomError(error)
91    }
92}
93
94impl From<mysql::error::Error> for Error {
95    fn from(error: mysql::error::Error) -> Self {
96        Error::MysqlError(error)
97    }
98}
99
100impl From<tokio_postgres::Error> for Error {
101    fn from(error: tokio_postgres::Error) -> Self {
102        Error::PostgresError(error)
103    }
104}
105
106impl From<mysql::UrlError> for Error {
107    fn from(error: mysql::UrlError) -> Self {
108        Error::MysqlUrlError(error)
109    }
110}
111
112impl From<tera::Error> for Error {
113    fn from(error: tera::Error) -> Self {
114        Error::TeraError(error)
115    }
116}
117
118impl From<quicli::prelude::Error> for Error {
119    fn from(error: quicli::prelude::Error) -> Error {
120        Error::CustomError(error.to_string())
121    }
122}
123
124impl From<serde_yaml::Error> for Error {
125    fn from(error: serde_yaml::Error) -> Error {
126        Error::SerdeYamlError(error)
127    }
128}
129
130impl From<serde_json::Error> for Error {
131    fn from(error: serde_json::Error) -> Error {
132        Error::SerdeJsonError(error)
133    }
134}
135
136impl From<std::io::Error> for Error {
137    fn from(s: std::io::Error) -> Self {
138        Error::IoError(s)
139    }
140}
141
142impl From<regex::Error> for Error {
143    fn from(s: regex::Error) -> Self {
144        Error::RegexError(s)
145    }
146}