use core::fmt;
use kafka_client_core::{
DeleteRecordsFailureKind as CoreFailureKind, DeleteRecordsResult as CoreResult,
DeleteRecordsTerminal as CoreTerminal, DeliveryStatus as CoreDeliveryStatus,
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DeleteRecordsDeliveryStatus {
NotSent,
PossiblySent,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DeleteRecordsEngineBrokerError {
code: i16,
}
impl DeleteRecordsEngineBrokerError {
pub const fn code(self) -> i16 {
self.code
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DeleteRecordsDescription {
low_watermark: i64,
}
impl DeleteRecordsDescription {
pub const fn low_watermark(self) -> i64 {
self.low_watermark
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DeleteRecordsEngineResult {
topic: String,
partition: i32,
result: Result<DeleteRecordsDescription, DeleteRecordsEngineBrokerError>,
}
impl DeleteRecordsEngineResult {
pub fn into_parts(
self,
) -> (
String,
i32,
Result<DeleteRecordsDescription, DeleteRecordsEngineBrokerError>,
) {
(self.topic, self.partition, self.result)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DeleteRecordsEngineBatch {
throttle_time_ms: u32,
records: Vec<DeleteRecordsEngineResult>,
}
impl DeleteRecordsEngineBatch {
pub fn into_parts(self) -> (u32, Vec<DeleteRecordsEngineResult>) {
(self.throttle_time_ms, self.records)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DeleteRecordsFailureKind {
DeadlineElapsed,
DriverRejected,
Transport,
ResponseTooLarge,
Compatibility,
InvalidResponse,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DeleteRecordsFailure {
kind: DeleteRecordsFailureKind,
delivery: DeleteRecordsDeliveryStatus,
throttle_time_ms: u32,
completed: Vec<DeleteRecordsEngineResult>,
failed_target: super::DeleteRecordsRequestTarget,
unattempted: Vec<super::DeleteRecordsRequestTarget>,
}
impl DeleteRecordsFailure {
pub const fn kind(&self) -> DeleteRecordsFailureKind {
self.kind
}
pub const fn delivery(&self) -> DeleteRecordsDeliveryStatus {
self.delivery
}
pub fn into_parts(
self,
) -> (
DeleteRecordsFailureKind,
DeleteRecordsDeliveryStatus,
u32,
Vec<DeleteRecordsEngineResult>,
super::DeleteRecordsRequestTarget,
Vec<super::DeleteRecordsRequestTarget>,
) {
(
self.kind,
self.delivery,
self.throttle_time_ms,
self.completed,
self.failed_target,
self.unattempted,
)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DeleteRecordsOutcome {
Deleted(DeleteRecordsEngineBatch),
Failed(DeleteRecordsFailure),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DeleteRecordsObserverError {
AlreadyObserved,
Stale,
}
impl fmt::Display for DeleteRecordsObserverError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(match self {
Self::AlreadyObserved => "Admin DeleteRecords result was already observed",
Self::Stale => "Admin DeleteRecords observer is stale",
})
}
}
impl std::error::Error for DeleteRecordsObserverError {}
pub(crate) fn translate_terminal(terminal: CoreTerminal) -> DeleteRecordsOutcome {
match terminal {
CoreTerminal::Deleted(batch) => {
let (throttle_time_ms, outcomes) = batch.into_parts();
DeleteRecordsOutcome::Deleted(DeleteRecordsEngineBatch {
throttle_time_ms,
records: outcomes.into_iter().map(translate_result).collect(),
})
}
CoreTerminal::Failed(failure) => {
let (kind, delivery_status, throttle_time_ms, completed, failed, unattempted) =
failure.into_parts();
DeleteRecordsOutcome::Failed(DeleteRecordsFailure {
kind: failure_kind(kind),
delivery: delivery(delivery_status),
throttle_time_ms,
completed: completed.into_iter().map(translate_result).collect(),
failed_target: translate_target(&failed),
unattempted: unattempted.iter().map(translate_target).collect(),
})
}
}
}
fn translate_result(outcome: kafka_client_core::DeleteRecordsOutcome) -> DeleteRecordsEngineResult {
let (topic, partition, result) = outcome.into_parts();
let result = match result {
CoreResult::Deleted(value) => Ok(DeleteRecordsDescription {
low_watermark: value.low_watermark(),
}),
CoreResult::Failed(error) => Err(DeleteRecordsEngineBrokerError { code: error.code() }),
};
DeleteRecordsEngineResult {
topic,
partition,
result,
}
}
fn translate_target(
target: &kafka_client_core::DeleteRecordsTarget,
) -> super::DeleteRecordsRequestTarget {
super::DeleteRecordsRequestTarget::new(
target.topic().to_owned(),
target.partition(),
target.before_offset(),
)
}
const fn failure_kind(kind: CoreFailureKind) -> DeleteRecordsFailureKind {
match kind {
CoreFailureKind::DeadlineElapsed => DeleteRecordsFailureKind::DeadlineElapsed,
CoreFailureKind::DriverRejected => DeleteRecordsFailureKind::DriverRejected,
CoreFailureKind::Transport => DeleteRecordsFailureKind::Transport,
CoreFailureKind::ResponseTooLarge => DeleteRecordsFailureKind::ResponseTooLarge,
CoreFailureKind::Compatibility => DeleteRecordsFailureKind::Compatibility,
CoreFailureKind::InvalidResponse => DeleteRecordsFailureKind::InvalidResponse,
}
}
const fn delivery(status: CoreDeliveryStatus) -> DeleteRecordsDeliveryStatus {
match status {
CoreDeliveryStatus::NotSent => DeleteRecordsDeliveryStatus::NotSent,
CoreDeliveryStatus::PossiblySent => DeleteRecordsDeliveryStatus::PossiblySent,
}
}