microrti_encoding/error.rs
1//! Typed errors of the encoding helpers (`EncoderException` and `DecoderException` of the standard APIs).
2
3use microrti_core::{ExceptionKind, RtiError};
4
5/// Reasons why a value cannot be encoded.
6#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
7#[non_exhaustive]
8pub enum EncoderError {
9 /// A variant record has no alternative registered for its active discriminant.
10 #[error("no variant is associated with the active discriminant")]
11 NoVariantForDiscriminant,
12 /// An array, string or octet sequence has more elements than the signed 32-bit element count can hold.
13 #[error("{count} elements do not fit the signed 32-bit element count of an HLA array")]
14 TooManyElements {
15 /// Number of elements of the offending value.
16 count: usize,
17 },
18 /// The alternative of an `HLAextendableVariantRecord` is longer than its 32-bit length field allows.
19 #[error("alternative of {length} octets does not fit the length field of an HLAextendableVariantRecord")]
20 AlternativeTooLong {
21 /// Encoded length of the alternative in octets.
22 length: usize,
23 },
24 /// An element does not have the type its container requires.
25 #[error("type mismatch: {context}")]
26 TypeMismatch {
27 /// What was being checked.
28 context: &'static str,
29 },
30 /// An index is outside a container.
31 #[error("index {index} is out of range for {size} element(s)")]
32 IndexOutOfRange {
33 /// The index that was used.
34 index: usize,
35 /// Number of elements of the container.
36 size: usize,
37 },
38 /// A value is outside the range of its datatype (for example a non-ASCII character).
39 #[error("invalid value for {type_name}: {detail}")]
40 InvalidValue {
41 /// HLA datatype name.
42 type_name: &'static str,
43 /// Human readable explanation.
44 detail: String,
45 },
46}
47
48/// Reasons why a byte sequence cannot be decoded.
49#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
50#[non_exhaustive]
51pub enum DecoderError {
52 /// The input ends before the value is complete.
53 #[error("truncated input: {needed} octet(s) needed at offset {offset}, {remaining} remaining")]
54 Truncated {
55 /// Offset from the start of the outermost value.
56 offset: usize,
57 /// Octets required at that offset.
58 needed: usize,
59 /// Octets actually left.
60 remaining: usize,
61 },
62 /// The octets do not form a legal value of the datatype.
63 #[error("invalid value for {type_name}: {detail}")]
64 InvalidValue {
65 /// HLA datatype name.
66 type_name: &'static str,
67 /// Human readable explanation.
68 detail: String,
69 },
70 /// An element count is negative.
71 #[error("invalid element count {count}")]
72 InvalidCount {
73 /// The decoded count.
74 count: i32,
75 },
76 /// An element count is larger than the number of octets left, so it cannot be honest.
77 #[error("element count {count} exceeds the {remaining} remaining octet(s)")]
78 CountExceedsInput {
79 /// The decoded count.
80 count: usize,
81 /// Octets left after the count.
82 remaining: usize,
83 },
84 /// A value has a different number of elements than its datatype prescribes.
85 #[error("count mismatch for {type_name}: expected {expected}, found {found}")]
86 CountMismatch {
87 /// HLA datatype name.
88 type_name: &'static str,
89 /// Number of elements the datatype requires.
90 expected: usize,
91 /// Number of elements found.
92 found: usize,
93 },
94 /// The discriminant of a variant record selects no known alternative.
95 #[error("unknown discriminant {discriminant} for {type_name}")]
96 UnknownDiscriminant {
97 /// Name of the variant record datatype.
98 type_name: &'static str,
99 /// Debug rendering of the decoded discriminant.
100 discriminant: String,
101 },
102 /// A value is not one of the enumerators of an enumerated datatype.
103 #[error("{value} is not an enumerator of {type_name}")]
104 InvalidEnumerator {
105 /// Name of the enumerated datatype.
106 type_name: &'static str,
107 /// Debug rendering of the decoded value.
108 value: String,
109 },
110 /// The alternative of an `HLAextendableVariantRecord` did not use exactly the announced length.
111 #[error("alternative announces {declared} octet(s) but its datatype uses {consumed}")]
112 LengthMismatch {
113 /// Length announced by the record.
114 declared: usize,
115 /// Octets the alternative datatype consumed.
116 consumed: usize,
117 },
118 /// Octets are left after the value (strict decoding only).
119 #[error("{count} trailing octet(s) after the value")]
120 TrailingBytes {
121 /// Number of unused octets.
122 count: usize,
123 },
124}
125
126impl From<EncoderError> for RtiError {
127 fn from(error: EncoderError) -> Self {
128 RtiError::new(ExceptionKind::CouldNotEncode, error.to_string())
129 }
130}
131
132impl From<DecoderError> for RtiError {
133 fn from(error: DecoderError) -> Self {
134 RtiError::new(ExceptionKind::CouldNotDecode, error.to_string())
135 }
136}