media-io / yaserde

Yet Another Serializer/Deserializer
MIT License
177 stars 59 forks source link

Impl YaDeserialize for a type that should be treated as Primitive #148

Open rrichardson opened 2 years ago

rrichardson commented 2 years ago

~I am baffled. I can run the examples crate tests just fine.~

~However, when I run this on my own code, deriving YaDeserialize fails because the constraint of T: YaDeserialize isn't met because T doesn't implement YaDeserialize.~

~This is for types like i32, String, etc.~

~I have looked through the code and I don't see anywhere where YaDeserialize is implemented for anything, so I have no idea how the examples crate works, where it has some fields which are String.~

*Edit*

I ran cargo-expand, and now understand how it avoids needing to impl YaDeserialize for primitive types. It bakes the from_str directly into the struct's deserialization routine (that seems like it'd cause a lot of unnecessary code duplication, but I guess that pertains to the RFC for the API redesign)

I guess my more fundamental question is:

How can I impl YaDeserialize for a custom type which I basically want to treat as a String it's a specially formatted Timestamp which will always serialize to a String-ish type, and always be parsed from a String-ish type. So it shouldn't have any properties, etc.

At the moment, I'm attempting to duplicate the impl for i32 and string members that I find.

However, I'm not sure how I can direct yaserde_derive::YaDeserialize to fetch the type correctly, since I basically want to treat it as a primitive.

mlevkov commented 1 year ago

@rrichardson I've put together an example of how you can do this for your own types here is the example -> https://github.com/media-io/yaserde/issues/87

nrbnlulu commented 6 months ago

You can use

use xsd_macro_utils::UtilsDefaultSerde;

#[derive(Default, PartialEq, Debug, UtilsTupleIo, UtilsDefaultSerde)]
pub struct YourCustomType(pub String);