use core::fmt; use serde::Deserialize; pub type MLE = Result; #[derive(Debug, Deserialize)] pub struct MLErr { etype: EType, message: String, } #[derive(Debug, Deserialize)] pub enum EType { ArgumentError, ArgumentCountError, ConfigError, DBError, ModError, LibToml, LibSql, LibReq, LibChrono, LibJson, LibIndicatif, IoError, Other, } impl std::error::Error for MLErr { fn description(&self) -> &str { &self.message } } impl fmt::Display for MLErr { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.etype { EType::ArgumentError => { write!(f, "User input not accepted: {}", self.message) } EType::ArgumentCountError => { write!(f, "Too many/too few arguments") } EType::ConfigError => write!(f, "CONFIG"), EType::DBError => write!(f, "Database: {}", self.message), EType::ModError => write!(f, "Mod: {}", self.message), EType::LibToml => write!(f, "TOML"), EType::LibSql => write!(f, "SQL: {}", self.message), EType::LibReq => write!(f, "REQWEST"), EType::LibChrono => write!(f, "Chrono error: {}", self.message), EType::LibJson => write!(f, "JSON: {}", self.message), EType::LibIndicatif => write!(f, "Indicativ: {}", self.message), EType::IoError => write!(f, "IO"), EType::Other => write!(f, "OTHER"), } } } impl From for MLErr { fn from(error: reqwest::Error) -> Self { Self { etype: EType::LibReq, message: error.to_string(), } } } impl From for MLErr { fn from(error: toml::de::Error) -> Self { Self { etype: EType::LibToml, message: error.to_string(), } } } impl From for MLErr { fn from(error: rusqlite::Error) -> Self { Self { etype: EType::LibSql, message: error.to_string(), } } } impl From for MLErr { fn from(error: toml::ser::Error) -> Self { Self { etype: EType::LibToml, message: error.to_string(), } } } impl From for MLErr { fn from(error: chrono::ParseError) -> Self { Self { etype: EType::LibChrono, message: error.to_string(), } } } impl From for MLErr { fn from(error: std::io::Error) -> Self { Self { etype: EType::IoError, message: error.to_string(), } } } impl From for MLErr { fn from(value: serde_json::error::Error) -> Self { Self { etype: EType::LibJson, message: value.to_string(), } } } impl MLErr { #[must_use] pub fn new(etype: EType, message: &str) -> Self { Self { etype, message: String::from(message), } } }