How to serialize this OffsetDateTime?

How to serialize this OffsetDateTime?

Hi everyone,

I have trouble serializing this time::OffsetDateTime struct to send it over Pulsar using the Rust Pulsar client,

What I'm trying to do:

# Cargo.toml
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
time = "0.3.4"
pulsar = "4.0.0"
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StructToSerialize {
    some_string: String,
    date: OffsetDateTime,
}

I want to serialize this in order to satisfy the trait pulsar::SerializeMessage, required to send messages over Apache Pulsar.

By copying and adapting the boilerplate from other projects, I do this:

use pulsar::{producer, Error as PulsarError, SerializeMessage};

impl SerializeMessage for StructToSerialize {
    fn serialize_message(input: Self) -> Result<producer::Message, PulsarError> {
        let payload = serde_json::to_vec(&input).map_err(|e| PulsarError::Custom(e.to_string()))?;
        Ok(producer::Message {
            payload,
            ..Default::default()
        })
    }
}

I have a threefold error:

error[E0277]: the trait bound `OffsetDateTime: Serialize` is not satisfied
  --> src/ssh/event_dto.rs:26:5
   |
26 |     date: OffsetDateTime,
   |     ^^^^ the trait `Serialize` is not implemented for `OffsetDateTime`
   |
   = note: required by `message::_::_serde::ser::SerializeStruct::serialize_field`

error[E0277]: the trait bound `OffsetDateTime: Deserialize<'_>` is not satisfied
  --> src/ssh/event_dto.rs:26:5
   |
26 |     date: OffsetDateTime,
   |     ^^^^ the trait `Deserialize<'_>` is not implemented for `OffsetDateTime`
   |
   = note: required by `next_element`

error[E0277]: the trait bound `OffsetDateTime: Deserialize<'_>` is not satisfied
  --> src/ssh/event_dto.rs:26:5
   |
26 |     date: OffsetDateTime,
   |     ^^^^ the trait `Deserialize<'_>` is not implemented for `OffsetDateTime`
   |
   = note: required by `next_value`

Digging into time::OffsetDateTime, I find that this type has

So what am I missing here? Thanks a lot in advance.

1 Like

Nevermind, I got to understand what this means:

Screenshot from 2021-11-12 19-51-36

This fixes it:

time = { version = "0.3.4", features = ["serde"] }

sigh there are no limits to my dumbness

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.