ccdata_api/
error.rs

1// Error types
2use std::{error::Error as ErrorTrait, env::VarError};
3use dotenv::Error as DotenvError;
4use serde_json::Error as SerdeJSONError;
5use reqwest::Error as ReqwestError;
6// Dependencies
7use std::{fmt::Display, convert::From};
8
9
10#[derive(Debug)]
11pub enum Error {
12    NoAPIKey,
13    // Std errors
14    VarError(VarError),
15    // Dotenv errors
16    DotenvError(DotenvError),
17    // Serde JSON errors
18    SerdeJSONError(SerdeJSONError),
19    // Reqwest errors
20    ReqwestError(ReqwestError),
21}
22
23impl Display for Error {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        match self {
26            Error::NoAPIKey => write!(f, "No API Key: No API key is defined."),
27            // Std errors
28            Error::VarError(e) => write!(f, "Var Error: {}", e.to_string()),
29            // Dotenv errors
30            Error::DotenvError(e) => write!(f, "Dotenv Error: {}", e.to_string()),
31            // Serde JSON errors
32            Error::SerdeJSONError(e) => write!(f, "Serde JSON Error: {}", e.to_string()), 
33            // Reqwest errors
34            Error::ReqwestError(e) => write!(f, "Reqwest Error: {}", e.to_string()),
35        }
36    }
37}
38
39impl ErrorTrait for Error {}
40
41impl From<VarError> for Error {
42    fn from(value: VarError) -> Self {
43        Error::VarError(value)
44    }
45}
46
47impl From<DotenvError> for Error {
48    fn from(value: DotenvError) -> Self {
49        Error::DotenvError(value)
50    }
51}
52
53impl From<SerdeJSONError> for Error {
54    fn from(value: SerdeJSONError) -> Self {
55        Error::SerdeJSONError(value)
56    }
57}
58
59impl From<ReqwestError> for Error {
60    fn from(value: ReqwestError) -> Self {
61        Error::ReqwestError(value)
62    }
63}