kafka_client_engine/admin/legacy_alter_configs/model/
resource.rs1use kafka_client_core::{
4 LegacyConfigEntry as CoreConfigEntry,
5 LegacyConfigResourceReplacement as CoreResourceReplacement,
6};
7
8use super::{canonical_string, canonical_vec};
9
10#[derive(Clone, Debug, Eq, PartialEq)]
12pub struct LegacyConfigEntry {
13 key: String,
14 value: Option<String>,
15}
16
17impl LegacyConfigEntry {
18 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#[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 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 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 pub const fn resource_type(&self) -> i8 {
73 self.resource_type
74 }
75
76 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
133pub type LegacyConfigResourceReplacement = LegacyTopicConfigReplacement;