-
Notifications
You must be signed in to change notification settings - Fork 546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serde module macro #1396
Serde module macro #1396
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1396 +/- ##
==========================================
+ Coverage 91.95% 92.17% +0.22%
==========================================
Files 40 41 +1
Lines 18035 17013 -1022
==========================================
- Hits 16584 15682 -902
+ Misses 1451 1331 -120 ☔ View full report in Codecov by Sentry. |
86c5e97
to
4a2a95e
Compare
4a2a95e
to
7afbe4d
Compare
7afbe4d
to
c945153
Compare
So I generally don't love macros like this that have fairly intricate DSL nature to them. I don't think it's all that natural to use a |
The problem is that I also don't particularly like it, but if 80% of all the code I have to scroll through is boilerplate I have a hard time catching issues. By now I think we are in pretty good shape, but we only support a few limited 64-bit timestamps. Once we add a couple thousand lines more it is even harder to maintain. Do you see another way to organize things or get rid of the boilerplate, besides using a macro with suboptimal syntax? |
Use an integration test to generate code, similar to the windows bindings stuff? |
That could work. Not going to be easy; I'll look into it sometime. |
Every (de)serializer we have for use with
serde_with
comes with a lot of boilerplate.Each needs two modules (for a regular and optional variant),
serialize
anddeserialize
methods, aVisitor
type and implementation, some documentation, and doc examples that serve as basic tests.This is the perfect use case for a macro. The most important inputs for such a macro are:
serialize
function and deserialize visitorsBecause we can't concatenate idents in macro's it unfortunately needs a few more inputs to have everything covered.
A disadvantage of macro's is that it becomes a kind of DSL. I went with the most straightforward solution: to make it to look like a struct with all the macro arguments.
The advantage is that in my opinion it becomes much easier to maintain the code and spot errors in documentation or implementation. And that is the part were our bugs used to be, not in the boilerplate. I think the line count speaks for itself; the implementations are ca. a quarter of before.
There is no change in functionality, but thanks to the macro the documentation is more consistent.
I did not replicate all doc examples. In my opinion the doc example for the regular modules is important, because that is what you point
serde_with
at. But I doubt anyone clicks on the individualserialize
anddeserialize
functions. I did extend the module documentation a bit to highlight how to use them though.Actually found another bug in our deserialization from timestamps while working on this.