use core::fmt;
use kafka_client_core::{
DeliveryStatus as CoreDeliveryStatus, ListPartitionReassignmentsFailureKind as CoreFailureKind,
ListPartitionReassignmentsTerminal as CoreTerminal,
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ListPartitionReassignmentsDeliveryStatus {
NotSent,
PossiblySent,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PartitionReassignment {
replicas: Vec<i32>,
adding_replicas: Vec<i32>,
removing_replicas: Vec<i32>,
}
impl PartitionReassignment {
pub fn into_parts(self) -> (Vec<i32>, Vec<i32>, Vec<i32>) {
(self.replicas, self.adding_replicas, self.removing_replicas)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PartitionReassignmentResult {
topic: String,
partition: i32,
reassignment: PartitionReassignment,
}
impl PartitionReassignmentResult {
pub fn into_parts(self) -> (String, i32, PartitionReassignment) {
(self.topic, self.partition, self.reassignment)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ListPartitionReassignmentsBatch {
throttle_time_ms: u32,
reassignments: Vec<PartitionReassignmentResult>,
}
impl ListPartitionReassignmentsBatch {
pub fn into_parts(self) -> (u32, Vec<PartitionReassignmentResult>) {
(self.throttle_time_ms, self.reassignments)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ListPartitionReassignmentsBrokerError {
code: i16,
message: Option<String>,
message_truncated: bool,
}
impl ListPartitionReassignmentsBrokerError {
pub fn into_parts(self) -> (i16, Option<String>, bool) {
(self.code, self.message, self.message_truncated)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ListPartitionReassignmentsFailureKind {
DeadlineElapsed,
DriverRejected,
Transport,
Broker(ListPartitionReassignmentsBrokerError),
ResponseTooLarge,
Compatibility,
InvalidResponse,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ListPartitionReassignmentsFailure {
kind: ListPartitionReassignmentsFailureKind,
delivery: ListPartitionReassignmentsDeliveryStatus,
}
impl ListPartitionReassignmentsFailure {
pub fn into_parts(
self,
) -> (
ListPartitionReassignmentsFailureKind,
ListPartitionReassignmentsDeliveryStatus,
) {
(self.kind, self.delivery)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ListPartitionReassignmentsOutcome {
Reassignments(ListPartitionReassignmentsBatch),
Failed(ListPartitionReassignmentsFailure),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ListPartitionReassignmentsObserverError {
AlreadyObserved,
Stale,
}
impl fmt::Display for ListPartitionReassignmentsObserverError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(match self {
Self::AlreadyObserved => "ListPartitionReassignments result was already observed",
Self::Stale => "ListPartitionReassignments observer is stale",
})
}
}
impl std::error::Error for ListPartitionReassignmentsObserverError {}
pub(crate) fn translate_terminal(terminal: CoreTerminal) -> ListPartitionReassignmentsOutcome {
match terminal {
CoreTerminal::Reassignments(batch) => {
let (throttle_time_ms, reassignments) = batch.into_parts();
ListPartitionReassignmentsOutcome::Reassignments(ListPartitionReassignmentsBatch {
throttle_time_ms,
reassignments: reassignments
.into_iter()
.map(|outcome| {
let (topic, partition, reassignment) = outcome.into_parts();
let (replicas, adding_replicas, removing_replicas) =
reassignment.into_parts();
PartitionReassignmentResult {
topic,
partition,
reassignment: PartitionReassignment {
replicas,
adding_replicas,
removing_replicas,
},
}
})
.collect(),
})
}
CoreTerminal::Failed(failure) => {
ListPartitionReassignmentsOutcome::Failed(ListPartitionReassignmentsFailure {
kind: failure_kind(failure.kind().clone()),
delivery: delivery(failure.delivery()),
})
}
}
}
fn failure_kind(kind: CoreFailureKind) -> ListPartitionReassignmentsFailureKind {
match kind {
CoreFailureKind::DeadlineElapsed => ListPartitionReassignmentsFailureKind::DeadlineElapsed,
CoreFailureKind::DriverRejected => ListPartitionReassignmentsFailureKind::DriverRejected,
CoreFailureKind::Transport => ListPartitionReassignmentsFailureKind::Transport,
CoreFailureKind::Broker(error) => {
ListPartitionReassignmentsFailureKind::Broker(ListPartitionReassignmentsBrokerError {
code: error.code(),
message: error.message().map(str::to_owned),
message_truncated: error.message_truncated(),
})
}
CoreFailureKind::ResponseTooLarge => {
ListPartitionReassignmentsFailureKind::ResponseTooLarge
}
CoreFailureKind::Compatibility => ListPartitionReassignmentsFailureKind::Compatibility,
CoreFailureKind::InvalidResponse => ListPartitionReassignmentsFailureKind::InvalidResponse,
}
}
const fn delivery(status: CoreDeliveryStatus) -> ListPartitionReassignmentsDeliveryStatus {
match status {
CoreDeliveryStatus::NotSent => ListPartitionReassignmentsDeliveryStatus::NotSent,
CoreDeliveryStatus::PossiblySent => ListPartitionReassignmentsDeliveryStatus::PossiblySent,
}
}