oxidecomputer / amd-apcb

AMD Generic Encapsulated Software Architecture Platform Security Processor Configuration Block manipulation library
Mozilla Public License 2.0
13 stars 1 forks source link

Support more optionality in json #39

Closed daym closed 2 years ago

daym commented 2 years ago

Previously, we abused Default for the initial state of things we deserialize into.

For Rust structs, there is no realistic way to avoid that.

But for Rust bitfields, there absolutely is a way to avoid that, and it's a good idea especially since we want to be allowed to only specify one member of the bitfield in the JSON, and the Default is not necessarily instantiating an empty bitfield. That means we would erroneously start with an instance with one flag set (for example Normal), and then additionally set another flag (for example Critical) in addition. That's probably not what the user intended.

This PR introduces a fn builder that's used by the deserializers (and the builder pattern) instead of Default.

Then, Default can actually have nice user-visible defaults that are not empty.