1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//! 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` |
//! | `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 (8, 16, 32 or 64 bits),
//! their endianness (big when absent) and the words of their interpretation. The layout rules, padding
//! included, are those of the typed layer of this crate; `HLAextendableVariantRecord` uses the layout of
//! the [`extendable`](crate::extendable) module.
//!
//! Decoding 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. 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.
pub use FomCodec;
pub use ;
pub use ;