1pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug, thiserror::Error)]
9#[non_exhaustive]
10pub enum Error {
11 #[error("XML error: {0}")]
13 Xml(String),
14
15 #[error("I/O error: {0}")]
17 Io(#[from] std::io::Error),
18
19 #[error("unexpected end of document")]
21 Eof,
22
23 #[error("expected element <{expected}>, found <{found}>")]
25 UnexpectedElement {
26 expected: String,
28 found: String,
30 },
31
32 #[error("missing required {kind} `{name}` in <{parent}>")]
34 MissingRequired {
35 kind: &'static str,
37 name: String,
39 parent: String,
41 },
42
43 #[error("cannot parse `{value}` as {ty}: {reason}")]
45 ParseValue {
46 value: String,
48 ty: &'static str,
50 reason: String,
52 },
53
54 #[error("unknown xsi:type `{value}` for abstract type `{ty}`")]
57 UnknownXsiType {
58 value: String,
60 ty: &'static str,
62 },
63
64 #[error("type `{ty}` has no field `{field}`")]
66 UnknownField {
67 ty: &'static str,
69 field: String,
71 },
72
73 #[error("invalid path `{path}`: {reason}")]
75 InvalidPath {
76 path: String,
78 reason: String,
80 },
81
82 #[error("type mismatch at `{path}`: expected {expected}, found {found}")]
84 TypeMismatch {
85 path: String,
87 expected: String,
89 found: String,
91 },
92
93 #[error("unknown type `{0}` (not registered)")]
95 UnknownType(String),
96
97 #[error("{0}")]
102 VersionAbsent(String),
103
104 #[error("{0}")]
106 Other(String),
107}
108
109impl Error {
110 pub fn other(msg: impl Into<String>) -> Self {
112 Error::Other(msg.into())
113 }
114
115 pub fn parse(value: impl Into<String>, ty: &'static str, reason: impl Into<String>) -> Self {
117 Error::ParseValue {
118 value: value.into(),
119 ty,
120 reason: reason.into(),
121 }
122 }
123}
124
125impl From<quick_xml::Error> for Error {
126 fn from(e: quick_xml::Error) -> Self {
127 Error::Xml(e.to_string())
128 }
129}
130
131impl From<quick_xml::events::attributes::AttrError> for Error {
132 fn from(e: quick_xml::events::attributes::AttrError) -> Self {
133 Error::Xml(e.to_string())
134 }
135}
136
137impl From<quick_xml::encoding::EncodingError> for Error {
138 fn from(e: quick_xml::encoding::EncodingError) -> Self {
139 Error::Xml(e.to_string())
140 }
141}
142
143impl From<quick_xml::escape::EscapeError> for Error {
144 fn from(e: quick_xml::escape::EscapeError) -> Self {
145 Error::Xml(e.to_string())
146 }
147}
148
149impl From<std::str::Utf8Error> for Error {
150 fn from(e: std::str::Utf8Error) -> Self {
151 Error::Xml(format!("invalid UTF-8: {e}"))
152 }
153}
154
155impl From<std::string::FromUtf8Error> for Error {
156 fn from(e: std::string::FromUtf8Error) -> Self {
157 Error::Xml(format!("invalid UTF-8: {e}"))
158 }
159}