1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! The error type for this library.

use http::header::ToStrError;
use reqwest::Error as ReqwestError;
use serde_json::Error as SerdeError;
use std::error::Error as StdError;
use std::num::ParseFloatError;
use std::num::ParseIntError;
use std::{fmt, fmt::Display};

/// The generic error type used for handling errors within this library.
#[derive(Debug)]
pub enum Error {
    /// Any other error which couldn't be represented well otherwise.
    Custom(String),
    /// A header is missing.
    MissingHeader(String),
    /// An error occured during parsing a float.
    ParseFloatError(ParseFloatError),
    /// An error occured during parsing an integer.
    ParseIntError(ParseIntError),
    /// Reqwest errors.
    Reqwest(ReqwestError),
    /// Serde errors.
    Serde(SerdeError),
    /// I wish there was a better way to do this.
    ToStrError(ToStrError),
}

impl StdError for Error {}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let reason = match self {
            Error::Custom(custom_e) => format!("Error: {:?}", custom_e),
            Error::MissingHeader(header_e) => format!("Missing Header: {:?}", header_e),
            Error::ParseFloatError(parsefloat_e) => format!("ParseFloat Error: {:?}", parsefloat_e),
            Error::ParseIntError(parseint_e) => format!("ParseInt Error: {:?}", parseint_e),
            Error::Reqwest(http_e) => format!("Reqwest Error: {:?}", http_e),
            Error::Serde(json_e) => format!("Serde Error: {:?}", json_e),
            Error::ToStrError(tostr_e) => format!("ToStr Error: {:?}", tostr_e),
        };
        f.write_str(&reason)
    }
}

impl From<ParseIntError> for Error {
    fn from(e: ParseIntError) -> Error {
        Error::ParseIntError(e)
    }
}

impl From<ParseFloatError> for Error {
    fn from(e: ParseFloatError) -> Error {
        Error::ParseFloatError(e)
    }
}

impl From<ReqwestError> for Error {
    fn from(e: ReqwestError) -> Error {
        Error::Reqwest(e)
    }
}

impl From<SerdeError> for Error {
    fn from(e: SerdeError) -> Error {
        Error::Serde(e)
    }
}

impl From<String> for Error {
    fn from(e: String) -> Error {
        Error::Custom(e)
    }
}

impl From<&str> for Error {
    fn from(e: &str) -> Error {
        Error::Custom(e.to_string())
    }
}

impl From<ToStrError> for Error {
    fn from(e: ToStrError) -> Error {
        Error::ToStrError(e)
    }
}