summaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs33
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 @@
1use core::fmt; 1use core::fmt;
2use serde::Deserialize;
2 3
3pub type MLE<T> = Result<T, MLError>; 4pub type MLE<T> = Result<T, MLError>;
4 5
5#[derive(Debug)] 6#[derive(Debug, Deserialize)]
6pub struct MLError { 7pub 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)]
12pub enum ErrorType { 13pub enum ErrorType {
13 ArgumentError 14 ArgumentError,
15 ConfigError,
16 LibToml,
17 IoError,
14} 18}
15 19
16impl std::error::Error for MLError { 20impl std::error::Error for MLError {
@@ -22,11 +26,32 @@ impl std::error::Error for MLError {
22impl fmt::Display for MLError { 26impl 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
37impl 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
43impl 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
49impl 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
30impl MLError { 55impl 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) }