I have the following code:
extern crate serde;
#[macro_use]
extern crate serde_derive;
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Base {
bold: Option<bool>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Component {
String {
#[serde(flatten)] base: Base,
text: String,
},
}
This gives me a compiler error:
error[E0425]: cannot find value `__collect` in this scope
--> src/main.rs:10:28
|
10 | #[derive(Debug, Serialize, Deserialize)]
| ^^^^^^^^^^^ did you mean `__content`?
error[E0277]: the trait bound `<__S as serde::Serializer>::SerializeStruct: serde::ser::SerializeMap` is not satisfied
--> src/main.rs:10:17
|
10 | #[derive(Debug, Serialize, Deserialize)]
| ^^^^^^^^^ the trait `serde::ser::SerializeMap` is not implemented for `<__S as serde::Serializer>::SerializeStruct`
|
= help: consider adding a `where <__S as serde::Serializer>::SerializeStruct: serde::ser::SerializeMap` bound
= note: required because of the requirements on the impl of `serde::Serializer` for `serde::private::ser::FlatMapSerializer<'_, <__S as serde::Serializer>::SerializeStruct>`
= note: required by `serde::Serialize::serialize`
If I change my code to this, it compiles fine:
extern crate serde;
#[macro_use]
extern crate serde_derive;
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Base {
bold: Option<bool>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Striii {
#[serde(flatten)]
base: Base,
text: String,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Component {
String(Striii),
}
I don't understand the compiler error or why it shouldn't work. Does Serde have no support for field attributes inside enum struct variants?
This is a known issue with Serde for both serialization and deserialization. There are no listed workarounds.