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
//! FOM-driven dynamic codec (cargo feature `fom`): encodes and decodes attribute and parameter values by
//! the name of their FOM data type, to and from a self-describing [`Value`] tree.
//!
//! [`FomCodec::new`] reduces the data type tables of a merged FDD (`microrti_fom::Fdd`) to a resolved type
//! table. The RTI uses it for the Management Object Model (every MOM attribute and parameter, 11.6);
//! tools use it to print or script arbitrary federation data.
//!
//! ```
//! use microrti_encoding::fom::{FomCodec, Value};
//! use microrti_fom::Fdd;
//!
//! let codec = FomCodec::new(&Fdd::with_standard_mim())?;
//! // HLAinteractionSubList: a variable array of HLAinteractionSubscription records.
//! let list = Value::array([Value::record([
//!     ("HLAinteractionClass", Value::handle([0u8, 0, 0, 9])),
//!     ("HLAactive", Value::Bool(true)),
//! ])]);
//! let bytes = codec.encode("HLAinteractionSubList", &list)?;
//! assert_eq!(bytes, [0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 9, 0, 0, 0, 1]);
//!
//! let decoded = codec.decode("HLAinteractionSubList", &bytes)?;
//! assert_eq!(decoded, list);
//! assert_eq!(decoded.to_string(), "[{HLAinteractionClass: handle(0x00000009), HLAactive: true}]");
//! assert_eq!(decoded.get(0).and_then(|entry| entry.field("HLAactive")), Some(&Value::Bool(true)));
//! # Ok::<(), microrti_encoding::fom::CodecError>(())
//! ```
//!
//! # How data types map to values
//!
//! | Data type | [`Value`] |
//! | --- | --- |
//! | integer basic representations (`HLAinteger16BE`...`HLAunsignedInteger64LE`, `HLAoctetPairBE/LE`) | `Integer` |
//! | `HLAfloat32BE/LE`, `HLAfloat64BE/LE` | `Float` |
//! | basic representations of more than 64 bits | `Bytes` |
//! | `HLAoctet`, and so `HLAbyte` | `Octet` |
//! | `HLAASCIIchar`, `HLAunicodeChar` | `Char` |
//! | simple and reference data types | the value of their representation |
//! | enumerated data types | `Enum`; `HLAboolean` gives `Bool` |
//! | arrays of `HLAASCIIchar` or `HLAunicodeChar` (`HLAASCIIstring`, `HLAunicodeString`...) | `String` |
//! | arrays of `HLAbyte` or `HLAoctet` (`HLAopaqueData`, `HLAlogicalTime`, `HLAtoken`...) | `Bytes` |
//! | the handle arrays of the MIM (`HLAfederateHandle`...) | `Handle` |
//! | other arrays, `HLAfixedArray` or `HLAvariableArray` | `Array` |
//! | `HLAfixedRecord` | `Record` |
//! | `HLAvariantRecord`, `HLAextendableVariantRecord` | `Variant` |
//! | user-defined encodings, unsupported or missing definitions | `Raw` |
//!
//! Basic representations that are not predefined are classified by their size, their endianness (big
//! when absent) and the words of their interpretation: an integer up to 64 bits (a float for 32 and 64
//! bits), the octets as they travel (`Bytes`) above. A size that is no multiple of 8 bits takes the next
//! multiple of 8 and its octet boundary is the smallest power of two that holds it (IEEE 1516.2
//! 4.14.10); the clause does not say where the padding bits go, here they are the most significant bits
//! of the field, written as zeros and ignored when read. Sizes above 1024 bits are raw. The layout rules, padding
//! included, are those of the typed layer of this crate; `HLAextendableVariantRecord` uses the layout of
//! the [`extendable`](crate::extendable) module.
//!
//! IEEE 1516.2 says how values are laid out, not what a receiver does with octets that break the rules.
//! Decoding ignores what padding octets and padding bits hold (4.14.10 asks zeros of the sender only;
//! encoding writes zeros), refuses what makes the layout unreadable (a negative count or length, an
//! alternative that does not fill its announced length), and is lenient where the octets are well
//! formed but the value is outside the FOM: a number that is no enumerator becomes an `Enum` without
//! name, an element count outside the cardinality range of a variable array is accepted. Encoding
//! rejects both, except in variant records, where a number without enumerator is passed on (annex C
//! lets other modules add enumerators). A discriminant of an `HLAvariantRecord` that no alternative
//! applies to, an enumerator that no row names or, without `HLAother`, a number that no enumerator has,
//! is the discriminant alone (4.14.10.2): a `Variant` without alternative and without value. Encoding
//! accepts the obvious substitutes: an
//! enumerator given as a name (`String`) or a number (`Integer`), an integer where a float or an octet
//! is needed, a one-character string for a character, record fields in any order.
//!
//! A value of a data type that cannot be resolved is `Raw`: it takes every octet that is left. A record
//! may end with such a field; any other data type that contains one is `Raw` as a whole.
//!
//! # Robustness
//!
//! Decoding never panics. Element counts are checked against the octets left before anything is
//! allocated, data types may nest at most 64 levels deep and recursive definitions are rejected by
//! [`FomCodec::new`], so memory and stack use are bounded by the input and by the FDD. Errors name the
//! place inside the value (`HLAinteractionSubList[3].HLAinteractionClass`), the offset and the reason.
//!
//! # Serde
//!
//! [`Value`] implements `Serialize` and `Deserialize`. Human-readable formats use natural shapes:
//! booleans, numbers, strings, arrays and, for records, objects in field order. The other variants are
//! objects with one key that starts with `$`: `{"$octet": 7}`, `{"$char": "a"}`, `{"$bytes": "0a0b"}`,
//! `{"$handle": "00000001"}`, `{"$raw": "ff"}`, `{"$float": "NaN"}` (floats that are not finite),
//! `{"$enum": {"name": "TimeGranted", "value": 0}}` and
//! `{"$variant": {"discriminant": {..}, "alternative": "..", "value": ..}}`. A record field whose name
//! starts with `$` is written with a second `$`. Binary formats (postcard) get a tagged enum instead.
//! Deserializing refuses values that nest deeper than 128 levels, in any format.
//!
//! A generic JSON tree that sorts object keys (`serde_json::Value` by default) returns the fields of a
//! record in another order; the encoder does not mind. `serde_json` reads floats exactly only with its
//! feature `float_roundtrip` (the workspace enables it); without it a float may come back one unit in
//! the last place off.

mod codec;
mod decode;
mod encode;
mod error;
mod serde_impl;
mod types;
mod value;

pub use codec::FomCodec;
pub use error::{CodecError, Failure, Reason};
pub use value::{Enumerator, Value, VariantValue};