microrti-encoding 0.2.0

HLA 4 data encoding of IEEE 1516.2-2025: basic types, strings, handles, arrays, records, variant records and a FOM-driven codec.
Documentation
//! Basic data representations (IEEE 1516.2 4.14.3, table 29) and the simple datatypes (4.14.4, table 31)
//! of the standard MIM (11.6).
//!
//! Each representation is a transparent wrapper around the Rust type it carries. The octet boundary of a
//! basic representation is its size in octets (4.14.10, table 43).

use std::fmt;

use crate::buffer::{ByteReader, ByteWriter};
use crate::error::DecoderError;
use crate::strings::HLAunicodeString;
use crate::typed::{BasicData, Decode, Encode, Enumerated, Representation};

macro_rules! basic_type {
    (
        $(#[$doc:meta])*
        $name:ident($native:ty), $size:literal, $to_bytes:ident, $from_bytes:ident
        $(, derive($($extra:ident),+))?
    ) => {
        $(#[$doc])*
        #[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default $($(, $extra)+)?)]
        pub struct $name(pub $native);

        impl $name {
            /// Encoded size in octets, which is also the octet boundary.
            pub const SIZE: usize = $size;

            /// Wraps a native value.
            pub const fn new(value: $native) -> Self {
                Self(value)
            }

            /// The wrapped native value.
            pub const fn get(self) -> $native {
                self.0
            }
        }

        impl From<$native> for $name {
            fn from(value: $native) -> Self {
                Self(value)
            }
        }

        impl From<$name> for $native {
            fn from(value: $name) -> Self {
                value.0
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                fmt::Display::fmt(&self.0, f)
            }
        }

        impl Encode for $name {
            const OCTET_BOUNDARY: usize = $size;

            fn encode(&self, writer: &mut ByteWriter<'_>) {
                writer.align($size);
                writer.put_slice(&self.0.$to_bytes());
            }

            fn encoded_len(&self) -> usize {
                $size
            }
        }

        impl Decode for $name {
            fn decode(reader: &mut ByteReader<'_>) -> Result<Self, DecoderError> {
                reader.align($size)?;
                Ok(Self(<$native>::$from_bytes(reader.take_array()?)))
            }
        }

        impl BasicData for $name {
            type Native = $native;

            fn from_native(value: $native) -> Self {
                Self(value)
            }

            fn to_native(self) -> $native {
                self.0
            }
        }

        impl Enumerated for $name {
            type Representation = Self;

            fn to_representation(&self) -> Self {
                *self
            }

            fn from_representation(representation: &Self) -> Option<Self> {
                Some(*representation)
            }
        }

        impl Representation<$native> for $name {
            const BOUNDARY: usize = $size;

            fn encode_value(value: &$native, writer: &mut ByteWriter<'_>) {
                Self(*value).encode(writer);
            }

            fn decode_value(reader: &mut ByteReader<'_>) -> Result<$native, DecoderError> {
                Self::decode(reader).map(|value| value.0)
            }

            fn encoded_len_of(_value: &$native) -> usize {
                $size
            }
        }
    };
}

basic_type! {
    /// `HLAoctet`: one uninterpreted octet.
    HLAoctet(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAoctetPairBE`: two octets, most significant first.
    HLAoctetPairBE(u16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAoctetPairLE`: two octets, least significant first.
    HLAoctetPairLE(u16), 2, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAinteger16BE`: 16-bit two's complement integer, big endian.
    HLAinteger16BE(i16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAinteger16LE`: 16-bit two's complement integer, little endian.
    HLAinteger16LE(i16), 2, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAinteger32BE`: 32-bit two's complement integer, big endian.
    HLAinteger32BE(i32), 4, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAinteger32LE`: 32-bit two's complement integer, little endian.
    HLAinteger32LE(i32), 4, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAinteger64BE`: 64-bit two's complement integer, big endian.
    HLAinteger64BE(i64), 8, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAinteger64LE`: 64-bit two's complement integer, little endian.
    HLAinteger64LE(i64), 8, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAunsignedInteger8`: 8-bit unsigned integer (MIM basic representation new in 2025).
    HLAunsignedInteger8(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAunsignedInteger16BE`: 16-bit unsigned integer, big endian.
    HLAunsignedInteger16BE(u16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAunsignedInteger16LE`: 16-bit unsigned integer, little endian.
    HLAunsignedInteger16LE(u16), 2, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAunsignedInteger32BE`: 32-bit unsigned integer, big endian.
    HLAunsignedInteger32BE(u32), 4, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAunsignedInteger32LE`: 32-bit unsigned integer, little endian.
    HLAunsignedInteger32LE(u32), 4, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAunsignedInteger64BE`: 64-bit unsigned integer, big endian.
    HLAunsignedInteger64BE(u64), 8, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAunsignedInteger64LE`: 64-bit unsigned integer, little endian.
    HLAunsignedInteger64LE(u64), 8, to_le_bytes, from_le_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAfloat32BE`: IEEE 754 single precision, big endian.
    HLAfloat32BE(f32), 4, to_be_bytes, from_be_bytes
}
basic_type! {
    /// `HLAfloat32LE`: IEEE 754 single precision, little endian.
    HLAfloat32LE(f32), 4, to_le_bytes, from_le_bytes
}
basic_type! {
    /// `HLAfloat64BE`: IEEE 754 double precision, big endian.
    HLAfloat64BE(f64), 8, to_be_bytes, from_be_bytes
}
basic_type! {
    /// `HLAfloat64LE`: IEEE 754 double precision, little endian.
    HLAfloat64LE(f64), 8, to_le_bytes, from_le_bytes
}
basic_type! {
    /// `HLAASCIIchar`: one ASCII character carried in an `HLAoctet`. The value is not range checked.
    HLAASCIIchar(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAunicodeChar`: one UTF-16 code unit carried in an `HLAoctetPairBE`.
    HLAunicodeChar(u16), 2, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}
basic_type! {
    /// `HLAbyte`: one uninterpreted 8-bit byte carried in an `HLAoctet`.
    HLAbyte(u8), 1, to_be_bytes, from_be_bytes, derive(Eq, Ord, Hash)
}

/// `HLAboolean`: the enumerated datatype `HLAfalse` = 0, `HLAtrue` = 1 carried in an `HLAinteger32BE`.
///
/// Decoding any other value is an error.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct HLAboolean(pub bool);

impl HLAboolean {
    /// Wraps a `bool`.
    pub const fn new(value: bool) -> Self {
        Self(value)
    }

    /// The wrapped `bool`.
    pub const fn get(self) -> bool {
        self.0
    }
}

impl From<bool> for HLAboolean {
    fn from(value: bool) -> Self {
        Self(value)
    }
}

impl From<HLAboolean> for bool {
    fn from(value: HLAboolean) -> Self {
        value.0
    }
}

impl fmt::Display for HLAboolean {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(if self.0 { "HLAtrue" } else { "HLAfalse" })
    }
}

impl Encode for HLAboolean {
    const OCTET_BOUNDARY: usize = 4;

    fn encode(&self, writer: &mut ByteWriter<'_>) {
        HLAinteger32BE(i32::from(self.0)).encode(writer);
    }

    fn encoded_len(&self) -> usize {
        4
    }
}

impl Decode for HLAboolean {
    fn decode(reader: &mut ByteReader<'_>) -> Result<Self, DecoderError> {
        let raw = HLAinteger32BE::decode(reader)?;
        Self::from_representation(&raw).ok_or_else(|| DecoderError::InvalidValue {
            type_name: "HLAboolean",
            detail: format!("{} is neither HLAfalse (0) nor HLAtrue (1)", raw.0),
        })
    }
}

impl Enumerated for HLAboolean {
    type Representation = HLAinteger32BE;

    fn to_representation(&self) -> HLAinteger32BE {
        HLAinteger32BE(i32::from(self.0))
    }

    fn from_representation(representation: &HLAinteger32BE) -> Option<Self> {
        match representation.0 {
            0 => Some(Self(false)),
            1 => Some(Self(true)),
            _ => None,
        }
    }
}

impl Representation<bool> for HLAboolean {
    const BOUNDARY: usize = 4;

    fn encode_value(value: &bool, writer: &mut ByteWriter<'_>) {
        Self(*value).encode(writer);
    }

    fn decode_value(reader: &mut ByteReader<'_>) -> Result<bool, DecoderError> {
        Self::decode(reader).map(|value| value.0)
    }

    fn encoded_len_of(_value: &bool) -> usize {
        4
    }
}

/// MIM simple datatype `HLAcount`.
pub type HLAcount = HLAinteger32BE;
/// MIM simple datatype `HLAseconds`.
pub type HLAseconds = HLAinteger32BE;
/// MIM simple datatype `HLAmsec`.
pub type HLAmsec = HLAinteger32BE;
/// MIM simple datatype `HLAindex`.
pub type HLAindex = HLAinteger32BE;
/// MIM simple datatype `HLAnormalizedFederateHandle`.
pub type HLAnormalizedFederateHandle = HLAinteger64BE;
/// MIM simple datatype `HLAnormalizedObjectClassHandle`.
pub type HLAnormalizedObjectClassHandle = HLAinteger64BE;
/// MIM simple datatype `HLAnormalizedInteractionClassHandle`.
pub type HLAnormalizedInteractionClassHandle = HLAinteger64BE;
/// MIM simple datatype `HLAnormalizedObjectInstanceHandle`.
pub type HLAnormalizedObjectInstanceHandle = HLAinteger64BE;
/// MIM simple datatype `HLAinteger64Time`: a bare logical time of the integer representation (12.4).
pub type HLAinteger64Time = HLAinteger64BE;
/// MIM simple datatype `HLAfloat64Time`: a bare logical time of the float representation (12.4).
pub type HLAfloat64Time = HLAfloat64BE;
/// MIM array datatype `HLAupdateRateName`.
pub type HLAupdateRateName = HLAunicodeString;
/// MIM array datatype `HLAtoken`: an `HLAfixedArray` of zero `HLAbyte`, encoded as nothing at all.
pub type HLAtoken = [HLAbyte; 0];