1use reqwest::StatusCode;
2
3pub type Result<T> = std::result::Result<T, Error>;
4
5pub enum Error {
6 StdError(std::io::Error),
7 RequestError(reqwest::Error),
8 DeError(quick_xml::DeError),
9 ResponseError(StatusCode),
10 Utf8Error(std::str::Utf8Error),
11 UrlError(url::ParseError),
12 InvalidResponse,
13 InvalidData(String),
14}
15
16impl Error {
17 pub fn is_std_err(&self) -> bool {
18 matches!(self, Self::StdError(_))
19 }
20
21 pub fn is_request_err(&self) -> bool {
22 matches!(self, Self::RequestError(_))
23 }
24 pub fn is_de_err(&self) -> bool {
25 matches!(self, Self::DeError(_))
26 }
27 pub fn is_response_err(&self) -> bool {
28 matches!(self, Self::ResponseError(_))
29 }
30
31 pub fn is_invalid_utf8_err(&self) -> bool {
32 matches!(self, Self::Utf8Error(_))
33 }
34}
35
36impl From<std::io::Error> for Error {
37 fn from(value: std::io::Error) -> Self {
38 Self::StdError(value)
39 }
40}
41impl From<reqwest::Error> for Error {
42 fn from(value: reqwest::Error) -> Self {
43 Self::RequestError(value)
44 }
45}
46impl From<quick_xml::DeError> for Error {
47 fn from(value: quick_xml::DeError) -> Self {
48 Self::DeError(value)
49 }
50}
51impl From<std::str::Utf8Error> for Error {
52 fn from(value: std::str::Utf8Error) -> Self {
53 Self::Utf8Error(value)
54 }
55}
56
57impl From<url::ParseError> for Error {
58 fn from(value: url::ParseError) -> Self {
59 Self::UrlError(value)
60 }
61}
62
63impl From<StatusCode> for Error {
64 fn from(value: StatusCode) -> Self {
65 Self::ResponseError(value)
66 }
67}
68
69impl std::fmt::Debug for Error {
70 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71 match self {
72 Self::StdError(arg0) => arg0.fmt(f),
73 Self::RequestError(arg0) => arg0.fmt(f),
74 Self::DeError(arg0) => arg0.fmt(f),
75 Self::ResponseError(arg) => arg.fmt(f),
76 Self::Utf8Error(arg) => arg.fmt(f),
77 Self::UrlError(parse_error) => parse_error.fmt(f),
78 Self::InvalidResponse => f.write_str("InvalidResponse"),
79 Self::InvalidData(data) => f.write_str(&format!("Invalid data: {}", data)),
80 }
81 }
82}
83
84impl std::fmt::Display for Error {
85 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86 match self {
87 Self::StdError(arg0) => arg0.fmt(f),
88 Self::RequestError(arg0) => arg0.fmt(f),
89 Self::DeError(arg0) => arg0.fmt(f),
90 Self::ResponseError(arg) => arg.fmt(f),
91 Self::Utf8Error(arg) => arg.fmt(f),
92 Self::UrlError(arg) => arg.fmt(f),
93 Self::InvalidResponse => f.write_str("Invalid response received"),
94 Self::InvalidData(data) => f.write_str(data),
95 }
96 }
97}
98impl std::error::Error for Error {}