1use bytes::BufMut;
4
5use crate::primitives::fixed::{get_bool, get_i16, get_i32, get_i8, put_bool, put_i16, put_i32, put_i8};
6use crate::primitives::string_bytes::{
7 compact_nullable_string_len, compact_string_len, nullable_string_len,
8 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
9 string_len,
10};
11use crate::primitives::string_bytes_borrowed::{
12 get_compact_nullable_string_borrowed, get_compact_string_borrowed,
13 get_nullable_string_borrowed, get_string_borrowed,
14};
15use crate::tagged_fields::{encode_to_bytes, read_tagged_fields, tagged_fields_len, WriteTaggedFields};
16use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
17
18pub const API_KEY: i16 = 19;
19pub const MIN_VERSION: i16 = 2;
20pub const MAX_VERSION: i16 = 7;
21pub const FLEXIBLE_MIN: i16 = 5;
22
23#[inline]
24fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct CreateTopicsResponse<'a> {
28 pub throttle_time_ms: i32,
29 pub topics: Vec<CreatableTopicResult<'a>>,
30 pub unknown_tagged_fields: UnknownTaggedFields,
31}
32
33impl<'a> Default for CreateTopicsResponse<'a> {
34 fn default() -> Self {
35 Self {
36 throttle_time_ms: 0i32,
37 topics: Vec::new(),
38 unknown_tagged_fields: Default::default(),
39 }
40 }
41}
42
43impl<'a> CreateTopicsResponse<'a> {
44 pub fn to_owned(&self) -> crate::owned::create_topics_response::CreateTopicsResponse {
45 crate::owned::create_topics_response::CreateTopicsResponse {
46 throttle_time_ms: (self.throttle_time_ms),
47 topics: (self.topics).iter().map(|it| it.to_owned()).collect(),
48 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
49 }
50 }
51}
52
53impl<'a> Encode for CreateTopicsResponse<'a> {
54 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
55 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
56 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
57 }
58 let flex = is_flexible(version);
59 if version >= 2 { put_i32(buf, self.throttle_time_ms) }
60 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { it.encode(buf, version)?; } } }
61 if flex {
62 let tagged = WriteTaggedFields::new();
63 tagged.write(buf, &self.unknown_tagged_fields);
64 }
65 Ok(())
66 }
67 fn encoded_len(&self, version: i16) -> usize {
68 let flex = is_flexible(version);
69 let mut n: usize = 0;
70 if version >= 2 { n += 4; }
71 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.topics).len(), flex); let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
72 if flex {
73 let known_pairs: Vec<(u32, usize)> = Vec::new();
74 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
75 }
76 n
77 }
78}
79
80impl<'de> DecodeBorrow<'de> for CreateTopicsResponse<'de> {
81 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
82 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
83 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
84 }
85 let flex = is_flexible(version);
86 let mut out = Self::default();
87 if version >= 2 { out.throttle_time_ms = get_i32(buf)?; }
88 if version >= 0 { out.topics = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(CreatableTopicResult::decode_borrow(buf, version)?); } v }; }
89 if flex {
90 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
91 Ok(false)
92 })?;
93 }
94 Ok(out)
95 }
96}
97
98#[derive(Debug, Clone, PartialEq, Eq)]
99pub struct CreatableTopicResult<'a> {
100 pub name: &'a str,
101 pub topic_id: crate::primitives::uuid::Uuid,
102 pub error_code: i16,
103 pub error_message: Option<&'a str>,
104 pub num_partitions: i32,
105 pub replication_factor: i16,
106 pub configs: Option<Vec<CreatableTopicConfigs<'a>>>,
107 pub topic_config_error_code: i16,
108 pub unknown_tagged_fields: UnknownTaggedFields,
109}
110
111impl<'a> Default for CreatableTopicResult<'a> {
112 fn default() -> Self {
113 Self {
114 name: "",
115 topic_id: Default::default(),
116 error_code: 0i16,
117 error_message: None,
118 num_partitions: -1i32,
119 replication_factor: -1i16,
120 configs: None,
121 topic_config_error_code: 0i16,
122 unknown_tagged_fields: Default::default(),
123 }
124 }
125}
126
127impl<'a> CreatableTopicResult<'a> {
128 pub fn to_owned(&self) -> crate::owned::create_topics_response::CreatableTopicResult {
129 crate::owned::create_topics_response::CreatableTopicResult {
130 name: (self.name).to_string(),
131 topic_id: (self.topic_id),
132 error_code: (self.error_code),
133 error_message: (self.error_message).map(|s| s.to_string()),
134 num_partitions: (self.num_partitions),
135 replication_factor: (self.replication_factor),
136 configs: (self.configs).as_ref().map(|v| v.iter().map(|it| it.to_owned()).collect()),
137 topic_config_error_code: (self.topic_config_error_code),
138 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
139 }
140 }
141}
142
143impl<'a> Encode for CreatableTopicResult<'a> {
144 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
145 let flex = version >= 5;
146 if version >= 0 { if flex { put_compact_string(buf, self.name) } else { put_string(buf, self.name) } }
147 if version >= 7 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
148 if version >= 0 { put_i16(buf, self.error_code) }
149 if version >= 1 { if flex { put_compact_nullable_string(buf, self.error_message) } else { put_nullable_string(buf, self.error_message) } }
150 if version >= 5 { put_i32(buf, self.num_partitions) }
151 if version >= 5 { put_i16(buf, self.replication_factor) }
152 if version >= 5 { { let len = (self.configs).as_ref().map(Vec::len); crate::primitives::array::put_nullable_array_len(buf, len, flex); if let Some(v) = &self.configs { for it in v { it.encode(buf, version)?; } } } }
153 if flex {
154 let mut tagged = WriteTaggedFields::new();
155 if !(crate::codegen_helpers::is_default(&self.topic_config_error_code)) {
156 let payload = encode_to_bytes(2, |b| { put_i16(b, self.topic_config_error_code); Ok(()) });
157 tagged.add(0, payload);
158 }
159 tagged.write(buf, &self.unknown_tagged_fields);
160 }
161 Ok(())
162 }
163 fn encoded_len(&self, version: i16) -> usize {
164 let flex = version >= 5;
165 let mut n: usize = 0;
166 if version >= 0 { n += if flex { compact_string_len(self.name) } else { string_len(self.name) }; }
167 if version >= 7 { n += 16; }
168 if version >= 0 { n += 2; }
169 if version >= 1 { n += if flex { compact_nullable_string_len(self.error_message) } else { nullable_string_len(self.error_message) }; }
170 if version >= 5 { n += 4; }
171 if version >= 5 { n += 2; }
172 if version >= 5 { n += { let opt: Option<&Vec<_>> = (self.configs).as_ref(); let prefix = crate::primitives::array::nullable_array_len_prefix_len(opt.map(|v| v.len()), flex); let body: usize = opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum()); prefix + body }; }
173 if flex {
174 let mut known_pairs: Vec<(u32, usize)> = Vec::new();
175 if !(crate::codegen_helpers::is_default(&self.topic_config_error_code)) {
176 known_pairs.push((0, 2));
177 }
178 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
179 }
180 n
181 }
182}
183
184impl<'de> DecodeBorrow<'de> for CreatableTopicResult<'de> {
185 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
186 let flex = version >= 5;
187 let mut out = Self::default();
188 if version >= 0 { out.name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
189 if version >= 7 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
190 if version >= 0 { out.error_code = get_i16(buf)?; }
191 if version >= 1 { out.error_message = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
192 if version >= 5 { out.num_partitions = get_i32(buf)?; }
193 if version >= 5 { out.replication_factor = get_i16(buf)?; }
194 if version >= 5 { out.configs = { let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?; match opt { None => None, Some(n) => { let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(CreatableTopicConfigs::decode_borrow(buf, version)?); } Some(v) } } }; }
195 if flex {
196 let mut tag_topic_config_error_code = None;
198 out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
199 match tag {
200 0 => { tag_topic_config_error_code = Some({ let b: &mut &[u8] = payload; get_i16(b)? }); Ok(true) }
201 _ => Ok(false),
202 }
203 })?;
204 if let Some(v) = tag_topic_config_error_code { out.topic_config_error_code = v; }
205 }
206 Ok(out)
207 }
208}
209
210#[derive(Debug, Clone, PartialEq, Eq)]
211pub struct CreatableTopicConfigs<'a> {
212 pub name: &'a str,
213 pub value: Option<&'a str>,
214 pub read_only: bool,
215 pub config_source: i8,
216 pub is_sensitive: bool,
217 pub unknown_tagged_fields: UnknownTaggedFields,
218}
219
220impl<'a> Default for CreatableTopicConfigs<'a> {
221 fn default() -> Self {
222 Self {
223 name: "",
224 value: None,
225 read_only: false,
226 config_source: -1i8,
227 is_sensitive: false,
228 unknown_tagged_fields: Default::default(),
229 }
230 }
231}
232
233impl<'a> CreatableTopicConfigs<'a> {
234 pub fn to_owned(&self) -> crate::owned::create_topics_response::CreatableTopicConfigs {
235 crate::owned::create_topics_response::CreatableTopicConfigs {
236 name: (self.name).to_string(),
237 value: (self.value).map(|s| s.to_string()),
238 read_only: (self.read_only),
239 config_source: (self.config_source),
240 is_sensitive: (self.is_sensitive),
241 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
242 }
243 }
244}
245
246impl<'a> Encode for CreatableTopicConfigs<'a> {
247 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
248 let flex = version >= 5;
249 if version >= 5 { if flex { put_compact_string(buf, self.name) } else { put_string(buf, self.name) } }
250 if version >= 5 { if flex { put_compact_nullable_string(buf, self.value) } else { put_nullable_string(buf, self.value) } }
251 if version >= 5 { put_bool(buf, self.read_only) }
252 if version >= 5 { put_i8(buf, self.config_source) }
253 if version >= 5 { put_bool(buf, self.is_sensitive) }
254 if flex {
255 let tagged = WriteTaggedFields::new();
256 tagged.write(buf, &self.unknown_tagged_fields);
257 }
258 Ok(())
259 }
260 fn encoded_len(&self, version: i16) -> usize {
261 let flex = version >= 5;
262 let mut n: usize = 0;
263 if version >= 5 { n += if flex { compact_string_len(self.name) } else { string_len(self.name) }; }
264 if version >= 5 { n += if flex { compact_nullable_string_len(self.value) } else { nullable_string_len(self.value) }; }
265 if version >= 5 { n += 1; }
266 if version >= 5 { n += 1; }
267 if version >= 5 { n += 1; }
268 if flex {
269 let known_pairs: Vec<(u32, usize)> = Vec::new();
270 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
271 }
272 n
273 }
274}
275
276impl<'de> DecodeBorrow<'de> for CreatableTopicConfigs<'de> {
277 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
278 let flex = version >= 5;
279 let mut out = Self::default();
280 if version >= 5 { out.name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
281 if version >= 5 { out.value = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
282 if version >= 5 { out.read_only = get_bool(buf)?; }
283 if version >= 5 { out.config_source = get_i8(buf)?; }
284 if version >= 5 { out.is_sensitive = get_bool(buf)?; }
285 if flex {
286 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
287 Ok(false)
288 })?;
289 }
290 Ok(out)
291 }
292}