1use std::io::Error as IoError;
4use std::num::ParseFloatError;
5use std::num::ParseIntError;
6
7use quick_xml::Error as XmlError;
8use thiserror::Error;
9use ureq::Error as UreqError;
10
11#[derive(Debug, Error, Clone, PartialEq, Eq)]
12pub enum ApiError {
14 #[error("bad request: {0}")]
15 BadRequest(String),
17 #[error("not found: {0}")]
18 NotFound(String),
20 #[error("not allowed: {0}")]
21 NotAllowed(String),
23 #[error("timeout: {0}")]
24 Timeout(String),
26 #[error("server busy: {0}")]
27 ServerBusy(String),
29 #[error("unimplemented!(): {0}")]
30 Unimplemented(String),
32 #[error("server error: {0}")]
33 ServerError(String),
35 #[error("unknown error: {0}")]
36 Unknown(String),
38}
39
40impl From<crate::model::rest::Fault> for ApiError {
41 fn from(fault: crate::model::rest::Fault) -> Self {
42 match fault.code.as_str() {
43 "PUGREST.BadRequest" => ApiError::BadRequest(fault.message),
44 "PUGREST.NotFound" => ApiError::NotFound(fault.message),
45 "PUGREST.NotAllowed" => ApiError::NotAllowed(fault.message),
46 "PUGREST.Timeout" => ApiError::Timeout(fault.message),
47 "PUGREST.ServerBusy" => ApiError::ServerBusy(fault.message),
48 "PUGREST.Unimplemented" => ApiError::Unimplemented(fault.message),
49 "PUGREST.ServerError" => ApiError::ServerError(fault.message),
50 _ => ApiError::Unknown(fault.message),
51 }
52 }
53}
54
55#[derive(Debug, Error, Clone, PartialEq, Eq)]
58pub enum ParseError {
60 #[error(transparent)]
61 Int(#[from] ParseIntError),
62 #[error(transparent)]
63 Float(#[from] ParseFloatError),
64}
65
66#[derive(Debug, Error)]
69pub enum Error {
73 #[error(transparent)]
74 Api(#[from] ApiError),
76 #[error(transparent)]
77 Request(#[from] UreqError),
79 #[error(transparent)]
80 Xml(#[from] XmlError),
87 #[error(transparent)]
88 Parse(#[from] ParseError),
90}
91
92impl From<IoError> for Error {
93 fn from(e: IoError) -> Self {
94 Self::from(XmlError::Io(e))
95 }
96}
97
98impl From<ParseIntError> for Error {
99 fn from(e: ParseIntError) -> Self {
100 Self::Parse(ParseError::Int(e))
101 }
102}
103
104impl From<ParseFloatError> for Error {
105 fn from(e: ParseFloatError) -> Self {
106 Self::Parse(ParseError::Float(e))
107 }
108}
109
110pub type Result<T> = std::result::Result<T, Error>;