Skip to main content

microrti_encoding/
basic.rs

1//! Basic data representations (IEEE 1516.2 4.14.3, table 29) and the simple datatypes (4.14.4, table 31)
2//! of the standard MIM (11.6).
3//!
4//! Each representation is a transparent wrapper around the Rust type it carries. The octet boundary of a
5//! basic representation is its size in octets (4.14.10, table 43).
6
7use std::fmt;
8
9use crate::buffer::{ByteReader, ByteWriter};
10use crate::error::DecoderError;
11use crate::strings::HLAunicodeString;
12use crate::typed::{BasicData, Decode, Encode, Enumerated, Representation};
13
14macro_rules! basic_type {
15    (
16        $(#[$doc:meta])*
17        $name:ident($native:ty), $size:literal, $to_bytes:ident, $from_bytes:ident
18        $(, derive($($extra:ident),+))?
19    ) => {
20        $(#[$doc])*
21        #[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default $($(, $extra)+)?)]
22        pub struct $name(pub $native);
23
24        impl $name {
25            /// Encoded size in octets, which is also the octet boundary.
26            pub const SIZE: usize = $size;
27
28            /// Wraps a native value.
29            pub const fn new(value: $native) -> Self {
30                Self(value)
31            }
32
33            /// The wrapped native value.
34            pub const fn get(self) -> $native {
35                self.0
36            }
37        }
38
39        impl From<$native> for $name {
40            fn from(value: $native) -> Self {
41                Self(value)
42            }
43        }
44
45        impl From<$name> for $native {
46            fn from(value: $name) -> Self {
47                value.0
48            }
49        }
50
51        impl fmt::Display for $name {
52            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53                fmt::Display::fmt(&self.0, f)
54            }
55        }
56
57        impl Encode for $name {
58            const OCTET_BOUNDARY: usize = $size;
59
60            fn encode(&self, writer: &mut ByteWriter<'_>) {
61                writer.align($size);
62                writer.put_slice(&self.0.$to_bytes());
63            }
64
65            fn encoded_len(&self) -> usize {
66                $size
67            }
68        }
69
70        impl Decode for $name {
71            fn decode(reader: &mut ByteReader<'_>) -> Result<Self, DecoderError> {
72                reader.align($size)?;
73                Ok(Self(<$native>::$from_bytes(reader.take_array()?)))
74            }
75        }
76
77        impl BasicData for $name {
78            type Native = $native;
79
80            fn from_native(value: $native) -> Self {
81                Self(value)
82            }
83
84            fn to_native(self) -> $native {
85                self.0
86            }
87        }
88
89        impl Enumerated for $name {
90            type Representation = Self;
91
92            fn to_representation(&self) -> Self {
93                *self
94            }
95
96            fn from_representation(representation: &Self) -> Option<Self> {
97                Some(*representation)
98            }
99        }
100
101        impl Representation<$native> for $name {
102            const BOUNDARY: usize = $size;
103
104            fn encode_value(value: &$native, writer: &mut ByteWriter<'_>) {
105                Self(*value).encode(writer);
106            }
107
108            fn decode_value(reader: &mut ByteReader<'_>) -> Result<$native, DecoderError> {
109                Self::decode(reader).map(|value| value.0)
110            }
111
112            fn encoded_len_of(_value: &$native) -> usize {
113                $size
114            }
115        }
116    };
117}
118
119basic_type! {
120    /// `HLAoctet`: one uninterpreted octet.
121    HLAoctet(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
122}
123basic_type! {
124    /// `HLAoctetPairBE`: two octets, most significant first.
125    HLAoctetPairBE(u16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
126}
127basic_type! {
128    /// `HLAoctetPairLE`: two octets, least significant first.
129    HLAoctetPairLE(u16), 2, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
130}
131basic_type! {
132    /// `HLAinteger16BE`: 16-bit two's complement integer, big endian.
133    HLAinteger16BE(i16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
134}
135basic_type! {
136    /// `HLAinteger16LE`: 16-bit two's complement integer, little endian.
137    HLAinteger16LE(i16), 2, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
138}
139basic_type! {
140    /// `HLAinteger32BE`: 32-bit two's complement integer, big endian.
141    HLAinteger32BE(i32), 4, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
142}
143basic_type! {
144    /// `HLAinteger32LE`: 32-bit two's complement integer, little endian.
145    HLAinteger32LE(i32), 4, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
146}
147basic_type! {
148    /// `HLAinteger64BE`: 64-bit two's complement integer, big endian.
149    HLAinteger64BE(i64), 8, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
150}
151basic_type! {
152    /// `HLAinteger64LE`: 64-bit two's complement integer, little endian.
153    HLAinteger64LE(i64), 8, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
154}
155basic_type! {
156    /// `HLAunsignedInteger8`: 8-bit unsigned integer (MIM basic representation new in 2025).
157    HLAunsignedInteger8(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
158}
159basic_type! {
160    /// `HLAunsignedInteger16BE`: 16-bit unsigned integer, big endian.
161    HLAunsignedInteger16BE(u16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
162}
163basic_type! {
164    /// `HLAunsignedInteger16LE`: 16-bit unsigned integer, little endian.
165    HLAunsignedInteger16LE(u16), 2, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
166}
167basic_type! {
168    /// `HLAunsignedInteger32BE`: 32-bit unsigned integer, big endian.
169    HLAunsignedInteger32BE(u32), 4, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
170}
171basic_type! {
172    /// `HLAunsignedInteger32LE`: 32-bit unsigned integer, little endian.
173    HLAunsignedInteger32LE(u32), 4, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
174}
175basic_type! {
176    /// `HLAunsignedInteger64BE`: 64-bit unsigned integer, big endian.
177    HLAunsignedInteger64BE(u64), 8, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
178}
179basic_type! {
180    /// `HLAunsignedInteger64LE`: 64-bit unsigned integer, little endian.
181    HLAunsignedInteger64LE(u64), 8, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
182}
183basic_type! {
184    /// `HLAfloat32BE`: IEEE 754 single precision, big endian.
185    HLAfloat32BE(f32), 4, to_be_bytes, from_be_bytes
186}
187basic_type! {
188    /// `HLAfloat32LE`: IEEE 754 single precision, little endian.
189    HLAfloat32LE(f32), 4, to_le_bytes, from_le_bytes
190}
191basic_type! {
192    /// `HLAfloat64BE`: IEEE 754 double precision, big endian.
193    HLAfloat64BE(f64), 8, to_be_bytes, from_be_bytes
194}
195basic_type! {
196    /// `HLAfloat64LE`: IEEE 754 double precision, little endian.
197    HLAfloat64LE(f64), 8, to_le_bytes, from_le_bytes
198}
199basic_type! {
200    /// `HLAASCIIchar`: one ASCII character carried in an `HLAoctet`. The value is not range checked.
201    HLAASCIIchar(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
202}
203basic_type! {
204    /// `HLAunicodeChar`: one UTF-16 code unit carried in an `HLAoctetPairBE`.
205    HLAunicodeChar(u16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
206}
207basic_type! {
208    /// `HLAbyte`: one uninterpreted 8-bit byte carried in an `HLAoctet`.
209    HLAbyte(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
210}
211
212/// `HLAboolean`: the enumerated datatype `HLAfalse` = 0, `HLAtrue` = 1 carried in an `HLAinteger32BE`.
213///
214/// Decoding any other value is an error.
215#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
216pub struct HLAboolean(pub bool);
217
218impl HLAboolean {
219    /// Wraps a `bool`.
220    pub const fn new(value: bool) -> Self {
221        Self(value)
222    }
223
224    /// The wrapped `bool`.
225    pub const fn get(self) -> bool {
226        self.0
227    }
228}
229
230impl From<bool> for HLAboolean {
231    fn from(value: bool) -> Self {
232        Self(value)
233    }
234}
235
236impl From<HLAboolean> for bool {
237    fn from(value: HLAboolean) -> Self {
238        value.0
239    }
240}
241
242impl fmt::Display for HLAboolean {
243    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
244        f.write_str(if self.0 { "HLAtrue" } else { "HLAfalse" })
245    }
246}
247
248impl Encode for HLAboolean {
249    const OCTET_BOUNDARY: usize = 4;
250
251    fn encode(&self, writer: &mut ByteWriter<'_>) {
252        HLAinteger32BE(i32::from(self.0)).encode(writer);
253    }
254
255    fn encoded_len(&self) -> usize {
256        4
257    }
258}
259
260impl Decode for HLAboolean {
261    fn decode(reader: &mut ByteReader<'_>) -> Result<Self, DecoderError> {
262        let raw = HLAinteger32BE::decode(reader)?;
263        Self::from_representation(&raw).ok_or_else(|| DecoderError::InvalidValue {
264            type_name: "HLAboolean",
265            detail: format!("{} is neither HLAfalse (0) nor HLAtrue (1)", raw.0),
266        })
267    }
268}
269
270impl Enumerated for HLAboolean {
271    type Representation = HLAinteger32BE;
272
273    fn to_representation(&self) -> HLAinteger32BE {
274        HLAinteger32BE(i32::from(self.0))
275    }
276
277    fn from_representation(representation: &HLAinteger32BE) -> Option<Self> {
278        match representation.0 {
279            0 => Some(Self(false)),
280            1 => Some(Self(true)),
281            _ => None,
282        }
283    }
284}
285
286impl Representation<bool> for HLAboolean {
287    const BOUNDARY: usize = 4;
288
289    fn encode_value(value: &bool, writer: &mut ByteWriter<'_>) {
290        Self(*value).encode(writer);
291    }
292
293    fn decode_value(reader: &mut ByteReader<'_>) -> Result<bool, DecoderError> {
294        Self::decode(reader).map(|value| value.0)
295    }
296
297    fn encoded_len_of(_value: &bool) -> usize {
298        4
299    }
300}
301
302/// MIM simple datatype `HLAcount`.
303pub type HLAcount = HLAinteger32BE;
304/// MIM simple datatype `HLAseconds`.
305pub type HLAseconds = HLAinteger32BE;
306/// MIM simple datatype `HLAmsec`.
307pub type HLAmsec = HLAinteger32BE;
308/// MIM simple datatype `HLAindex`.
309pub type HLAindex = HLAinteger32BE;
310/// MIM simple datatype `HLAnormalizedFederateHandle`.
311pub type HLAnormalizedFederateHandle = HLAinteger64BE;
312/// MIM simple datatype `HLAnormalizedObjectClassHandle`.
313pub type HLAnormalizedObjectClassHandle = HLAinteger64BE;
314/// MIM simple datatype `HLAnormalizedInteractionClassHandle`.
315pub type HLAnormalizedInteractionClassHandle = HLAinteger64BE;
316/// MIM simple datatype `HLAnormalizedObjectInstanceHandle`.
317pub type HLAnormalizedObjectInstanceHandle = HLAinteger64BE;
318/// MIM simple datatype `HLAinteger64Time`: a bare logical time of the integer representation (12.4).
319pub type HLAinteger64Time = HLAinteger64BE;
320/// MIM simple datatype `HLAfloat64Time`: a bare logical time of the float representation (12.4).
321pub type HLAfloat64Time = HLAfloat64BE;
322/// MIM array datatype `HLAupdateRateName`.
323pub type HLAupdateRateName = HLAunicodeString;
324/// MIM array datatype `HLAtoken`: an `HLAfixedArray` of zero `HLAbyte`, encoded as nothing at all.
325pub type HLAtoken = [HLAbyte; 0];