Module map_decoder

Source
Expand description

Helpers to decode structures encoded as CBOR maps with string keys.

Various different kinds of structured information (such as item stacks) are encoded in this way by OpenComputers. In Lua they look like tables. In OC-Wasm they look like CBOR maps. In Rust it is more useful to expose them as proper structs with named fields. The helpers in this module assist with implementing Decode for such data.

The typical approach is to implement a “builder structure” whose shape parallels that of the main structure, but with each field being an Option over the type of the corresponding field in the main structure. The builder structure should then implement Builder.

Inheritance between structures is supported. In some cases, an object may look similar to another object but with additional keys added. In this case, the builder of the outer object should contain a builder of the inner object and delegate to it.

Traits§

Builder
A builder that can hold the intermediate state while an object is being decoded from a map.
NullableBuilder
A builder that can also build its object type from a CBOR null data item rather than a map.

Functions§

decode
Decodes a type from a map using a builder.
decode_nullable
Decodes a type from a map or null using a builder.