use kafka_client_core::{
AdminListOffsetSpec, AdminListOffsetTarget, AdminListOffsetsPlan, AdminListOffsetsPlanError,
};
use crate::config::ConsumerReadIsolation;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AdminListOffsetsRequestSpec {
Earliest,
Latest,
MaxTimestamp,
EarliestLocal,
LatestTiered,
EarliestPendingUpload,
Timestamp(i64),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AdminListOffsetsRequestTarget {
topic: String,
partition: i32,
spec: AdminListOffsetsRequestSpec,
current_leader_epoch: Option<i32>,
}
impl AdminListOffsetsRequestTarget {
pub const fn new(topic: String, partition: i32, spec: AdminListOffsetsRequestSpec) -> Self {
Self {
topic,
partition,
spec,
current_leader_epoch: None,
}
}
pub const fn current_leader_epoch(mut self, current_leader_epoch: i32) -> Self {
self.current_leader_epoch = Some(current_leader_epoch);
self
}
fn canonicalize(mut self) -> Self {
self.topic = self.topic.into_boxed_str().into_string();
self
}
fn into_core(self) -> AdminListOffsetTarget {
let spec = match self.spec {
AdminListOffsetsRequestSpec::Earliest => AdminListOffsetSpec::Earliest,
AdminListOffsetsRequestSpec::Latest => AdminListOffsetSpec::Latest,
AdminListOffsetsRequestSpec::MaxTimestamp => AdminListOffsetSpec::MaxTimestamp,
AdminListOffsetsRequestSpec::EarliestLocal => AdminListOffsetSpec::EarliestLocal,
AdminListOffsetsRequestSpec::LatestTiered => AdminListOffsetSpec::LatestTiered,
AdminListOffsetsRequestSpec::EarliestPendingUpload => {
AdminListOffsetSpec::EarliestPendingUpload
}
AdminListOffsetsRequestSpec::Timestamp(timestamp) => {
AdminListOffsetSpec::Timestamp(timestamp)
}
};
AdminListOffsetTarget::new(self.topic, self.partition, spec)
.with_current_leader_epoch(self.current_leader_epoch)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AdminListOffsetsRequest {
targets: Vec<AdminListOffsetsRequestTarget>,
read_isolation: ConsumerReadIsolation,
}
impl AdminListOffsetsRequest {
pub const fn new(targets: Vec<AdminListOffsetsRequestTarget>) -> Self {
Self {
targets,
read_isolation: ConsumerReadIsolation::ReadUncommitted,
}
}
pub const fn with_read_isolation(mut self, read_isolation: ConsumerReadIsolation) -> Self {
self.read_isolation = read_isolation;
self
}
pub(crate) fn canonicalize(mut self) -> Self {
self.targets = self
.targets
.into_iter()
.map(AdminListOffsetsRequestTarget::canonicalize)
.collect();
self.targets.shrink_to_fit();
self
}
pub(crate) fn into_plan(self) -> Result<AdminListOffsetsPlan, AdminListOffsetsPlanError> {
AdminListOffsetsPlan::with_read_isolation(
self.targets
.into_iter()
.map(AdminListOffsetsRequestTarget::into_core)
.collect(),
self.read_isolation.core(),
)
}
}