diff options
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/src/error.rs b/src/error.rs index c82688c..1ac2a48 100644 --- a/src/error.rs +++ b/src/error.rs | |||
@@ -1,16 +1,20 @@ | |||
1 | use core::fmt; | 1 | use core::fmt; |
2 | use serde::Deserialize; | ||
2 | 3 | ||
3 | pub type MLE<T> = Result<T, MLError>; | 4 | pub type MLE<T> = Result<T, MLError>; |
4 | 5 | ||
5 | #[derive(Debug)] | 6 | #[derive(Debug, Deserialize)] |
6 | pub struct MLError { | 7 | pub struct MLError { |
7 | etype: ErrorType, | 8 | etype: ErrorType, |
8 | message: String, | 9 | message: String, |
9 | } | 10 | } |
10 | 11 | ||
11 | #[derive(Debug)] | 12 | #[derive(Debug, Deserialize)] |
12 | pub enum ErrorType { | 13 | pub enum ErrorType { |
13 | ArgumentError | 14 | ArgumentError, |
15 | ConfigError, | ||
16 | LibToml, | ||
17 | IoError, | ||
14 | } | 18 | } |
15 | 19 | ||
16 | impl std::error::Error for MLError { | 20 | impl std::error::Error for MLError { |
@@ -22,11 +26,32 @@ impl std::error::Error for MLError { | |||
22 | impl fmt::Display for MLError { | 26 | impl fmt::Display for MLError { |
23 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 27 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
24 | match self.etype { | 28 | match self.etype { |
25 | ErrorType::ArgumentError => write!(f, "ARGS") | 29 | ErrorType::ArgumentError => write!(f, "ARGS"), |
30 | ErrorType::ConfigError => write!(f, "CONFIG"), | ||
31 | ErrorType::LibToml => write!(f, "TOML"), | ||
32 | ErrorType::IoError => write!(f, "IO") | ||
26 | } | 33 | } |
27 | } | 34 | } |
28 | } | 35 | } |
29 | 36 | ||
37 | impl From<toml::de::Error> for MLError { | ||
38 | fn from(error: toml::de::Error) -> Self { | ||
39 | Self { etype: ErrorType::LibToml, message: error.to_string() } | ||
40 | } | ||
41 | } | ||
42 | |||
43 | impl From<toml::ser::Error> for MLError { | ||
44 | fn from(error: toml::ser::Error) -> Self { | ||
45 | Self { etype: ErrorType::LibToml, message: error.to_string() } | ||
46 | } | ||
47 | } | ||
48 | |||
49 | impl From<std::io::Error> for MLError { | ||
50 | fn from(error: std::io::Error) -> Self { | ||
51 | Self { etype: ErrorType::IoError, message: error.to_string() } | ||
52 | } | ||
53 | } | ||
54 | |||
30 | impl MLError { | 55 | impl MLError { |
31 | pub fn new(etype: ErrorType, message: &str) -> Self { | 56 | pub fn new(etype: ErrorType, message: &str) -> Self { |
32 | Self { etype, message: String::from(message) } | 57 | Self { etype, message: String::from(message) } |