use core::fmt;
use kafka_client_core::{
DeleteTopicResult as CoreTopicResult, DeleteTopicsFailureKind as CoreFailureKind,
DeleteTopicsTerminal, DeliveryStatus as CoreDeliveryStatus,
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DeleteTopicsDeliveryStatus {
NotSent,
PossiblySent,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DeleteTopicError {
code: i16,
message: Option<String>,
message_truncated: bool,
}
impl DeleteTopicError {
pub const fn code(&self) -> i16 {
self.code
}
pub fn message(&self) -> Option<&str> {
self.message.as_deref()
}
pub const fn message_truncated(&self) -> bool {
self.message_truncated
}
pub fn into_parts(self) -> (i16, Option<String>, bool) {
(self.code, self.message, self.message_truncated)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DeleteTopicResult {
topic: String,
result: Result<(), DeleteTopicError>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DeleteTopicIdResult {
topic_id: [u8; 16],
result: Result<(), DeleteTopicError>,
}
impl DeleteTopicIdResult {
pub const fn topic_id(&self) -> [u8; 16] {
self.topic_id
}
pub const fn result(&self) -> &Result<(), DeleteTopicError> {
&self.result
}
pub fn into_parts(self) -> ([u8; 16], Result<(), DeleteTopicError>) {
(self.topic_id, self.result)
}
}
impl DeleteTopicResult {
pub fn topic(&self) -> &str {
&self.topic
}
pub const fn result(&self) -> &Result<(), DeleteTopicError> {
&self.result
}
pub fn into_parts(self) -> (String, Result<(), DeleteTopicError>) {
(self.topic, self.result)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DeleteTopicsFailureKind {
DeadlineElapsed,
DriverRejected,
Transport,
InvalidResponse,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DeleteTopicsFailure {
kind: DeleteTopicsFailureKind,
delivery: DeleteTopicsDeliveryStatus,
}
impl DeleteTopicsFailure {
pub const fn kind(self) -> DeleteTopicsFailureKind {
self.kind
}
pub const fn delivery(self) -> DeleteTopicsDeliveryStatus {
self.delivery
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DeleteTopicsOutcome {
Topics(Vec<DeleteTopicResult>),
TopicIds(Vec<DeleteTopicIdResult>),
Failed(DeleteTopicsFailure),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DeleteTopicsObserverError {
AlreadyObserved,
Stale,
}
impl fmt::Display for DeleteTopicsObserverError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(match self {
Self::AlreadyObserved => "DeleteTopics result was already observed",
Self::Stale => "DeleteTopics observer is stale",
})
}
}
impl std::error::Error for DeleteTopicsObserverError {}
pub(crate) fn translate_terminal(terminal: DeleteTopicsTerminal) -> DeleteTopicsOutcome {
match terminal {
DeleteTopicsTerminal::Topics(outcomes) => DeleteTopicsOutcome::Topics(
outcomes
.into_iter()
.map(|outcome| {
let (topic, result) = outcome.into_parts();
let result = match result {
CoreTopicResult::Deleted => Ok(()),
CoreTopicResult::Failed(error) => {
let (code, message, message_truncated) = error.into_parts();
Err(DeleteTopicError {
code,
message,
message_truncated,
})
}
};
DeleteTopicResult { topic, result }
})
.collect(),
),
DeleteTopicsTerminal::TopicIds(outcomes) => DeleteTopicsOutcome::TopicIds(
outcomes
.into_iter()
.map(|outcome| {
let (topic_id, result) = outcome.into_parts();
let result = match result {
CoreTopicResult::Deleted => Ok(()),
CoreTopicResult::Failed(error) => {
let (code, message, message_truncated) = error.into_parts();
Err(DeleteTopicError {
code,
message,
message_truncated,
})
}
};
DeleteTopicIdResult { topic_id, result }
})
.collect(),
),
DeleteTopicsTerminal::Failed(failure) => {
let kind = match failure.kind() {
CoreFailureKind::DeadlineElapsed => DeleteTopicsFailureKind::DeadlineElapsed,
CoreFailureKind::DriverRejected => DeleteTopicsFailureKind::DriverRejected,
CoreFailureKind::Transport => DeleteTopicsFailureKind::Transport,
CoreFailureKind::InvalidResponse => DeleteTopicsFailureKind::InvalidResponse,
};
DeleteTopicsOutcome::Failed(DeleteTopicsFailure {
kind,
delivery: match failure.delivery() {
CoreDeliveryStatus::NotSent => DeleteTopicsDeliveryStatus::NotSent,
CoreDeliveryStatus::PossiblySent => DeleteTopicsDeliveryStatus::PossiblySent,
},
})
}
}
}