use crate::error::BoxError;
use crate::{codec::Decoder, message::OrderingKey};
use serde::Serialize;
use std::fmt::Debug;
#[cfg(feature = "kafka")]
use crate::adapters::kafka::{KafkaMetadata, KafkaRecord, text_headers};
#[cfg(feature = "pubsub")]
use crate::adapters::pubsub::{PubSubAttributes, PubSubMetadata, PubSubRecord};
#[cfg(feature = "pulsar")]
use crate::adapters::pulsar::{PulsarMessageId, PulsarMetadata, PulsarRecord};
#[cfg(feature = "rabbitmq")]
use crate::adapters::rabbitmq::{
RabbitMqHeaders, RabbitMqMetadata, RabbitMqProperties, RabbitMqRecord,
text_headers as rabbitmq_text_headers,
};
#[cfg(feature = "servicebus")]
use crate::adapters::servicebus::{
ServiceBusApplicationProperties, ServiceBusMetadata, ServiceBusProperties, ServiceBusRecord,
text_properties as servicebus_text_properties,
};
#[cfg(feature = "sqs")]
use crate::adapters::sqs::{SqsAttributes, SqsMetadata, SqsRecord, text_attributes};
#[cfg(feature = "pulsar")]
use std::collections::HashMap;
pub trait TestRecord: Clone + Debug + Serialize + Send + Sync + 'static {
type Decoded<T: Clone + Send + Sync + 'static>: Clone + Send + Sync + 'static;
fn decode<C>(&self, codec: &C) -> Result<Self::Decoded<C::Value>, BoxError>
where
C: Decoder<Value: Clone + Send + Sync + 'static>;
fn ordering_key(&self) -> Option<OrderingKey> {
None
}
fn propagation_fields(&self) -> Vec<(&str, &str)> {
Vec::new()
}
}
impl TestRecord for Vec<u8> {
type Decoded<T: Clone + Send + Sync + 'static> = T;
fn decode<C>(&self, codec: &C) -> Result<C::Value, BoxError>
where
C: Decoder<Value: Clone + Send + Sync + 'static>,
{
codec.decode(self)
}
}
#[cfg(feature = "kafka")]
impl TestRecord for KafkaRecord<Vec<u8>> {
type Decoded<T: Clone + Send + Sync + 'static> = KafkaRecord<T>;
fn decode<C>(&self, codec: &C) -> Result<KafkaRecord<C::Value>, BoxError>
where
C: Decoder<Value: Clone + Send + Sync + 'static>,
{
Ok(KafkaRecord {
key: self.key.clone(),
value: self
.value
.as_deref()
.map(|bytes| codec.decode(bytes))
.transpose()?,
headers: self.headers.clone(),
metadata: self.metadata.clone(),
})
}
fn ordering_key(&self) -> Option<OrderingKey> {
Some(OrderingKey::new(
self.metadata.topic.as_str(),
i64::from(self.metadata.partition),
))
}
fn propagation_fields(&self) -> Vec<(&str, &str)> {
text_headers(
self.headers
.iter()
.map(|(name, value)| (name.as_str(), value.as_deref())),
)
}
}
#[cfg(feature = "pulsar")]
impl TestRecord for PulsarRecord<Vec<u8>> {
type Decoded<T: Clone + Send + Sync + 'static> = PulsarRecord<T>;
fn decode<C>(&self, codec: &C) -> Result<PulsarRecord<C::Value>, BoxError>
where
C: Decoder<Value: Clone + Send + Sync + 'static>,
{
Ok(PulsarRecord {
value: self
.value
.as_deref()
.map(|bytes| codec.decode(bytes))
.transpose()?,
key: self.key.clone(),
properties: self.properties.clone(),
event_time: self.event_time,
metadata: self.metadata.clone(),
})
}
fn ordering_key(&self) -> Option<OrderingKey> {
Some(OrderingKey::new(
self.metadata.topic.as_str(),
i64::from(self.metadata.message_id.partition),
))
}
fn propagation_fields(&self) -> Vec<(&str, &str)> {
self.properties
.iter()
.map(|(name, value)| (name.as_str(), value.as_str()))
.collect()
}
}
#[cfg(feature = "rabbitmq")]
impl TestRecord for RabbitMqRecord<Vec<u8>> {
type Decoded<T: Clone + Send + Sync + 'static> = RabbitMqRecord<T>;
fn decode<C>(&self, codec: &C) -> Result<RabbitMqRecord<C::Value>, BoxError>
where
C: Decoder<Value: Clone + Send + Sync + 'static>,
{
Ok(RabbitMqRecord {
value: codec.decode(&self.value)?,
headers: self.headers.clone(),
properties: self.properties.clone(),
metadata: self.metadata.clone(),
})
}
fn propagation_fields(&self) -> Vec<(&str, &str)> {
rabbitmq_text_headers(&self.headers)
}
}
#[cfg(feature = "pubsub")]
impl TestRecord for PubSubRecord<Vec<u8>> {
type Decoded<T: Clone + Send + Sync + 'static> = PubSubRecord<T>;
fn decode<C>(&self, codec: &C) -> Result<PubSubRecord<C::Value>, BoxError>
where
C: Decoder<Value: Clone + Send + Sync + 'static>,
{
Ok(PubSubRecord {
value: codec.decode(&self.value)?,
attributes: self.attributes.clone(),
ordering_key: self.ordering_key.clone(),
metadata: self.metadata.clone(),
})
}
fn ordering_key(&self) -> Option<OrderingKey> {
let key = self.ordering_key.as_deref()?;
Some(OrderingKey::new(self.metadata.subscription.as_str(), 0).with_key(key.as_bytes()))
}
fn propagation_fields(&self) -> Vec<(&str, &str)> {
self.attributes
.iter()
.map(|(name, value)| (name.as_str(), value.as_str()))
.collect()
}
}
#[cfg(feature = "sqs")]
impl TestRecord for SqsRecord<Vec<u8>> {
type Decoded<T: Clone + Send + Sync + 'static> = SqsRecord<T>;
fn decode<C>(&self, codec: &C) -> Result<SqsRecord<C::Value>, BoxError>
where
C: Decoder<Value: Clone + Send + Sync + 'static>,
{
Ok(SqsRecord {
value: codec.decode(&self.value)?,
attributes: self.attributes.clone(),
metadata: self.metadata.clone(),
})
}
fn ordering_key(&self) -> Option<OrderingKey> {
let group = self.metadata.message_group_id.as_deref()?;
self.metadata.queue_url.ends_with(".fifo").then(|| {
OrderingKey::new(self.metadata.queue_url.as_str(), 0).with_key(group.as_bytes())
})
}
fn propagation_fields(&self) -> Vec<(&str, &str)> {
text_attributes(&self.attributes)
}
}
#[cfg(feature = "servicebus")]
impl TestRecord for ServiceBusRecord<Vec<u8>> {
type Decoded<T: Clone + Send + Sync + 'static> = ServiceBusRecord<T>;
fn decode<C>(&self, codec: &C) -> Result<ServiceBusRecord<C::Value>, BoxError>
where
C: Decoder<Value: Clone + Send + Sync + 'static>,
{
Ok(ServiceBusRecord {
value: codec.decode(&self.value)?,
application_properties: self.application_properties.clone(),
properties: self.properties.clone(),
metadata: self.metadata.clone(),
})
}
fn propagation_fields(&self) -> Vec<(&str, &str)> {
servicebus_text_properties(&self.application_properties)
}
}
#[cfg(feature = "kafka")]
pub fn kafka_record(
topic: impl Into<String>,
partition: i32,
offset: i64,
value: impl Into<Vec<u8>>,
) -> KafkaRecord<Vec<u8>> {
KafkaRecord {
key: None,
value: Some(value.into()),
headers: Vec::new(),
metadata: KafkaMetadata {
topic: topic.into(),
partition,
offset,
timestamp: None,
},
}
}
#[cfg(feature = "pulsar")]
pub fn pulsar_record(
topic: impl Into<String>,
entry_id: u64,
value: impl Into<Vec<u8>>,
) -> PulsarRecord<Vec<u8>> {
PulsarRecord {
value: Some(value.into()),
key: None,
properties: HashMap::new(),
event_time: None,
metadata: PulsarMetadata {
topic: topic.into(),
message_id: PulsarMessageId {
ledger_id: 0,
entry_id,
partition: -1,
batch_index: -1,
},
publish_time: 0,
},
}
}
#[cfg(feature = "rabbitmq")]
pub fn rabbitmq_record(
queue: impl Into<String>,
delivery_tag: u64,
value: impl Into<Vec<u8>>,
) -> RabbitMqRecord<Vec<u8>> {
let queue = queue.into();
RabbitMqRecord {
value: value.into(),
headers: RabbitMqHeaders::new(),
properties: RabbitMqProperties::default(),
metadata: RabbitMqMetadata {
routing_key: queue.clone(),
queue,
exchange: String::new(),
redelivered: false,
persistent: true,
delivery_tag,
},
}
}
#[cfg(feature = "pubsub")]
pub fn pubsub_record(
subscription: impl Into<String>,
message_id: impl Into<String>,
value: impl Into<Vec<u8>>,
) -> PubSubRecord<Vec<u8>> {
PubSubRecord {
value: value.into(),
attributes: PubSubAttributes::new(),
ordering_key: None,
metadata: PubSubMetadata {
subscription: subscription.into(),
message_id: message_id.into(),
publish_time: None,
delivery_attempt: None,
},
}
}
#[cfg(feature = "sqs")]
pub fn sqs_record(
queue_url: impl Into<String>,
message_id: impl Into<String>,
value: impl Into<Vec<u8>>,
) -> SqsRecord<Vec<u8>> {
SqsRecord {
value: value.into(),
attributes: SqsAttributes::new(),
metadata: SqsMetadata {
queue_url: queue_url.into(),
message_id: message_id.into(),
receive_count: 1,
sent_timestamp: None,
first_receive_timestamp: None,
message_group_id: None,
deduplication_id: None,
sequence_number: None,
},
}
}
#[cfg(feature = "servicebus")]
pub fn servicebus_record(
entity_path: impl Into<String>,
sequence_number: i64,
value: impl Into<Vec<u8>>,
) -> ServiceBusRecord<Vec<u8>> {
ServiceBusRecord {
value: value.into(),
application_properties: ServiceBusApplicationProperties::new(),
properties: ServiceBusProperties::default(),
metadata: ServiceBusMetadata {
entity_path: entity_path.into(),
sequence_number,
enqueued_time: None,
scheduled_enqueue_time: None,
delivery_count: 1,
lock_token: "00000000-0000-0000-0000-000000000000".to_owned(),
dead_letter_source: None,
},
}
}