serde icon indicating copy to clipboard operation
serde copied to clipboard

Exhaustive internally tagged tests + support of internally tagged enums in non self-describing formats

Open Mingun opened this issue 10 months ago • 0 comments

This PR starts as a refactor of internally tagged enums tests in preparation to updating #1922, but in the process of development, I found some interesting consequences.

The first is that ContentDeserializer and ContentRefDeserializer do what they should not do, namely, determine the validity of certain data when calling deserialization methods. All Deserializer methods are just a hints what data is expected from the deserializer, and the deserializer is not obliged to follow them. It is free to call any Visitor method it sees fit. It is Visitor responsibility to detect if called visit_* method appropriate or not. So I removed all decisions from ContentDeserializer and ContentRefDeserializer about validity of content in certain methods.

This change will allow to make the next step and eliminate all calls to deserialize_any from internally tagged enums. Because this method does not called anymore, this would allow to support internally tagged enums in non-self-describing formats. I've replaced deserialize_any with deserialize_map because:

  • internally tagged enums serialized using serialize_map (for newtype variants) / serialize_struct (for unit and struct variants), that means that deserialization also should expect map
  • deserialization of structs looks the same -- calling Deserializer::deserialize_map or Deserializer::deserialize_struct and provide a visitor with visit_map and visit_seq. This is what both TaggedContentVisitor and InternallyTaggedUnitVisitor provides

The same considerations are used when replacing deserialize_any with deserialize_map for untagged variant; that code path is also called for adjacently tagged enums. Passed visitor supports only visit_map method.

This PR depends on the https://github.com/serde-rs/test/pull/31 and I temporary add a [patch] section to use patched version to make tests passable. That is why this PR in a draft stage.

Closes #2187 Replaces #2557

Mingun avatar Aug 12 '23 10:08 Mingun