use core::fmt; use serde::Deserialize; pub type MLE = Result; #[derive(Debug, Deserialize)] pub struct MLError { etype: ErrorType, message: String, } #[derive(Debug, Deserialize)] pub enum ErrorType { ArgumentError, ConfigError, LibToml, IoError, } impl std::error::Error for MLError { fn description(&self) -> &str { &self.message } } impl fmt::Display for MLError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.etype { ErrorType::ArgumentError => write!(f, "ARGS"), ErrorType::ConfigError => write!(f, "CONFIG"), ErrorType::LibToml => write!(f, "TOML"), ErrorType::IoError => write!(f, "IO") } } } impl From for MLError { fn from(error: toml::de::Error) -> Self { Self { etype: ErrorType::LibToml, message: error.to_string() } } } impl From for MLError { fn from(error: toml::ser::Error) -> Self { Self { etype: ErrorType::LibToml, message: error.to_string() } } } impl From for MLError { fn from(error: std::io::Error) -> Self { Self { etype: ErrorType::IoError, message: error.to_string() } } } impl MLError { pub fn new(etype: ErrorType, message: &str) -> Self { Self { etype, message: String::from(message) } } }