1use thiserror::Error;
21
22#[derive(Error, Debug)]
24pub enum MxError {
25 #[error("XML serialization error: {0}")]
27 XmlSerialization(String),
28
29 #[error("XML deserialization error: {0}")]
31 XmlDeserialization(String),
32
33 #[error("XML validation error: {0}")]
35 XmlValidation(String),
36
37 #[error("XML error: {0}")]
39 Xml(String),
40
41 #[error("JSON error: {0}")]
43 Json(#[from] serde_json::Error),
44
45 #[error("Validation error: {message}")]
47 Validation {
48 code: u32,
49 message: String,
50 field: Option<String>,
51 path: Option<String>,
52 },
53
54 #[error("Cannot detect message format")]
56 FormatDetection,
57
58 #[error("Unknown message type: {0}")]
60 UnknownMessageType(String),
61
62 #[error("IO error: {0}")]
64 Io(#[from] std::io::Error),
65}
66
67#[derive(Debug, Clone, PartialEq)]
69pub struct ValidationError {
70 pub code: u32,
71 pub message: String,
72 pub field: Option<String>,
73 pub path: Option<String>,
74}
75
76impl ValidationError {
77 pub fn new(code: u32, message: String) -> Self {
78 ValidationError {
79 code,
80 message,
81 field: None,
82 path: None,
83 }
84 }
85
86 pub fn with_field(mut self, field: String) -> Self {
87 self.field = Some(field);
88 self
89 }
90
91 pub fn with_path(mut self, path: String) -> Self {
92 self.path = Some(path);
93 self
94 }
95}
96
97impl From<ValidationError> for MxError {
98 fn from(err: ValidationError) -> Self {
99 MxError::Validation {
100 code: err.code,
101 message: err.message,
102 field: err.field,
103 path: err.path,
104 }
105 }
106}