use crate::error::BoxError;
use crate::shutdown::CancellationToken;
use serde::Serialize;
use std::{fmt::Debug, future::Future, pin::Pin, sync::Arc};
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct OrderingKey {
scope: Arc<str>,
index: i64,
key: Option<Arc<[u8]>>,
}
impl OrderingKey {
pub fn new(scope: impl Into<Arc<str>>, index: i64) -> Self {
Self {
scope: scope.into(),
index,
key: None,
}
}
pub fn with_key(mut self, key: impl Into<Arc<[u8]>>) -> Self {
self.key = Some(key.into());
self
}
pub fn scope(&self) -> &str {
&self.scope
}
pub fn index(&self) -> i64 {
self.index
}
pub fn key(&self) -> Option<&[u8]> {
self.key.as_deref()
}
}
pub trait SourceMessage: Send + 'static {
type Item: Clone + Send + Sync + 'static;
fn decode(&self) -> Result<Self::Item, BoxError>;
fn ack(self) -> impl Future<Output = Result<(), BoxError>> + Send;
type Raw: Debug + Serialize + Send + Sync + 'static;
fn raw(&self) -> Self::Raw;
fn ordering_key(&self) -> Option<OrderingKey> {
None
}
fn revocation(&self) -> Option<CancellationToken> {
None
}
fn propagation_fields(&self) -> Vec<(&str, &str)> {
Vec::new()
}
}
type AckFuture = Pin<Box<dyn Future<Output = Result<(), BoxError>> + Send>>;
type Acknowledge = Box<dyn FnOnce() -> AckFuture + Send>;
pub struct Delivery<T, R = ()> {
pub value: T,
ack: Acknowledge,
ordering_key: Option<OrderingKey>,
revocation: Option<CancellationToken>,
propagation: Vec<(String, String)>,
raw: R,
}
impl<T> Delivery<T> {
pub fn new<F, Fut>(value: T, ack: F) -> Self
where
F: FnOnce() -> Fut + Send + 'static,
Fut: Future<Output = Result<(), BoxError>> + Send + 'static,
{
Self {
value,
ack: Box::new(|| Box::pin(ack())),
ordering_key: None,
revocation: None,
propagation: Vec::new(),
raw: (),
}
}
pub fn untracked(value: T) -> Self {
Self::new(value, || async { Ok(()) })
}
}
impl<T, R> Delivery<T, R> {
pub fn with_raw<U>(self, raw: U) -> Delivery<T, U> {
Delivery {
value: self.value,
ack: self.ack,
ordering_key: self.ordering_key,
revocation: self.revocation,
propagation: self.propagation,
raw,
}
}
pub fn with_ordering_key(mut self, key: OrderingKey) -> Self {
self.ordering_key = Some(key);
self
}
pub fn with_revocation(mut self, token: CancellationToken) -> Self {
self.revocation = Some(token);
self
}
pub fn with_propagation_fields(mut self, fields: Vec<(String, String)>) -> Self {
self.propagation = fields;
self
}
pub async fn ack(self) -> Result<(), BoxError> {
(self.ack)().await
}
}
impl<T, R> SourceMessage for Delivery<T, R>
where
T: Clone + Send + Sync + 'static,
R: Clone + Debug + Serialize + Send + Sync + 'static,
{
type Item = T;
type Raw = R;
fn decode(&self) -> Result<T, BoxError> {
Ok(self.value.clone())
}
fn raw(&self) -> R {
self.raw.clone()
}
async fn ack(self) -> Result<(), BoxError> {
Delivery::ack(self).await
}
fn ordering_key(&self) -> Option<OrderingKey> {
self.ordering_key.clone()
}
fn revocation(&self) -> Option<CancellationToken> {
self.revocation.clone()
}
fn propagation_fields(&self) -> Vec<(&str, &str)> {
self.propagation
.iter()
.map(|(name, value)| (name.as_str(), value.as_str()))
.collect()
}
}