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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::num::{ParseFloatError, ParseIntError};
use std::result;
use std::string::FromUtf8Error;
use base64::DecodeError;
use quick_xml::Error as XmlError;
use thiserror::Error as ThisError;
use crate::Value;
#[derive(ThisError, Debug)]
pub enum Error {
#[error("parse error: {0}")]
ParseError(#[from] ParseError),
#[error("encoding error: {0}")]
EncodingError(#[from] EncodingError),
#[error("server fault: {0}")]
Fault(#[from] Fault),
}
#[derive(ThisError, Debug)]
pub enum ParseError {
#[error("malformed XML: {0}")]
XmlError(#[from] XmlError),
#[error("malformed XML: {0}")]
ParseIntError(#[from] ParseIntError),
#[error("malformed XML: {0}")]
ParseFloatError(#[from] ParseFloatError),
#[error("malformed XML: {0}")]
Base64DecodeError(#[from] DecodeError),
#[error("malformed XML: {0}")]
DateTimeDecodeError(String),
#[error("malformed XML: invalid boolean value: {0}")]
BooleanDecodeError(String),
#[error("malformed UTF-8: {0}")]
Utf8Error(#[from] FromUtf8Error),
#[error("unexpected tag: found {0}, expected {1}")]
UnexpectedTag(String, String),
#[error("unexpected error: {0}, expected tag {1}")]
UnexpectedError(anyhow::Error, String),
#[error("unexpected event: expected tag {0}")]
UnexpectedEvent(String),
#[error("unexpected EOF: expected tag {0}")]
UnexpectedEOF(String),
#[error("tag not found: {0}")]
TagNotFound(String),
#[error("fault: {0}")]
ParseFaultError(String),
}
#[derive(ThisError, Debug)]
pub enum EncodingError {
#[error("malformed UTF-8: {0}")]
Utf8Error(#[from] FromUtf8Error),
#[error("XML error: {0}")]
XmlError(#[from] XmlError),
}
pub type Result<T> = result::Result<T, Error>;
#[derive(ThisError, Debug, PartialEq, Eq)]
#[error("{fault_string} ({fault_code})")]
pub struct Fault {
pub fault_code: i32,
pub fault_string: String,
}
impl TryFrom<Value> for Fault {
type Error = ParseError;
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
match value {
Value::Struct(ref map) => {
match (map.get("faultCode"), map.get("faultString")) {
(Some(&Value::Int(fault_code)), Some(&Value::String(ref fault_string))) => {
if map.len() != 2 {
Err(ParseError::ParseFaultError(
"extra fields returned in fault".into(),
))
} else {
Ok(Fault {
fault_code,
fault_string: fault_string.to_string(),
})
}
}
_ => Err(ParseError::ParseFaultError(
"missing either faultCode or faultString".into(),
)),
}
}
_ => Err(ParseError::ParseFaultError("expected struct".into())),
}
}
}
impl From<&Fault> for Value {
fn from(other: &Fault) -> Self {
let mut map = BTreeMap::new();
map.insert("faultCode".to_string(), Value::from(other.fault_code));
map.insert(
"faultString".to_string(),
Value::from(other.fault_string.clone()),
);
Value::Struct(map)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::error;
#[test]
fn fault_roundtrip() {
let input = Fault {
fault_code: -123456,
fault_string: "The Bald Lazy House Jumps Over The Hyperactive Kitten".to_string(),
};
assert_eq!(Fault::try_from(Value::from(&input)).unwrap(), input);
}
#[test]
fn error_impls_error() {
fn assert_error<T: error::Error>() {}
assert_error::<Error>();
}
#[test]
fn error_is_send_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<Error>();
}
}