use kafka_client_core::{
UpdateFeature as CoreFeature, UpdateFeatureIntent as CoreIntent,
UpdateFeaturesPlan as CorePlan, UpdateFeaturesPlanError as CorePlanError,
};
pub(crate) enum UpdateFeaturesPlanFailure {
Invalid,
RetainedBytes,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum UpdateFeatureIntent {
Upgrade,
SafeDowngrade,
UnsafeDowngrade,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UpdateFeature {
feature: String,
max_version_level: i16,
intent: UpdateFeatureIntent,
}
impl UpdateFeature {
pub const fn new(feature: String, max_version_level: i16, intent: UpdateFeatureIntent) -> Self {
Self {
feature,
max_version_level,
intent,
}
}
pub fn feature(&self) -> &str {
&self.feature
}
pub const fn max_version_level(&self) -> i16 {
self.max_version_level
}
pub const fn intent(&self) -> UpdateFeatureIntent {
self.intent
}
fn to_core(&self) -> CoreFeature {
CoreFeature::new(
self.feature.clone().into_boxed_str().into_string(),
self.max_version_level,
match self.intent {
UpdateFeatureIntent::Upgrade => CoreIntent::Upgrade,
UpdateFeatureIntent::SafeDowngrade => CoreIntent::SafeDowngrade,
UpdateFeatureIntent::UnsafeDowngrade => CoreIntent::UnsafeDowngrade,
},
)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UpdateFeaturesRequest {
updates: Vec<UpdateFeature>,
validate_only: bool,
}
impl UpdateFeaturesRequest {
pub const fn new(updates: Vec<UpdateFeature>, validate_only: bool) -> Self {
Self {
updates,
validate_only,
}
}
pub fn updates(&self) -> &[UpdateFeature] {
&self.updates
}
pub const fn validate_only(&self) -> bool {
self.validate_only
}
pub(crate) fn plan(&self) -> Result<CorePlan, UpdateFeaturesPlanFailure> {
let mut updates = Vec::new();
updates
.try_reserve_exact(self.updates.len())
.map_err(|_| UpdateFeaturesPlanFailure::RetainedBytes)?;
updates.extend(self.updates.iter().map(UpdateFeature::to_core));
CorePlan::new(updates, self.validate_only)
.map_err(|_error: CorePlanError| UpdateFeaturesPlanFailure::Invalid)
}
}