Added serde(skip)
functionality inside Enum Variants
#180
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Observation
I wanted to skip serializing an enum error variant in a project, similar to this:
This produced the following error: "
skip
is not applicable to newtype fields".Problem
I wanted to skip serializing the field, to not send it to the client, as I want to keep
Any
errors on the server and treat them as an anonymousInternalServerError
to the client.However, I want to keep the internal error data for logging, I do not want to "delete" the error data.
It seemed like I needed a copy of my
Error
and omit theAny
variants value (anyhow::Error
), and then create aFrom
/Into
implementation from theError
with theAny
field into theError
without theAny
field.Solution
In this pull request I have modified the
ts-rs
code so that it generates the types in the same way thatserde
serializes Enums in these conditions.This is achieved by treating an Enum Variant with exactly one Unnamed field (which was already done by the code before in some places) and if that field has a
skip
attribute, then treat that Enum Variant as a Unit instead of a Newtype.Tests
I implemented tests to guarantee the behaviour for all 4 types of Enum Tags (Untagged, Externally, Adjacently, Internally) both for
Named
andUnnamed
field variants.