quartiq / minimq

Minimal no_std MQTT v5.0 client implementation
MIT License
144 stars 16 forks source link

Refactor serialization #94

Closed ryan-summers closed 2 years ago

ryan-summers commented 2 years ago

Refactor the serialization and deserialization of packets. In an ideal world, we should use serde to handle all types of serial/deserialization operations if possible.

ryan-summers commented 2 years ago

I have been looking at this extensively, and there's a number of reasons why we may want to avoid using serde for this:

  1. Serde's deserializer trait requires a lot of boilerplate for types that we don't need to support
  2. Properties have non-sequential property identifiers. This means that we cannot use #[derive(Deserialize, Serialize)] on them, as the enum position is encoded as opposed to the property ID.
  3. Manual implementation of deserialize and serialize for properties is not possible without restructuring the Property, as we need to deserialize multiple values (the property and the value), but the serde traits consume deserializer when deserializing a single value.
ryan-summers commented 2 years ago

After an implementation in #101, it was found that many of the drawbacks can be worked around through custom implementation of a deserializer. There is a small amount of manual impl Deserialize and impl Serialize required for the properties and packets themselves, but the rest of the types may be derived at will.