Bit lost on how to implement Deserializers

Hello. I'm having some difficulty wrapping my head around how to implement Deserializers. I have an enum:

#[derive(Debug, Serialize, Deserialize)]
pub enum Variants {

that I am using as a variable inside of a struct:

#[derive(Debug, Serialize, Deserialize)]
pub struct Hello {
    message: String,
    variant: Variants,

I'm trying to deserialize the struct from a bson::ordered::OrderedDocument, but I get the error

error[E0277]: the trait bound `bson::ordered::OrderedDocument: variants::_IMPL_DESERIALIZE_FOR_Variants::_serde::Deserializer<'_> is not satisfied'

I see String gets automatically deserialized I'm assuming due to serde's StringDeserializer struct. Do I have to implement a similar thing for my enum's deserialization to work?

What code are you using to deserialize, specifically? Something like

let hello = bson::from_bson(bson::Bson::Document(doc)).unwrap();


Hello @asymmetrikon


where ordered_document is a bson::ordered::OrderedDocument.

You can't deserialize directly like that - you need something that implements Deserializer, which OrderedDocument doesn't - it only implements Deserialize. You can use bson::from_bson to do the deserialization:

let hello: Hello = bson::from_bson(bson::Bson::Document(ordered_document))?;


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.