leontoeides / google_maps

An unofficial Google Maps Platform client library for the Rust programming language.
https://crates.io/crates/google_maps
Apache License 2.0
58 stars 24 forks source link

Deserialization of PlaceType is not invertible. #13

Closed adamnemecek closed 1 year ago

adamnemecek commented 1 year ago

This code does not work.

use google_maps::prelude::PlaceType;
let z: PlaceType = serde_json::from_str(&serde_json::to_string(&PlaceType::Accounting).unwrap()).unwrap();
leontoeides commented 1 year ago

That's a quirk of Rust. String::from(my_string) and my_string.to_string() are not the same.

I implemented Display (presumably intended for display to end-users, terminal output) for these types. In Rust, the to_string() function uses the Display implementation to convert the enumeration into a string.

Therefore I'd argue that x.to_string() is for rendering to display, log files, and such - and that String::from(x) is better suited for internal processing, serialization, deserialization, etc.

I've implemented String::from for these types. This will give the "code" (rather than the text) you're looking for. I haven't tried this myself, but you could try using String::from(&PlaceType::Accounting) rather than serde_json::to_string(&PlaceType::Accounting).unwrap()

leontoeides commented 1 year ago

Just a note that I came around on this issue and this has been fixed.