ddex_parser/
error.rs

1use std::fmt;
2
3pub type Result<T> = std::result::Result<T, ParseError>;
4
5#[derive(Debug, Clone)]
6pub enum ParseError {
7    MissingField(String),
8    InvalidValue { field: String, value: String },
9    XmlError(String),
10    StreamError(StreamError),
11    InvalidUtf8 { message: String },
12    SimpleXmlError(String),
13    ConversionError { from: String, to: String, message: String },
14    IoError(String),
15    Timeout { message: String },
16    DepthLimitExceeded { depth: usize, limit: usize },
17    SecurityViolation { message: String },
18    MalformedXml { message: String, position: usize },
19    MismatchedTags { expected: String, found: String, position: usize },
20    UnexpectedClosingTag { tag: String, position: usize },
21    InvalidAttribute { message: String, position: usize },
22    UnclosedTags { tags: Vec<String>, position: usize },
23}
24
25#[derive(Debug, Clone)]
26pub enum StreamError {
27    MissingReleaseReference,
28    MissingResourceReference,
29    MissingDealReference,
30    MissingPartyReference,
31    IncompleteData(String),
32}
33
34impl fmt::Display for ParseError {
35    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36        match self {
37            ParseError::MissingField(field) => {
38                write!(f, "Required DDEX field missing: {}", field)
39            }
40            ParseError::InvalidValue { field, value } => {
41                write!(f, "Invalid value '{}' for field '{}'", value, field)
42            }
43            ParseError::XmlError(msg) => write!(f, "XML parsing error: {}", msg),
44            ParseError::StreamError(e) => write!(f, "Streaming error: {:?}", e),
45            ParseError::InvalidUtf8 { message } => write!(f, "UTF-8 error: {}", message),
46            ParseError::SimpleXmlError(msg) => write!(f, "Simple XML error: {}", msg),
47            ParseError::ConversionError { from, to, message } => {
48                write!(f, "Conversion error from {} to {}: {}", from, to, message)
49            }
50            ParseError::IoError(msg) => write!(f, "IO error: {}", msg),
51            ParseError::Timeout { message } => write!(f, "Timeout: {}", message),
52            ParseError::DepthLimitExceeded { depth, limit } => write!(f, "Depth limit exceeded: {} > {}", depth, limit),
53            ParseError::SecurityViolation { message } => write!(f, "Security violation: {}", message),
54            ParseError::MalformedXml { message, position } => write!(f, "Malformed XML at position {}: {}", position, message),
55            ParseError::MismatchedTags { expected, found, position } => write!(f, "Mismatched tags at position {}: expected '{}', found '{}'", position, expected, found),
56            ParseError::UnexpectedClosingTag { tag, position } => write!(f, "Unexpected closing tag '{}' at position {}", tag, position),
57            ParseError::InvalidAttribute { message, position } => write!(f, "Invalid attribute at position {}: {}", position, message),
58            ParseError::UnclosedTags { tags, position } => write!(f, "Unclosed tags at position {}: {:?}", position, tags),
59        }
60    }
61}
62
63impl std::error::Error for ParseError {}
64
65// From implementations for error conversion
66impl From<std::io::Error> for ParseError {
67    fn from(err: std::io::Error) -> Self {
68        ParseError::IoError(err.to_string())
69    }
70}
71
72impl From<std::str::Utf8Error> for ParseError {
73    fn from(err: std::str::Utf8Error) -> Self {
74        ParseError::InvalidUtf8 { message: err.to_string() }
75    }
76}
77
78impl From<quick_xml::Error> for ParseError {
79    fn from(err: quick_xml::Error) -> Self {
80        ParseError::XmlError(err.to_string())
81    }
82}
83
84impl From<quick_xml::events::attributes::AttrError> for ParseError {
85    fn from(err: quick_xml::events::attributes::AttrError) -> Self {
86        ParseError::XmlError(format!("Attribute parsing error: {}", err))
87    }
88}
89
90impl From<String> for ParseError {
91    fn from(err: String) -> Self {
92        ParseError::SimpleXmlError(err)
93    }
94}
95