use thiserror::Error;
#[derive(Error, Debug)]
pub enum MxError {
#[error("XML serialization error: {0}")]
XmlSerialization(String),
#[error("XML deserialization error: {0}")]
XmlDeserialization(String),
#[error("XML validation error: {0}")]
XmlValidation(String),
#[error("XML error: {0}")]
Xml(String),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("Validation error: {message}")]
Validation {
code: u32,
message: String,
field: Option<String>,
path: Option<String>,
},
#[error("Cannot detect message format")]
FormatDetection,
#[error("Unknown message type: {0}")]
UnknownMessageType(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
}
#[derive(Debug, Clone, PartialEq)]
pub struct ValidationError {
pub code: u32,
pub message: String,
pub field: Option<String>,
pub path: Option<String>,
}
impl ValidationError {
pub fn new(code: u32, message: String) -> Self {
ValidationError {
code,
message,
field: None,
path: None,
}
}
pub fn with_field(mut self, field: String) -> Self {
self.field = Some(field);
self
}
pub fn with_path(mut self, path: String) -> Self {
self.path = Some(path);
self
}
}
impl From<ValidationError> for MxError {
fn from(err: ValidationError) -> Self {
MxError::Validation {
code: err.code,
message: err.message,
field: err.field,
path: err.path,
}
}
}