egg-mode-rs / egg-mode

a twitter api crate for rust
https://crates.io/crates/egg-mode
Mozilla Public License 2.0
372 stars 65 forks source link

impl Serialize for Tweet/TwitterUser #99

Closed QuietMisdreavus closed 4 years ago

QuietMisdreavus commented 4 years ago

Closes #90

This PR adds a Serialize impl for various types in the library, most notably Tweet and TwitterUser. Since those types have existing Deserialize impls that load via a "raw" type that mirrors Twitter's JSON representation, i needed to introduce a new mechanism to allow loading via either Twitter's JSON or egg-mode's.

The way i did this for this PR was to introduce a new round_trip! macro that wrapped the structs and added some new items used internally by the macro:

One major drawback with this approach is that if the "raw" type gets out-of-sync with Twitter's representation, the only error message you see is "data did not match any variant of untagged enum SerEnum", instead of knowing which field is missing. On the other hand, doing it this way means that there is no overhead of doing the deserialization manually (anyone remember the original FromJson impls?) or loading the whole response into a serde_json::Value first. I'm posting this provisionally to see if this suits people's purposes, and to ask whether this should wrap other public types in egg-mode.

QuietMisdreavus commented 4 years ago

I had an idea about the error-message problem and posted a new commit. It adds two new functions to types that deserialize via round_trip!: twitter_deser_error and roundtrip_deser_error. They both attempt to deserialize into the inner types of SerEnum, and reports the error that comes out, if any. It's not a perfect solution, but should allow for some kind of error reporting when combined with the raw module. I'm not sure how to make it so that these can be called from the actual deserialization process, but it's at least something.