use crate::propagation::PropagationCarrier;
use serde::Serialize;
use std::collections::BTreeMap;
pub type PubSubAttributes = BTreeMap<String, String>;
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct PubSubMetadata {
pub subscription: String,
pub message_id: String,
pub publish_time: Option<i64>,
pub delivery_attempt: Option<u32>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct PubSubRecord<T> {
pub value: T,
pub attributes: PubSubAttributes,
pub ordering_key: Option<String>,
pub metadata: PubSubMetadata,
}
impl<T> PubSubRecord<T> {
pub fn metadata(&self) -> &PubSubMetadata {
&self.metadata
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PubSubPublish<T> {
pub value: T,
pub attributes: PubSubAttributes,
pub ordering_key: Option<String>,
}
impl<T> PubSubPublish<T> {
pub fn new(value: T) -> Self {
Self {
value,
attributes: PubSubAttributes::new(),
ordering_key: None,
}
}
pub fn with_ordering_key(mut self, key: impl Into<String>) -> Self {
self.ordering_key = Some(key.into());
self
}
}
impl<T> PropagationCarrier for PubSubPublish<T> {
fn set_propagation_field(&mut self, name: &str, value: String) {
self.attributes.insert(name.to_owned(), value);
}
}