summaryrefslogblamecommitdiff
path: root/src/error.rs
blob: 652fa0cf2a019a8598c5e528c320459f15415dd9 (plain) (tree)
1
2
3
4
5
6
7
8
              
                       
 
                                   
 
                             

                  


                    
                             
                
                  
                       
                
            
             
            

           
              
            
                 
            
          

 
                                  




                                   
                             

                                                          
                                     

                                                                      
                                          

                                                       










                                                                            



         
                                     
                                            
              
                                 

                                       


     
                                      
                                             
              
                                  

                                       


     
                                      
                                             
              
                                 

                                       


     
                                       
                                              
              
                                  

                                       


     
                                         
                                                
              
                                    

                                       


     
                                     
                                            
              
                                  

                                       


     
                                               
                                                      
              
                                  

                                       
     

 

                                                                 



                                           

     
use core::fmt;
use serde::Deserialize;

pub type MLE<T> = Result<T, MLErr>;

#[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<reqwest::Error> for MLErr {
    fn from(error: reqwest::Error) -> Self {
        Self {
            etype: EType::LibReq,
            message: error.to_string(),
        }
    }
}

impl From<toml::de::Error> for MLErr {
    fn from(error: toml::de::Error) -> Self {
        Self {
            etype: EType::LibToml,
            message: error.to_string(),
        }
    }
}

impl From<rusqlite::Error> for MLErr {
    fn from(error: rusqlite::Error) -> Self {
        Self {
            etype: EType::LibSql,
            message: error.to_string(),
        }
    }
}

impl From<toml::ser::Error> for MLErr {
    fn from(error: toml::ser::Error) -> Self {
        Self {
            etype: EType::LibToml,
            message: error.to_string(),
        }
    }
}

impl From<chrono::ParseError> for MLErr {
    fn from(error: chrono::ParseError) -> Self {
        Self {
            etype: EType::LibChrono,
            message: error.to_string(),
        }
    }
}

impl From<std::io::Error> for MLErr {
    fn from(error: std::io::Error) -> Self {
        Self {
            etype: EType::IoError,
            message: error.to_string(),
        }
    }
}

impl From<serde_json::error::Error> 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),
        }
    }
}