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
use std::num::{ParseFloatError, ParseIntError};
use std::string::FromUtf8Error;
use base64::DecodeError;
use quick_xml::Error as XmlError;
use serde::{Deserialize, Serialize};
use thiserror::Error as ThisError;
#[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),
#[error("serde decoding error: {0}")]
DecodeError(String),
#[error("serde encoding error: {0}")]
EncodeError(String),
}
impl serde::de::Error for Error {
fn custom<T>(msg: T) -> Self
where
T: std::fmt::Display,
{
Error::DecodeError(msg.to_string())
}
}
impl serde::ser::Error for Error {
fn custom<T>(msg: T) -> Self
where
T: std::fmt::Display,
{
Error::EncodeError(msg.to_string())
}
}
#[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("key must be convertable to a string")]
KeyMustBeString,
#[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> = std::result::Result<T, Error>;
#[derive(ThisError, Deserialize, Serialize, Debug, PartialEq, Eq)]
#[error("{fault_string} ({fault_code})")]
#[serde(rename_all = "camelCase")]
pub struct Fault {
pub fault_code: i32,
pub fault_string: String,
}
#[cfg(test)]
mod tests {
use super::*;
use std::error;
use crate::Value;
#[test]
fn fault_roundtrip() {
let input = Fault {
fault_code: -123456,
fault_string: "The Bald Lazy House Jumps Over The Hyperactive Kitten".to_string(),
};
let value: Value = input.serialize(crate::value::Serializer::new()).unwrap();
let deserializer = crate::value::Deserializer::from_value(value);
let new_input: Fault = Fault::deserialize(deserializer).unwrap();
assert_eq!(new_input, 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>();
}
}