xtce_decode/error.rs
1//! Decoding failures.
2//!
3//! Every one of these is a `Result`, never a panic. A decoder fed a corrupt or hostile
4//! packet must keep running: it is typically sitting on a live downlink, and one malformed
5//! frame must not take the process with it.
6
7use xtce_model::{ContainerId, ParamId};
8
9use crate::bits::BitError;
10
11/// Anything that can go wrong decoding a packet.
12#[derive(Clone, Debug, thiserror::Error)]
13#[non_exhaustive]
14pub enum DecodeError {
15 /// A field extends past the end of the packet.
16 #[error("{parameter}: {source}")]
17 Bits {
18 /// Name of the parameter being decoded.
19 parameter: String,
20 /// The underlying bit-cursor failure.
21 #[source]
22 source: BitError,
23 },
24
25 /// The named root container does not exist in the database.
26 #[error("no container named {name:?}")]
27 NoSuchContainer {
28 /// The name that was looked up.
29 name: String,
30 },
31
32 /// The database offers no unambiguous root container and none was named.
33 #[error(
34 "no root container could be chosen automatically ({candidates} candidates); name one explicitly"
35 )]
36 AmbiguousRoot {
37 /// How many containers have no base container.
38 candidates: usize,
39 },
40
41 /// Decoding reached an abstract container whose inheritors all failed their criteria.
42 ///
43 /// This is the reference implementation's `UnrecognizedPacketTypeError`: the packet is
44 /// of a type the definition does not describe.
45 #[error("packet not recognised: abstract container {container} has no matching inheritor")]
46 UnrecognizedPacket {
47 /// Name of the abstract container that ran out of options.
48 container: String,
49 /// Names of the inheritors that were considered.
50 candidates: Vec<String>,
51 },
52
53 /// More than one inheritor matched, so the packet type is ambiguous.
54 #[error("ambiguous packet type: {} inheritors of {container} all match", .candidates.len())]
55 AmbiguousPacket {
56 /// Name of the container being specialised.
57 container: String,
58 /// Names of the inheritors that matched.
59 candidates: Vec<String>,
60 },
61
62 /// A criterion or dynamic size referenced a parameter that has not been decoded yet.
63 ///
64 /// XTCE references are positional: a field can only depend on a field that precedes it.
65 #[error("{context} references parameter {parameter:?}, which has not been decoded yet")]
66 ParameterNotYetDecoded {
67 /// Where the reference appeared.
68 context: &'static str,
69 /// The parameter that was referenced.
70 parameter: String,
71 },
72
73 /// A comparison's literal cannot be interpreted as the type of the value it is compared
74 /// against.
75 #[error("cannot compare {parameter} ({value_kind}) against literal {literal:?}")]
76 IncomparableValue {
77 /// Name of the referenced parameter.
78 parameter: String,
79 /// What kind of value it held.
80 value_kind: &'static str,
81 /// The literal as written in the definition.
82 literal: String,
83 },
84
85 /// The bytes are not valid text in the declared character set.
86 #[error("{parameter}: {bytes} byte(s) are not valid {charset}")]
87 InvalidText {
88 /// Name of the parameter being decoded.
89 parameter: String,
90 /// Character set that was expected.
91 charset: &'static str,
92 /// Length of the offending buffer.
93 bytes: usize,
94 },
95
96 /// A string declares a termination character that does not occur in its buffer.
97 #[error("{parameter}: termination character not found in the {bytes}-byte string buffer")]
98 UnterminatedString {
99 /// Name of the parameter being decoded.
100 parameter: String,
101 /// Length of the buffer that was searched.
102 bytes: usize,
103 },
104
105 /// A raw value is outside an enumeration's defined values.
106 #[error("{parameter}: raw value {value} is not in the enumeration")]
107 UnknownEnumeration {
108 /// Name of the parameter being decoded.
109 parameter: String,
110 /// The raw value that was looked up.
111 value: i128,
112 },
113
114 /// A calibrator rejected its input.
115 #[error("{parameter}: {reason}")]
116 Calibration {
117 /// Name of the parameter being decoded.
118 parameter: String,
119 /// Why calibration failed.
120 reason: String,
121 },
122
123 /// A dynamic size resolved to something unusable.
124 #[error("{parameter}: computed field size {bits} bits is not usable")]
125 BadFieldSize {
126 /// Name of the parameter being decoded.
127 parameter: String,
128 /// The size that was computed.
129 bits: i64,
130 },
131
132 /// A `DiscreteLookupList` had no matching entry.
133 #[error("{parameter}: no discrete lookup matched")]
134 NoDiscreteLookupMatch {
135 /// Name of the parameter being decoded.
136 parameter: String,
137 },
138
139 /// The definition uses a construct this crate models but cannot decode.
140 ///
141 /// This is the promise `SUPPORTED.md` makes: out-of-scope constructs load without
142 /// complaint and fail here, at the exact point a value depends on them.
143 #[error("unsupported XTCE construct <{element}> reached while decoding {context}")]
144 Unsupported {
145 /// The element that is out of scope.
146 element: String,
147 /// What was being decoded when it was reached.
148 context: String,
149 },
150
151 /// The database is internally inconsistent — an index does not resolve.
152 #[error("internal: {what} index does not resolve")]
153 DanglingIndex {
154 /// Which kind of index.
155 what: &'static str,
156 },
157}
158
159impl DecodeError {
160 pub(crate) fn dangling_parameter(_id: ParamId) -> Self {
161 Self::DanglingIndex { what: "parameter" }
162 }
163
164 pub(crate) fn dangling_container(_id: ContainerId) -> Self {
165 Self::DanglingIndex { what: "container" }
166 }
167}