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