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
use thiserror::Error;
use crate::error::DxrError;
use crate::traits::TryFromValue;
use crate::values::FaultResponse;
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[error("Server Fault {}: {}", .code, .string)]
pub struct Fault {
code: i32,
string: String,
}
impl Fault {
pub fn new(code: i32, string: String) -> Fault {
Fault { code, string }
}
pub fn code(&self) -> i32 {
self.code
}
pub fn string(&self) -> &str {
self.string.as_str()
}
}
impl TryFrom<FaultResponse> for Fault {
type Error = DxrError;
fn try_from(value: FaultResponse) -> Result<Self, Self::Error> {
let mut members = value.members().iter();
let (first, second) = match (members.next(), members.next(), members.next()) {
(Some(first), Some(second), None) => (first, second),
_ => return Err(DxrError::parameter_mismatch(members.len(), 2)),
};
let fault_code = if first.name() == "faultCode" {
first.inner()
} else {
return Err(DxrError::missing_field("fault", "faultCode"));
};
let fault_string = if second.name() == "faultString" {
second.inner()
} else {
return Err(DxrError::missing_field("fault", "faultString"));
};
let code: i32 = i32::try_from_value(fault_code)?;
let string: String = String::try_from_value(fault_string)?;
Ok(Fault::new(code, string))
}
}