use std::sync::Arc;
use bytes::Bytes;
use kafka_client_core::PartitionIndex;
use super::super::record::{
ProducerHeader as StoredProducerHeader, ProducerRecord as StoredProducerRecord,
ProducerRecordParts,
};
#[derive(Debug, Eq, PartialEq)]
pub struct ProducerHeader {
pub(super) name: String,
pub(super) value: Option<Bytes>,
}
impl ProducerHeader {
pub fn new(name: impl Into<String>, value: impl Into<Bytes>) -> Self {
Self {
name: name.into(),
value: Some(value.into()),
}
}
pub fn null(name: impl Into<String>) -> Self {
Self {
name: name.into(),
value: None,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn value(&self) -> Option<&Bytes> {
self.value.as_ref()
}
}
#[derive(Debug, Eq, PartialEq)]
pub struct ProducerRecord {
pub(super) topic: Arc<str>,
pub(super) partition: Option<i32>,
pub(super) timestamp_ms: Option<i64>,
pub(super) key: Option<Bytes>,
pub(super) value: Option<Bytes>,
pub(super) headers: Vec<ProducerHeader>,
}
impl ProducerRecord {
pub fn to(topic: impl Into<Arc<str>>) -> Self {
Self {
topic: topic.into(),
partition: None,
timestamp_ms: None,
key: None,
value: None,
headers: Vec::new(),
}
}
pub const fn partition(mut self, partition: i32) -> Self {
self.partition = Some(partition);
self
}
pub const fn timestamp_milliseconds(mut self, timestamp_ms: i64) -> Self {
self.timestamp_ms = Some(timestamp_ms);
self
}
pub fn key(mut self, key: impl Into<Bytes>) -> Self {
self.key = Some(key.into());
self
}
pub fn value(mut self, value: impl Into<Bytes>) -> Self {
self.value = Some(value.into());
self
}
pub fn header(mut self, header: ProducerHeader) -> Self {
self.headers.push(header);
self
}
pub fn topic(&self) -> &str {
&self.topic
}
pub const fn explicit_partition(&self) -> Option<i32> {
self.partition
}
pub const fn timestamp(&self) -> Option<i64> {
self.timestamp_ms
}
pub fn key_bytes(&self) -> Option<&Bytes> {
self.key.as_ref()
}
pub fn value_bytes(&self) -> Option<&Bytes> {
self.value.as_ref()
}
pub fn headers(&self) -> &[ProducerHeader] {
&self.headers
}
pub(super) fn validate_explicit_partition(&self) -> Option<PartitionIndex> {
let raw = u32::try_from(self.partition?).ok()?;
Some(PartitionIndex::from_raw(raw))
}
pub(super) fn into_stored(
self,
partition: Option<PartitionIndex>,
default_timestamp_ms: i64,
) -> StoredProducerRecord {
let defaulted_timestamp = self.timestamp_ms.is_none();
let timestamp_ms = self.timestamp_ms.unwrap_or(default_timestamp_ms);
let headers = self
.headers
.into_iter()
.map(|header| StoredProducerHeader::new(header.name, header.value))
.collect();
StoredProducerRecord::from_public(
self.topic,
partition,
timestamp_ms,
defaulted_timestamp,
self.key,
self.value,
headers,
)
}
pub(super) fn from_stored(record: StoredProducerRecord) -> Self {
let ProducerRecordParts {
topic,
partition,
timestamp_ms,
defaulted_timestamp,
key,
value,
headers,
} = record.into_public_parts();
let headers = headers
.into_iter()
.map(|header| {
let (name, value) = header.into_parts();
ProducerHeader { name, value }
})
.collect();
Self {
topic,
partition: partition.and_then(|partition| i32::try_from(partition.get()).ok()),
timestamp_ms: (!defaulted_timestamp).then_some(timestamp_ms),
key,
value,
headers,
}
}
}