1use 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 pub const SIZE: usize = $size;
27
28 pub const fn new(value: $native) -> Self {
30 Self(value)
31 }
32
33 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(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
122}
123basic_type! {
124 HLAoctetPairBE(u16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
126}
127basic_type! {
128 HLAoctetPairLE(u16), 2, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
130}
131basic_type! {
132 HLAinteger16BE(i16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
134}
135basic_type! {
136 HLAinteger16LE(i16), 2, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
138}
139basic_type! {
140 HLAinteger32BE(i32), 4, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
142}
143basic_type! {
144 HLAinteger32LE(i32), 4, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
146}
147basic_type! {
148 HLAinteger64BE(i64), 8, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
150}
151basic_type! {
152 HLAinteger64LE(i64), 8, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
154}
155basic_type! {
156 HLAunsignedInteger8(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
158}
159basic_type! {
160 HLAunsignedInteger16BE(u16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
162}
163basic_type! {
164 HLAunsignedInteger16LE(u16), 2, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
166}
167basic_type! {
168 HLAunsignedInteger32BE(u32), 4, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
170}
171basic_type! {
172 HLAunsignedInteger32LE(u32), 4, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
174}
175basic_type! {
176 HLAunsignedInteger64BE(u64), 8, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
178}
179basic_type! {
180 HLAunsignedInteger64LE(u64), 8, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
182}
183basic_type! {
184 HLAfloat32BE(f32), 4, to_be_bytes, from_be_bytes
186}
187basic_type! {
188 HLAfloat32LE(f32), 4, to_le_bytes, from_le_bytes
190}
191basic_type! {
192 HLAfloat64BE(f64), 8, to_be_bytes, from_be_bytes
194}
195basic_type! {
196 HLAfloat64LE(f64), 8, to_le_bytes, from_le_bytes
198}
199basic_type! {
200 HLAASCIIchar(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
202}
203basic_type! {
204 HLAunicodeChar(u16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
206}
207basic_type! {
208 HLAbyte(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
210}
211
212#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
216pub struct HLAboolean(pub bool);
217
218impl HLAboolean {
219 pub const fn new(value: bool) -> Self {
221 Self(value)
222 }
223
224 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
302pub type HLAcount = HLAinteger32BE;
304pub type HLAseconds = HLAinteger32BE;
306pub type HLAmsec = HLAinteger32BE;
308pub type HLAindex = HLAinteger32BE;
310pub type HLAnormalizedFederateHandle = HLAinteger64BE;
312pub type HLAnormalizedObjectClassHandle = HLAinteger64BE;
314pub type HLAnormalizedInteractionClassHandle = HLAinteger64BE;
316pub type HLAnormalizedObjectInstanceHandle = HLAinteger64BE;
318pub type HLAinteger64Time = HLAinteger64BE;
320pub type HLAfloat64Time = HLAfloat64BE;
322pub type HLAupdateRateName = HLAunicodeString;
324pub type HLAtoken = [HLAbyte; 0];