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
//! HLA data encoding helpers of IEEE 1516-2025: the `encoding` package of the standard Java and C++ APIs
//! (12.11.4.2, 12.12.4.2) as a Rust library.
//!
//! Attribute and parameter values are opaque octets for the RTI. Federates agree on their layout through
//! the datatypes of the FOM, whose encodings are defined by IEEE 1516.2 (OMT). This crate implements
//! those encodings, including the padding rules, in two layers.
//!
//! # Typed layer
//!
//! [`Encode`] and [`Decode`] are implemented by Rust types whose HLA datatype is known at compile time:
//!
//! * the basic representations as wrappers of the Rust type they carry: [`HLAinteger32BE`]`(i32)`,
//!   [`HLAfloat64LE`]`(f64)`, [`HLAoctet`]`(u8)`...;
//! * the simple datatypes [`HLAASCIIchar`], [`HLAunicodeChar`], [`HLAbyte`], [`HLAboolean`];
//! * [`HLAASCIIstring`], [`HLAunicodeString`] (also `String`), [`HLAopaqueData`];
//! * the handle elements ([`HLAfederateHandle`]...) and [`HLAlogicalTime`], [`HLAlogicalTimeInterval`];
//! * `Vec<T>` (`HLAvariableArray`) and `[T; N]` (`HLAfixedArray`);
//! * structs and enums declared with [`hla_fixed_record!`], [`hla_enumerated!`], [`hla_variant_record!`]
//!   and [`hla_extendable_variant_record!`].
//!
//! Bare integers and floats have no encoding of their own because the datatype decides size and byte
//! order; the macros accept them with an encoding marker (`speed: f32 as HLAfloat32BE`).
//!
//! ```
//! use microrti_encoding::{
//!     decode_from_slice_strict, encode_to_vec, hla_fixed_record, DecoderError, HLAfloat64BE, HLAoctet,
//! };
//!
//! hla_fixed_record! {
//!     /// A position report.
//!     pub struct Report {
//!         pub quality: HLAoctet,
//!         pub position: [HLAfloat64BE; 2],
//!         pub label: String,
//!     }
//! }
//!
//! let report = Report {
//!     quality: HLAoctet(3),
//!     position: [HLAfloat64BE(1.0), HLAfloat64BE(-2.0)],
//!     label: "x".into(),
//! };
//! let bytes = encode_to_vec(&report);
//! // 1 octet, 7 of padding, 2 x 8 octets, a count and one UTF-16 code unit.
//! assert_eq!(bytes.len(), 1 + 7 + 16 + 4 + 2);
//! assert_eq!(decode_from_slice_strict::<Report>(&bytes)?, report);
//! # Ok::<(), DecoderError>(())
//! ```
//!
//! # Dynamic layer
//!
//! [`DataElement`] is the object-safe trait of the standard APIs. Every typed value is a data element,
//! and the containers [`HLAfixedRecord`], [`HLAfixedArray`], [`HLAvariableArray`], [`HLAvariantRecord`]
//! and [`HLAextendableVariantRecord`] assemble datatypes at run time, for example from a parsed FOM.
//!
//! ```
//! use microrti_encoding::{
//!     DataElement, HLAboolean, HLAfixedRecord, HLAinteractionClassHandle, HLAvariableArray,
//! };
//!
//! // MOM datatype HLAinteractionSubList: a variable array of HLAinteractionSubscription records.
//! let subscription = |class: [u8; 4], active: bool| {
//!     HLAfixedRecord::new().with(HLAinteractionClassHandle::new(class)).with(HLAboolean(active))
//! };
//! let mut list = HLAvariableArray::new(subscription([0; 4], false));
//! list.push(subscription([0, 0, 0, 9], true))?;
//! let bytes = list.to_bytes()?;
//! assert_eq!(bytes, [0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 9, 0, 0, 0, 1]);
//!
//! let mut decoded = HLAvariableArray::new(subscription([0; 4], false));
//! decoded.decode_slice_strict(&bytes)?;
//! assert_eq!(decoded, list);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! # Padding
//!
//! Every datatype has an octet boundary (IEEE 1516.2 4.14.10), and its encoding starts at an offset,
//! counted from the start of the outermost value, that is a multiple of it. Here each element aligns
//! itself before it writes or reads, which yields the padding that 4.14.10.1 to 4.14.10.5 place between
//! the parts of a constructed datatype and none after the last part: a constructed datatype starts at a
//! multiple of its own boundary, the largest of its parts, so offsets counted from the outermost value
//! and from the start of the datatype give the same padding. [`ByteWriter`] and [`ByteReader`] keep
//! track of the offset. Encoding writes zero padding, as 4.14.10 requires of the sender; the clause says
//! nothing of a receiver that finds other octets there, and decoding ignores what padding holds.
//!
//! # Robustness
//!
//! Decoding never panics. Errors are [`DecoderError`] values, element counts are checked against the
//! octets that are left before anything is allocated, and memory use is bounded by a small multiple of
//! the input size.
//!
//! # FOM-driven codec
//!
//! With the cargo feature `fom` the module `fom` adds a third layer: `FomCodec` encodes and decodes
//! any data type of a merged FDD (`microrti-fom`) by its name, to and from a self-describing `Value`
//! tree. The feature is off by default, and the default build does not depend on `microrti-fom`.
#![warn(missing_docs)]

mod basic;
mod buffer;
mod dynamic;
mod element;
mod error;
pub mod extendable;
#[cfg(feature = "fom")]
pub mod fom;
mod handles;
mod macros;
mod strings;
mod time;
mod typed;

pub use basic::*;
pub use buffer::{ByteReader, ByteWriter, max_boundary, padding_for};
pub use dynamic::{
    HLAextendableVariantRecord, HLAfixedArray, HLAfixedRecord, HLAvariableArray, HLAvariantRecord,
};
pub use element::{DataElement, DynElement};
pub use error::{DecoderError, EncoderError};
pub use handles::*;
pub use strings::{HLAASCIIstring, HLAopaqueData, HLAunicodeString};
pub use time::{HLAlogicalTime, HLAlogicalTimeInterval};
#[doc(hidden)]
pub use typed::__first_or_zero;
pub use typed::{
    BasicData, Decode, Encode, Enumerated, FixedArrayOf, Representation, VariableArrayOf, decode_from_slice,
    decode_from_slice_strict, decode_prefix, encode_into, encode_to_vec, try_encode_to_vec,
};