media-io / yaserde

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

#[derive(YaDeserialize)] requires Default #144

Open Wassasin opened 2 years ago

Wassasin commented 2 years ago

When deriving YaDeserialize the macro requires that all member types implement the Default trait. I suppose this is to instantiate a value even when the XML document is missing that child or attribute.

As a consumer of this crate I would have to either make all fields Option, or implement Default for all of them. However I would rather that YaDeserialize would throw an error if that child or attribute is missing from the XML, and have an attribute #[yaserde(default)] for the current behaviour.

2xsaiko commented 7 months ago

With 82df2e1, how do you specify the equivalent of #[serde(default)] (i.e. use Default::default if value missing)? There is a #[yaserde(default)] but it seems to be for something different since it takes a string value.

Wassasin commented 7 months ago

It has been a long time since I created the relevant commit, but I believe the string can be used to specify a function name. This function will be called when the value is not provided in the XML input.

2xsaiko commented 7 months ago

Hmm, so it does indeed. It has to be in the current scope though, I tested with setting it to Default::default() (with or without the parens) before and it gave an invalid identifier error.