Skip to content

Should with_prefix be used for Unit type enums? #204

Answered by jonasbb
elpiel asked this question in Q&A
Discussion options

You must be logged in to vote

The with_prefix! macro is meant to be used to add prefixes to a struct/enum, if you cannot fully control the Serialize implementation or if you need different prefixes. Something like this:

struct SomeStruct {
	// ...
	#[serde(with = "my_enum_prefix")]
	my_enum: MyEnum,
	// ..
}

I don't think with_prefix! can really be used to rename the fields of a struct or unit variants without such a wrapping struct/enum. The field name / unit variant already gets emitted before the with part runs, so it cannot change those values.

The solution right now is to use #[serde(rename = "myEnum....")] on every enum variant.

Alternatively, it should be possible to provide a proc-macro which performs this ste…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by jonasbb
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #204 on December 16, 2020 20:07.