Skip to main content

microrti_encoding/
basic.rs

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