use time::OffsetDateTime;
use crate::record::InboxRecord;
use crate::record_id::InboxRecordId;
use crate::scope::InboxScope;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct InboxDeadCursor {
#[cfg_attr(feature = "serde", serde(with = "time::serde::rfc3339"))]
dead_at: OffsetDateTime,
id: InboxRecordId,
}
impl InboxDeadCursor {
#[must_use]
pub const fn new(dead_at: OffsetDateTime, id: InboxRecordId) -> Self {
Self { dead_at, id }
}
#[must_use]
pub fn from_record(record: &InboxRecord) -> Option<Self> {
record.dead_at.map(|dead_at| Self::new(dead_at, record.id))
}
#[must_use]
pub const fn dead_at(self) -> OffsetDateTime {
self.dead_at
}
#[must_use]
pub const fn id(self) -> InboxRecordId {
self.id
}
}
pub trait InboxDeadLetters: Send + Sync {
type Error: std::error::Error + Send + Sync + 'static;
fn list_dead(
&self,
query: InboxDeadQuery,
) -> impl Future<Output = Result<Vec<InboxRecord>, Self::Error>> + Send;
fn retry_dead(
&self,
ids: &[InboxRecordId],
) -> impl Future<Output = Result<u64, Self::Error>> + Send;
fn purge_dead(
&self,
ids: &[InboxRecordId],
) -> impl Future<Output = Result<u64, Self::Error>> + Send;
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(default, deny_unknown_fields))]
#[non_exhaustive]
pub struct InboxDeadQuery {
pub scope: Option<InboxScope>,
pub message_type: Option<String>,
#[cfg_attr(feature = "serde", serde(with = "time::serde::rfc3339::option"))]
pub dead_before: Option<OffsetDateTime>,
pub limit: u32,
pub after: Option<InboxDeadCursor>,
}
impl Default for InboxDeadQuery {
fn default() -> Self {
Self {
scope: None,
message_type: None,
dead_before: None,
limit: 100,
after: None,
}
}
}
impl InboxDeadQuery {
#[must_use]
pub fn scope(mut self, scope: InboxScope) -> Self {
self.scope = Some(scope);
self
}
#[must_use]
pub fn message_type(mut self, message_type: impl Into<String>) -> Self {
self.message_type = Some(message_type.into());
self
}
#[must_use]
pub const fn dead_before(mut self, dead_before: OffsetDateTime) -> Self {
self.dead_before = Some(dead_before);
self
}
#[must_use]
pub const fn limit(mut self, limit: u32) -> Self {
self.limit = limit;
self
}
#[must_use]
pub const fn after(mut self, after: InboxDeadCursor) -> Self {
self.after = Some(after);
self
}
}