microrti_encoding/extendable.rs
1//! Wire layout of `HLAextendableVariantRecord`, the variant record encoding introduced in 2025 so that
2//! FOM modules can add alternatives to an existing datatype.
3//!
4//! **This layout is an assumption.** IEEE 1516.2-2025, which defines it, was not available when this
5//! crate was written. The API files and public descriptions only establish the intent: the encoding
6//! states the alignment and the length of the alternative explicitly, so that a receiver can skip an
7//! alternative it does not know, and adding an alternative never moves the neighbours of the record.
8//! Everything that depends on the exact layout lives in this module; the dynamic container and the
9//! `hla_extendable_variant_record!` macro only call these functions.
10//!
11//! Assumed layout, offsets counted from the start of the outermost value:
12//!
13//! | Part | Rule |
14//! | --- | --- |
15//! | record | starts at a multiple of [`OCTET_BOUNDARY`] (8, whatever the alternatives are) |
16//! | discriminant | its own encoding |
17//! | padding | to a multiple of [`LENGTH_BOUNDARY`] (4) |
18//! | length | `HLAunsignedInteger32BE`: octets of the alternative, 0 when the alternative has no data |
19//! | padding | to a multiple of [`ALTERNATIVE_BOUNDARY`] (8), only when the length is not 0 |
20//! | alternative | its own encoding, exactly `length` octets |
21
22use crate::buffer::{ByteReader, ByteWriter};
23use crate::error::{DecoderError, EncoderError};
24
25/// Octet boundary of every `HLAextendableVariantRecord`, independent of its alternatives.
26pub const OCTET_BOUNDARY: usize = 8;
27/// Octet boundary of the length field.
28pub const LENGTH_BOUNDARY: usize = 4;
29/// Octet boundary at which the alternative starts.
30pub const ALTERNATIVE_BOUNDARY: usize = 8;
31
32/// Aligns the writer for a record. The caller encodes the discriminant next.
33pub fn begin_encode(writer: &mut ByteWriter<'_>) {
34 writer.align(OCTET_BOUNDARY);
35}
36
37/// Writes the length field after the discriminant and aligns for the alternative. The caller then
38/// encodes the alternative, which must take exactly `alternative_len` octets.
39pub fn write_length(writer: &mut ByteWriter<'_>, alternative_len: usize) {
40 writer.align(LENGTH_BOUNDARY);
41 let length = match u32::try_from(alternative_len) {
42 Ok(length) => length,
43 Err(_) => {
44 writer.fail(EncoderError::AlternativeTooLong { length: alternative_len });
45 u32::MAX
46 }
47 };
48 writer.put_slice(&length.to_be_bytes());
49 if alternative_len > 0 {
50 writer.align(ALTERNATIVE_BOUNDARY);
51 }
52}
53
54/// Aligns the reader for a record. The caller decodes the discriminant next.
55pub fn begin_decode(reader: &mut ByteReader<'_>) -> Result<(), DecoderError> {
56 reader.align(OCTET_BOUNDARY)
57}
58
59/// Reads the length field after the discriminant and delimits the alternative.
60pub fn read_alternative<'a>(reader: &mut ByteReader<'a>) -> Result<Alternative<'a>, DecoderError> {
61 reader.align(LENGTH_BOUNDARY)?;
62 let length = usize::try_from(u32::from_be_bytes(reader.take_array()?)).unwrap_or(usize::MAX);
63 if length > 0 {
64 reader.align(ALTERNATIVE_BOUNDARY)?;
65 }
66 Ok(Alternative { reader: reader.window(length)?, length })
67}
68
69/// The octets of one alternative, as delimited by the length field.
70#[derive(Debug, Clone)]
71pub struct Alternative<'a> {
72 reader: ByteReader<'a>,
73 length: usize,
74}
75
76impl<'a> Alternative<'a> {
77 /// Announced length of the alternative in octets.
78 pub fn len(&self) -> usize {
79 self.length
80 }
81
82 /// True when the alternative carries no data.
83 pub fn is_empty(&self) -> bool {
84 self.length == 0
85 }
86
87 /// Reader limited to the alternative; decode the alternative datatype from it.
88 pub fn reader(&mut self) -> &mut ByteReader<'a> {
89 &mut self.reader
90 }
91
92 /// Checks that the alternative datatype used exactly the announced length.
93 pub fn finish(self) -> Result<(), DecoderError> {
94 match self.reader.remaining() {
95 0 => Ok(()),
96 unused => {
97 Err(DecoderError::LengthMismatch { declared: self.length, consumed: self.length - unused })
98 }
99 }
100 }
101
102 /// The octets not consumed so far; the whole alternative when it is skipped.
103 pub fn into_bytes(mut self) -> Vec<u8> {
104 let rest = self.reader.remaining();
105 self.reader.take(rest).map(<[u8]>::to_vec).unwrap_or_default()
106 }
107}