-
|
I have a special use case where I use an Enum as a way to define fields of a map which is dynamically build. Since serde can represent the value Right now this examples doesn't compile serde_with::with_prefix!(my_enum_prefix "myEnum.");
#[derive(Debug, Serialize, Deserialize)]
enum MyEnum {
#[serde(with = "my_enum_prefix")]
Field,
#[serde(with = "my_enum_prefix")]
Field2
}Compile error: I also tried to add it to the enum of fields: #[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Field {
AdView(#[serde(with = "...")] AdView),
....
}However this results to the prefix not being accounted for. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
The struct SomeStruct {
// ...
#[serde(with = "my_enum_prefix")]
my_enum: MyEnum,
// ..
}I don't think The solution right now is to use Alternatively, it should be possible to provide a proc-macro which performs this step. It could be used like: #[serde_with::prefix_all("my_enum")]
enum MyEnum {
// ...
}Ideally it would work on enum variants and struct fields. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the feedback and indeed a proc-macro could work. |
Beta Was this translation helpful? Give feedback.
The
with_prefix!macro is meant to be used to add prefixes to a struct/enum, if you cannot fully control theSerializeimplementation or if you need different prefixes. Something like this: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…