1use 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 pub const SIZE: usize = $size;
26
27 pub const fn new(value: $native) -> Self {
29 Self(value)
30 }
31
32 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(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
121}
122basic_type! {
123 HLAoctetPairBE(u16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
125}
126basic_type! {
127 HLAoctetPairLE(u16), 2, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
129}
130basic_type! {
131 HLAinteger16BE(i16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
133}
134basic_type! {
135 HLAinteger16LE(i16), 2, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
137}
138basic_type! {
139 HLAinteger32BE(i32), 4, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
141}
142basic_type! {
143 HLAinteger32LE(i32), 4, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
145}
146basic_type! {
147 HLAinteger64BE(i64), 8, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
149}
150basic_type! {
151 HLAinteger64LE(i64), 8, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
153}
154basic_type! {
155 HLAunsignedInteger8(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
157}
158basic_type! {
159 HLAunsignedInteger16BE(u16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
161}
162basic_type! {
163 HLAunsignedInteger16LE(u16), 2, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
165}
166basic_type! {
167 HLAunsignedInteger32BE(u32), 4, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
169}
170basic_type! {
171 HLAunsignedInteger32LE(u32), 4, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
173}
174basic_type! {
175 HLAunsignedInteger64BE(u64), 8, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
177}
178basic_type! {
179 HLAunsignedInteger64LE(u64), 8, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
181}
182basic_type! {
183 HLAfloat32BE(f32), 4, to_be_bytes, from_be_bytes
185}
186basic_type! {
187 HLAfloat32LE(f32), 4, to_le_bytes, from_le_bytes
189}
190basic_type! {
191 HLAfloat64BE(f64), 8, to_be_bytes, from_be_bytes
193}
194basic_type! {
195 HLAfloat64LE(f64), 8, to_le_bytes, from_le_bytes
197}
198basic_type! {
199 HLAASCIIchar(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
201}
202basic_type! {
203 HLAunicodeChar(u16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
205}
206basic_type! {
207 HLAbyte(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
209}
210
211#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
215pub struct HLAboolean(pub bool);
216
217impl HLAboolean {
218 pub const fn new(value: bool) -> Self {
220 Self(value)
221 }
222
223 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
301pub type HLAcount = HLAinteger32BE;
303pub type HLAseconds = HLAinteger32BE;
305pub type HLAmsec = HLAinteger32BE;
307pub type HLAindex = HLAinteger32BE;
309pub type HLAnormalizedFederateHandle = HLAinteger64BE;
311pub type HLAnormalizedObjectClassHandle = HLAinteger64BE;
313pub type HLAnormalizedInteractionClassHandle = HLAinteger64BE;
315pub type HLAnormalizedObjectInstanceHandle = HLAinteger64BE;
317pub type HLAinteger64Time = HLAinteger64BE;
319pub type HLAfloat64Time = HLAfloat64BE;
321pub type HLAupdateRateName = HLAunicodeString;
323pub type HLAtoken = [HLAbyte; 0];