microrti-encoding 0.2.2

HLA 4 data encoding of IEEE 1516.2-2025: basic types, strings, handles, arrays, records, variant records and a FOM-driven codec.
Documentation
//! Logical time elements (12.3, 12.4): a time or interval travels inside attribute and parameter values as
//! the MIM array datatypes `HLAlogicalTime` and `HLAtimeInterval`, `HLAvariableArray`s of `HLAbyte`
//! holding the encoded form of the value.
//!
//! The encoded form does not say which time representation it uses, so the elements keep the octets and
//! the caller names the representation of its federation execution when it wants the value.

use microrti_core::{LogicalTime, LogicalTimeInterval, TimeKind};

use crate::error::DecoderError;
use crate::strings::octet_array_element;

/// Encoded size of both standardized time representations.
const TIME_SIZE: usize = 8;

fn check_size(type_name: &'static str, octets: &[u8]) -> Result<(), DecoderError> {
    if octets.len() == TIME_SIZE {
        Ok(())
    } else {
        Err(DecoderError::CountMismatch { type_name, expected: TIME_SIZE, found: octets.len() })
    }
}

octet_array_element! {
    /// `HLAlogicalTime`: the encoded form of a logical time.
    HLAlogicalTime
}

impl HLAlogicalTime {
    /// The element carrying `time`.
    pub fn from_time(time: LogicalTime) -> Self {
        Self(time.encode().to_vec())
    }

    /// Interprets the octets as a time of the given representation.
    pub fn time(&self, kind: TimeKind) -> Result<LogicalTime, DecoderError> {
        check_size("HLAlogicalTime", &self.0)?;
        kind.decode_time(&self.0).map_err(|error| DecoderError::InvalidValue {
            type_name: "HLAlogicalTime",
            detail: error.message,
        })
    }
}

impl From<LogicalTime> for HLAlogicalTime {
    fn from(time: LogicalTime) -> Self {
        Self::from_time(time)
    }
}

octet_array_element! {
    /// `HLAlogicalTimeInterval` (MIM datatype `HLAtimeInterval`): the encoded form of a time interval.
    HLAlogicalTimeInterval
}

impl HLAlogicalTimeInterval {
    /// The element carrying `interval`.
    pub fn from_interval(interval: LogicalTimeInterval) -> Self {
        Self(interval.encode().to_vec())
    }

    /// Interprets the octets as an interval of the given representation.
    pub fn interval(&self, kind: TimeKind) -> Result<LogicalTimeInterval, DecoderError> {
        check_size("HLAlogicalTimeInterval", &self.0)?;
        kind.decode_interval(&self.0).map_err(|error| DecoderError::InvalidValue {
            type_name: "HLAlogicalTimeInterval",
            detail: error.message,
        })
    }
}

impl From<LogicalTimeInterval> for HLAlogicalTimeInterval {
    fn from(interval: LogicalTimeInterval) -> Self {
        Self::from_interval(interval)
    }
}