xtce_model/error.rs
1//! Errors produced while reading an XTCE document.
2
3use std::fmt;
4use std::path::PathBuf;
5
6/// Anything that can go wrong turning an XTCE document into an [`crate::XtceDb`].
7#[derive(Debug, thiserror::Error)]
8#[non_exhaustive]
9pub enum XtceError {
10 /// The file could not be read.
11 #[error("failed to read {path}")]
12 Io {
13 /// Path that could not be read.
14 path: PathBuf,
15 /// Underlying I/O failure.
16 #[source]
17 source: std::io::Error,
18 },
19
20 /// The document is not well-formed XML.
21 #[error(transparent)]
22 Parse(#[from] ParseError),
23
24 /// The document is well-formed but not an XTCE `SpaceSystem`.
25 #[error("expected a <SpaceSystem> root element, found <{found}>")]
26 NotXtce {
27 /// The root element actually present.
28 found: String,
29 },
30
31 /// A required element or attribute is absent.
32 #[error("missing {what} at {path}")]
33 Missing {
34 /// What was expected, e.g. `"name attribute"`.
35 what: &'static str,
36 /// Element path within the document.
37 path: String,
38 },
39
40 /// A value is present but cannot be interpreted.
41 #[error("invalid {what} {value:?} at {path}: {reason}")]
42 Invalid {
43 /// What was being interpreted, e.g. `"sizeInBits"`.
44 what: &'static str,
45 /// The offending text.
46 value: String,
47 /// Element path within the document.
48 path: String,
49 /// Why it could not be used.
50 reason: String,
51 },
52
53 /// A name reference does not resolve to a definition.
54 #[error("unresolved {kind} reference {reference:?} at {path}")]
55 UnresolvedReference {
56 /// Which namespace was searched.
57 kind: RefKind,
58 /// The reference text as written.
59 reference: String,
60 /// Element path within the document.
61 path: String,
62 },
63
64 /// Two definitions share a name in a namespace that requires uniqueness.
65 #[error("duplicate {kind} definition {name:?} at {path}")]
66 DuplicateDefinition {
67 /// Which namespace the clash is in.
68 kind: RefKind,
69 /// The duplicated name.
70 name: String,
71 /// Element path of the second definition.
72 path: String,
73 },
74
75 /// Container inheritance forms a cycle.
76 ///
77 /// Detected during resolution rather than during decoding, so a malformed database
78 /// fails at load time instead of overflowing the stack on the first packet.
79 #[error("container inheritance cycle: {}", .chain.join(" -> "))]
80 InheritanceCycle {
81 /// Container names along the cycle, in order.
82 chain: Vec<String>,
83 },
84
85 /// An XTCE construct outside this crate's declared scope.
86 ///
87 /// This is only raised for constructs that cannot be *represented*. Constructs that can
88 /// be represented but not decoded are recorded in the IR instead and reported by
89 /// [`crate::XtceDb::unsupported`], so loading a real mission database never fails just
90 /// because part of it is out of scope.
91 #[error("unsupported XTCE construct <{element}> at {path}")]
92 Unsupported {
93 /// The element that is out of scope.
94 element: String,
95 /// Element path within the document.
96 path: String,
97 },
98
99 /// An array entry could not be turned into one parameter per element.
100 ///
101 /// Loading fails rather than the array being dropped: an entry that does not expand
102 /// leaves every field after it at the wrong offset, and a container decoded at the wrong
103 /// offsets is worse than one that refuses.
104 #[error("cannot expand the array at {path}: {reason}")]
105 ArrayNotExpanded {
106 /// Why the expansion could not be carried out.
107 reason: String,
108 /// Element path within the document.
109 path: String,
110 },
111}
112
113/// Which XTCE namespace a reference is resolved in.
114#[derive(Clone, Copy, PartialEq, Eq, Debug)]
115pub enum RefKind {
116 /// `parameterRef`, `parameterInstanceRef`.
117 Parameter,
118 /// `parameterTypeRef`.
119 ParameterType,
120 /// `argumentTypeRef`.
121 ///
122 /// A namespace of its own, because the schema's uniqueness keys make it one: an argument
123 /// type must not clash with a *command*'s parameter types, and may clash with the
124 /// telemetry ones.
125 ArgumentType,
126 /// `containerRef`, `baseContainer`.
127 Container,
128 /// `metaCommandRef`.
129 MetaCommand,
130}
131
132impl fmt::Display for RefKind {
133 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134 f.write_str(match self {
135 Self::Parameter => "parameter",
136 Self::ParameterType => "parameter type",
137 Self::ArgumentType => "argument type",
138 Self::Container => "container",
139 Self::MetaCommand => "telecommand",
140 })
141 }
142}
143
144/// A low-level XML syntax failure, with the byte offset where it was detected.
145#[derive(Debug, thiserror::Error)]
146pub struct ParseError {
147 kind: ParseErrorKind,
148 offset: Option<u64>,
149}
150
151impl ParseError {
152 pub(crate) fn new(kind: ParseErrorKind) -> Self {
153 Self { kind, offset: None }
154 }
155
156 pub(crate) fn at_offset(offset: u64, source: impl Into<ParseErrorKind>) -> Self {
157 Self {
158 kind: source.into(),
159 offset: Some(offset),
160 }
161 }
162
163 /// Byte offset in the input where the failure was detected, when known.
164 #[must_use]
165 pub fn offset(&self) -> Option<u64> {
166 self.offset
167 }
168}
169
170impl fmt::Display for ParseError {
171 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172 match self.offset {
173 Some(offset) => write!(f, "XML error at byte {offset}: {}", self.kind),
174 None => write!(f, "XML error: {}", self.kind),
175 }
176 }
177}
178
179/// The underlying cause of a [`ParseError`].
180#[derive(Debug, thiserror::Error)]
181pub(crate) enum ParseErrorKind {
182 #[error(transparent)]
183 Xml(#[from] quick_xml::Error),
184 #[error("document contains no elements")]
185 EmptyDocument,
186}
187
188impl From<quick_xml::encoding::EncodingError> for ParseErrorKind {
189 fn from(value: quick_xml::encoding::EncodingError) -> Self {
190 Self::Xml(quick_xml::Error::Encoding(value))
191 }
192}
193
194impl From<quick_xml::events::attributes::AttrError> for ParseErrorKind {
195 fn from(value: quick_xml::events::attributes::AttrError) -> Self {
196 Self::Xml(quick_xml::Error::InvalidAttr(value))
197 }
198}