pokemon_tcg_api_client/pokemon_api_client/
error.rs

1use std::{error::Error, fmt};
2
3/// Error for the uniting the different errors.
4#[derive(Debug)]
5pub enum ApiError {
6    Reqwest(reqwest::Error),
7    Deserialize(serde_json::Error),
8    Io(std::io::Error),
9    General(String),
10}
11
12impl fmt::Display for ApiError {
13    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14        match self {
15            ApiError::Reqwest(err) => write!(f, "Reqwest error: {err}"),
16            ApiError::Deserialize(err) => write!(f, "serde_json error: {err}"),
17            ApiError::Io(err) => write!(f, "IO error: {err}"),
18            ApiError::General(s) => write!(f, "General error: {s}"),
19        }
20    }
21}
22
23impl Error for ApiError {
24    fn source(&self) -> Option<&(dyn Error + 'static)> {
25        match self {
26            ApiError::Reqwest(err) => Some(err),
27            ApiError::Deserialize(err) => Some(err),
28            ApiError::Io(err) => Some(err),
29            ApiError::General(_) => None,
30        }
31    }
32}
33
34impl From<std::io::Error> for ApiError {
35    fn from(e: std::io::Error) -> Self {
36        Self::Io(e)
37    }
38}
39impl From<reqwest::Error> for ApiError {
40    fn from(e: reqwest::Error) -> Self {
41        Self::Reqwest(e)
42    }
43}
44impl From<serde_json::error::Error> for ApiError {
45    fn from(e: serde_json::error::Error) -> Self {
46        Self::Deserialize(e)
47    }
48}