mirror of
https://github.com/ramsayleung/rspotify.git
synced 2026-04-26 07:55:55 +03:00
[GH-ISSUE #163] The way to serialize enum as number and duration #55
Labels
No labels
Stale
bug
discussion
enhancement
good first issue
good first issue
help wanted
pull-request
question
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/rspotify#55
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @ramsayleung on GitHub (Dec 10, 2020).
Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/163
Is your feature request related to a problem? Please describe.
As #127 pointing out that
AudioAnalysisSection::modeandAudioFeatures::modearef32s but should beOption<Mode>s whereenum Mode { Major, Minor }as it is more useful, it needs to serialize enum to number and deserialize number to enum.Describe the solution you'd like
The
serdeofficial documentation provides a recommended way to serialize enum to number, but this solution needs an external crate namedserde_repr, and I think we should be cautious to introduce a new dependency since it will increase the compile time.Describe alternatives you've considered
there is another solution to convert enum value to an integer, but needs unsafe operation, check this link for more details
Additional context
I am trying to figure out that is there a more lightweight way to do so, if you have any suggestion, feel free to help :)
PS:
I am still looking for a way to serialize/deserialize
Duration, and I find some tracking issues:@ramsayleung commented on GitHub (Dec 12, 2020):
I figured out that the better choice is that we de/serialize
std::time::Durationmanually with our own de/serialize functions, just deserializingDurationfrom millisecond and serializingDurationto millisecond:@marioortizmanero commented on GitHub (Dec 13, 2020):
I think manually deserializing
std::time::Durationis a great choice as well, since it's not that much code. As to theAudioAnalysisSectionandAudioAnalysisSegmentissue, I don't fully understand it. What exactly isenum Mode { Major, Minor }and where in the models does it come from?@ramsayleung commented on GitHub (Dec 14, 2020):
The
modefiled fromAudioAnalysisSectionis an integer, which indicates the modality (major or minor) of a track, the type of scale from which its melodic content is derived. This field will contain a0forminor, a1formajor, or a-1for no result.the
modefield was stored as af32before, and Koxiaet suggests that it's would be better to replacef32withenum Mode { Major, Minor}. Thus it's necessary to figure out how to deserialize a0/1/-1toenum Mode. Perhaps we could manually deserializingf32toenum Modeas what we do forstd::time::Duration.@marioortizmanero commented on GitHub (Dec 14, 2020):
Ah I was confused because rather than
f32it could've just usedi32ori8to represent 0, 1, or -1. I would do exactly asstd::time::Duration, yeah. It seems like the easiest way to go. If we end up needing more of this in the future then we can pullserde_repr.@ramsayleung commented on GitHub (Dec 15, 2020):
Yep, I have to confess it's a design mistake,
i8is a better choice to represent0,1, or-1.Agree! we should avoid to introduce any unnecessary dependency as well.
@ramsayleung commented on GitHub (Dec 15, 2020):
My solution to manually deserialize
0/1/-1toenum Mode:You may be curious about the
visit_i64andvisit_u64method, since if I setmodefiled to-1,Serdewill callvisit_i64, then I setmodefield to1, it will call thevisit_u64, I am not sure how doesSerdedispatch to relatedvisitmethod.