Skip to main content

kafka_client_engine/admin/legacy_alter_configs/model/
resource.rs

1//! Canonical engine-owned legacy configuration resource snapshots.
2
3use kafka_client_core::{
4    LegacyConfigEntry as CoreConfigEntry,
5    LegacyConfigResourceReplacement as CoreResourceReplacement,
6};
7
8use super::{canonical_string, canonical_vec};
9
10/// One exact nullable configuration entry in a legacy replacement snapshot.
11#[derive(Clone, Debug, Eq, PartialEq)]
12pub struct LegacyConfigEntry {
13    key: String,
14    value: Option<String>,
15}
16
17impl LegacyConfigEntry {
18    /// Creates one raw entry for validation at admission.
19    pub const fn new(key: String, value: Option<String>) -> Self {
20        Self { key, value }
21    }
22
23    fn canonicalize(mut self) -> Self {
24        self.key = canonical_string(self.key);
25        self.value = self.value.map(canonical_string);
26        self
27    }
28
29    fn text_bytes(&self) -> Option<usize> {
30        self.key
31            .len()
32            .checked_add(self.value.as_ref().map_or(0, String::len))
33    }
34
35    fn into_core(self) -> CoreConfigEntry {
36        CoreConfigEntry::new(self.key, self.value)
37    }
38}
39
40/// One Kafka resource and its complete caller-ordered configuration snapshot.
41#[derive(Clone, Debug, Eq, PartialEq)]
42pub struct LegacyTopicConfigReplacement {
43    resource_type: i8,
44    resource_name: String,
45    configs: Vec<LegacyConfigEntry>,
46}
47
48impl LegacyTopicConfigReplacement {
49    /// Creates one raw topic snapshot; an empty snapshot clears dynamic values.
50    pub const fn new(topic: String, configs: Vec<LegacyConfigEntry>) -> Self {
51        Self {
52            resource_type: 2,
53            resource_name: topic,
54            configs,
55        }
56    }
57
58    /// Creates one exact raw resource snapshot for admission-time validation.
59    pub const fn resource(
60        resource_type: i8,
61        resource_name: String,
62        configs: Vec<LegacyConfigEntry>,
63    ) -> Self {
64        Self {
65            resource_type,
66            resource_name,
67            configs,
68        }
69    }
70
71    /// Returns Kafka's exact resource-type code.
72    pub const fn resource_type(&self) -> i8 {
73        self.resource_type
74    }
75
76    /// Returns the resource name.
77    pub fn resource_name(&self) -> &str {
78        &self.resource_name
79    }
80
81    pub(super) fn canonicalize(mut self) -> Self {
82        self.resource_name = canonical_string(self.resource_name);
83        self.configs = canonical_vec(
84            self.configs
85                .into_iter()
86                .map(LegacyConfigEntry::canonicalize)
87                .collect(),
88        );
89        self
90    }
91
92    pub(super) fn config_count(&self) -> usize {
93        self.configs.len()
94    }
95
96    pub(super) fn text_bytes(&self) -> Option<usize> {
97        self.configs
98            .iter()
99            .try_fold(self.resource_name.len(), |bytes, config| {
100                bytes.checked_add(config.text_bytes()?)
101            })
102    }
103
104    pub(super) fn resource_name_bytes(&self) -> usize {
105        self.resource_name.len()
106    }
107
108    pub(super) fn into_core(self) -> CoreResourceReplacement {
109        CoreResourceReplacement::resource(
110            self.resource_type,
111            self.resource_name,
112            self.configs
113                .into_iter()
114                .map(LegacyConfigEntry::into_core)
115                .collect(),
116        )
117    }
118
119    #[cfg(test)]
120    pub(super) fn storage_is_canonical(&self) -> bool {
121        self.resource_name.capacity() == self.resource_name.len()
122            && self.configs.capacity() == self.configs.len()
123            && self.configs.iter().all(|config| {
124                config.key.capacity() == config.key.len()
125                    && config
126                        .value
127                        .as_ref()
128                        .is_none_or(|value| value.capacity() == value.len())
129            })
130    }
131}
132
133/// Resource-generic name for one exact type/name replacement snapshot.
134pub type LegacyConfigResourceReplacement = LegacyTopicConfigReplacement;