use crate::v1::meta::MailboxMessage;
use crate::v1::meta::{mailbox_message::Payload, MailboxMessageHeader};
use serde::Serialize;
use serde_json::Result;
use std::{
collections::HashMap,
fmt::{Display, Formatter},
};
impl MailboxMessage {
pub fn json_message<T>(
subject: &str,
from: &str,
to: &str,
timestamp_millis: i64,
payload: &T,
tracing_context: Option<HashMap<String, String>>,
) -> Result<MailboxMessage>
where
T: ?Sized + Serialize + Display,
{
let payload = serde_json::to_string(payload)?;
let header = MailboxMessageHeader {
tracing_context: tracing_context.unwrap_or_default(),
};
Ok(MailboxMessage {
header: Some(header),
id: 0, subject: subject.to_string(),
from: from.to_string(),
to: to.to_string(),
timestamp_millis,
payload: Some(Payload::Json(payload)),
})
}
}
impl Display for MailboxMessage {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"MailboxMessage(id={}, subject={}, from={}, to={}, timestamp_millis={}, payload={:?})",
self.id, self.subject, self.from, self.to, self.timestamp_millis, self.payload
)
}
}