microrti-encoding 0.4.0

HLA 4 data encoding of IEEE 1516.2-2025: basic types, strings, handles, arrays, records, variant records and a FOM-driven codec.
Documentation
//! Typed errors of the encoding helpers (`EncoderException` and `DecoderException` of the standard APIs).

use microrti_core::{ExceptionKind, RtiError};

/// Reasons why a value cannot be encoded.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum EncoderError {
    /// A variant record has no alternative registered for its active discriminant.
    #[error("no variant is associated with the active discriminant")]
    NoVariantForDiscriminant,
    /// An array, string or octet sequence has more elements than the signed 32-bit element count can hold.
    #[error("{count} elements do not fit the signed 32-bit element count of an HLA array")]
    TooManyElements {
        /// Number of elements of the offending value.
        count: usize,
    },
    /// The alternative of an `HLAextendableVariantRecord` is longer than its 32-bit length field allows.
    #[error("alternative of {length} octets does not fit the length field of an HLAextendableVariantRecord")]
    AlternativeTooLong {
        /// Encoded length of the alternative in octets.
        length: usize,
    },
    /// An element does not have the type its container requires.
    #[error("type mismatch: {context}")]
    TypeMismatch {
        /// What was being checked.
        context: &'static str,
    },
    /// An index is outside a container.
    #[error("index {index} is out of range for {size} element(s)")]
    IndexOutOfRange {
        /// The index that was used.
        index: usize,
        /// Number of elements of the container.
        size: usize,
    },
    /// A value is outside the range of its datatype (for example a non-ASCII character).
    #[error("invalid value for {type_name}: {detail}")]
    InvalidValue {
        /// HLA datatype name.
        type_name: &'static str,
        /// Human readable explanation.
        detail: String,
    },
}

/// Reasons why a byte sequence cannot be decoded.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum DecoderError {
    /// The input ends before the value is complete.
    #[error("truncated input: {needed} octet(s) needed at offset {offset}, {remaining} remaining")]
    Truncated {
        /// Offset from the start of the outermost value.
        offset: usize,
        /// Octets required at that offset.
        needed: usize,
        /// Octets actually left.
        remaining: usize,
    },
    /// The octets do not form a legal value of the datatype.
    #[error("invalid value for {type_name}: {detail}")]
    InvalidValue {
        /// HLA datatype name.
        type_name: &'static str,
        /// Human readable explanation.
        detail: String,
    },
    /// An element count is negative.
    #[error("invalid element count {count}")]
    InvalidCount {
        /// The decoded count.
        count: i32,
    },
    /// An element count is larger than the number of octets left, so it cannot be honest.
    #[error("element count {count} exceeds the {remaining} remaining octet(s)")]
    CountExceedsInput {
        /// The decoded count.
        count: usize,
        /// Octets left after the count.
        remaining: usize,
    },
    /// A value has a different number of elements than its datatype prescribes.
    #[error("count mismatch for {type_name}: expected {expected}, found {found}")]
    CountMismatch {
        /// HLA datatype name.
        type_name: &'static str,
        /// Number of elements the datatype requires.
        expected: usize,
        /// Number of elements found.
        found: usize,
    },
    /// The discriminant of a variant record selects no known alternative.
    #[error("unknown discriminant {discriminant} for {type_name}")]
    UnknownDiscriminant {
        /// Name of the variant record datatype.
        type_name: &'static str,
        /// Debug rendering of the decoded discriminant.
        discriminant: String,
    },
    /// A value is not one of the enumerators of an enumerated datatype.
    #[error("{value} is not an enumerator of {type_name}")]
    InvalidEnumerator {
        /// Name of the enumerated datatype.
        type_name: &'static str,
        /// Debug rendering of the decoded value.
        value: String,
    },
    /// The alternative of an `HLAextendableVariantRecord` did not use exactly the announced length.
    #[error("alternative announces {declared} octet(s) but its datatype uses {consumed}")]
    LengthMismatch {
        /// Length announced by the record.
        declared: usize,
        /// Octets the alternative datatype consumed.
        consumed: usize,
    },
    /// Octets are left after the value (strict decoding only).
    #[error("{count} trailing octet(s) after the value")]
    TrailingBytes {
        /// Number of unused octets.
        count: usize,
    },
}

impl From<EncoderError> for RtiError {
    fn from(error: EncoderError) -> Self {
        RtiError::new(ExceptionKind::CouldNotEncode, error.to_string())
    }
}

impl From<DecoderError> for RtiError {
    fn from(error: DecoderError) -> Self {
        RtiError::new(ExceptionKind::CouldNotDecode, error.to_string())
    }
}