use kafka_client_core::{
ConfigAlteration as CoreAlteration,
IncrementalConfigResourceAlteration as CoreResourceAlteration,
};
use super::{canonical_string, canonical_vec};
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum IncrementalConfigOperation {
Set(String),
Delete,
Append(String),
Subtract(String),
}
impl IncrementalConfigOperation {
fn canonicalize(self) -> Self {
match self {
Self::Set(value) => Self::Set(canonical_string(value)),
Self::Delete => Self::Delete,
Self::Append(value) => Self::Append(canonical_string(value)),
Self::Subtract(value) => Self::Subtract(canonical_string(value)),
}
}
fn value_bytes(&self) -> usize {
match self {
Self::Set(value) | Self::Append(value) | Self::Subtract(value) => value.len(),
Self::Delete => 0,
}
}
fn into_core(self, key: String) -> CoreAlteration {
match self {
Self::Set(value) => CoreAlteration::set(key, value),
Self::Delete => CoreAlteration::delete(key),
Self::Append(value) => CoreAlteration::append(key, value),
Self::Subtract(value) => CoreAlteration::subtract(key, value),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct IncrementalConfigAlteration {
key: String,
operation: IncrementalConfigOperation,
}
impl IncrementalConfigAlteration {
pub const fn new(key: String, operation: IncrementalConfigOperation) -> Self {
Self { key, operation }
}
fn canonicalize(mut self) -> Self {
self.key = canonical_string(self.key);
self.operation = self.operation.canonicalize();
self
}
fn text_bytes(&self) -> Option<usize> {
self.key.len().checked_add(self.operation.value_bytes())
}
fn into_core(self) -> CoreAlteration {
self.operation.into_core(self.key)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TopicConfigAlterations {
resource_type: i8,
resource_name: String,
alterations: Vec<IncrementalConfigAlteration>,
}
impl TopicConfigAlterations {
pub const fn new(topic: String, alterations: Vec<IncrementalConfigAlteration>) -> Self {
Self {
resource_type: 2,
resource_name: topic,
alterations,
}
}
pub const fn resource(
resource_type: i8,
resource_name: String,
alterations: Vec<IncrementalConfigAlteration>,
) -> Self {
Self {
resource_type,
resource_name,
alterations,
}
}
pub const fn resource_type(&self) -> i8 {
self.resource_type
}
pub fn resource_name(&self) -> &str {
&self.resource_name
}
pub(super) fn canonicalize(mut self) -> Self {
self.resource_name = canonical_string(self.resource_name);
self.alterations = canonical_vec(
self.alterations
.into_iter()
.map(IncrementalConfigAlteration::canonicalize)
.collect(),
);
self
}
pub(super) fn alteration_count(&self) -> usize {
self.alterations.len()
}
pub(super) fn text_bytes(&self) -> Option<usize> {
self.alterations
.iter()
.try_fold(self.resource_name.len(), |bytes, alteration| {
bytes.checked_add(alteration.text_bytes()?)
})
}
pub(super) fn resource_name_bytes(&self) -> usize {
self.resource_name.len()
}
pub(super) fn into_core(self) -> CoreResourceAlteration {
CoreResourceAlteration::resource(
self.resource_type,
self.resource_name,
self.alterations
.into_iter()
.map(IncrementalConfigAlteration::into_core)
.collect(),
)
}
#[cfg(test)]
pub(super) fn storage_is_canonical(&self) -> bool {
self.resource_name.capacity() == self.resource_name.len()
&& self.alterations.capacity() == self.alterations.len()
&& self
.alterations
.iter()
.all(|alteration| alteration.key.capacity() == alteration.key.len())
}
}
pub type IncrementalConfigResourceAlterations = TopicConfigAlterations;