dtolnay / typetag

Serde serializable and deserializable trait objects
Apache License 2.0
1.19k stars 38 forks source link

Support rename_all attribute #4

Open dtolnay opened 5 years ago

dtolnay commented 5 years ago
#[typetag::serde(tag = "type", rename_all = "snake_case")]
trait WebEvent {...} //        ^^^^^^^^^^^^^^^^^^^^^^^^^

#[typetag::serde]
impl WebEvent for PageLoad {...}

#[typetag::serde]
impl WebEvent for Click {...}

These should serialize with "type": "page_load" and "type": "click".

ghost commented 3 years ago

As far as my understanding of macros goes, this is difficult to achieve at compile time because there isn't a way to read this value from impl sites, where type names can be known. Would it be an acceptable implementation, then, to paste RenameRule::apply_to_variant to the trait-defining site, and convert the strings during the creation of typetag::Registry? This would, of course, introduce multiple copies of the renaming code if multiple traits are decorated with typetag, but imo the code is short enough for this to not pose a huge problem.

I'd be happy to make a PR if this is the case!

therealhieu commented 1 year ago

any update on this feature?