1use bytes::BufMut;
3
4use crate::{
5 DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6 primitives::{
7 fixed::{get_bool, get_i8, get_i16, get_i32, put_bool, put_i8, put_i16, put_i32},
8 string_bytes::{
9 compact_nullable_string_len, compact_string_len, nullable_string_len,
10 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
11 string_len,
12 },
13 string_bytes_borrowed::{
14 get_compact_nullable_string_borrowed, get_compact_string_borrowed,
15 get_nullable_string_borrowed, get_string_borrowed,
16 },
17 },
18 tagged_fields::{WriteTaggedFields, encode_to_bytes, read_tagged_fields, tagged_fields_len},
19};
20pub const API_KEY: i16 = 19;
21pub const MIN_VERSION: i16 = 2;
22pub const MAX_VERSION: i16 = 7;
23pub const FLEXIBLE_MIN: i16 = 5;
24#[inline]
25#[must_use]
26pub fn is_flexible(version: i16) -> bool {
27 version >= FLEXIBLE_MIN
28}
29#[derive(Debug, Clone, PartialEq, Eq, Default)]
30pub struct CreateTopicsResponse<'a> {
31 pub throttle_time_ms: i32,
32 pub topics: Vec<CreatableTopicResult<'a>>,
33 pub unknown_tagged_fields: UnknownTaggedFields,
34}
35impl CreateTopicsResponse<'_> {
36 #[must_use]
40 pub fn to_owned(&self) -> crate::owned::create_topics_response::CreateTopicsResponse {
41 crate::owned::create_topics_response::CreateTopicsResponse {
42 throttle_time_ms: (self.throttle_time_ms),
43 topics: (self.topics)
44 .iter()
45 .map(CreatableTopicResult::to_owned)
46 .collect(),
47 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
48 }
49 }
50}
51impl Encode for CreateTopicsResponse<'_> {
52 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
53 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
54 return Err(ProtocolError::UnsupportedVersion {
55 api_key: API_KEY,
56 version,
57 });
58 }
59 let flex = is_flexible(version);
60 if version >= 2 {
61 put_i32(buf, self.throttle_time_ms);
62 }
63 if version >= 0 {
64 {
65 crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
66 for it in &self.topics {
67 it.encode(buf, version)?;
68 }
69 };
70 }
71 if flex {
72 let tagged = WriteTaggedFields::new();
73 tagged.write(buf, &self.unknown_tagged_fields);
74 }
75 Ok(())
76 }
77 fn encoded_len(&self, version: i16) -> usize {
78 let flex = is_flexible(version);
79 let mut n: usize = 0;
80 if version >= 2 {
81 n += 4;
82 }
83 if version >= 0 {
84 n += {
85 let prefix =
86 crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
87 let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
88 prefix + body
89 };
90 }
91 if flex {
92 let known_pairs: Vec<(u32, usize)> = Vec::new();
93 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
94 }
95 n
96 }
97}
98impl<'de> DecodeBorrow<'de> for CreateTopicsResponse<'de> {
99 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
100 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
101 return Err(ProtocolError::UnsupportedVersion {
102 api_key: API_KEY,
103 version,
104 });
105 }
106 let flex = is_flexible(version);
107 let mut out = Self::default();
108 if version >= 2 {
109 out.throttle_time_ms = get_i32(buf)?;
110 }
111 if version >= 0 {
112 out.topics = {
113 let n = crate::primitives::array::get_array_len(buf, flex)?;
114 let mut v = Vec::with_capacity(n);
115 for _ in 0..n {
116 v.push(CreatableTopicResult::decode_borrow(buf, version)?);
117 }
118 v
119 };
120 }
121 if flex {
122 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
123 }
124 Ok(out)
125 }
126}
127#[cfg(test)]
128impl CreateTopicsResponse<'_> {
129 #[must_use]
130 pub fn populated(version: i16) -> Self {
131 let mut m = Self::default();
132 if version >= 2 {
133 m.throttle_time_ms = 1i32;
134 }
135 if version >= 0 {
136 m.topics = vec![CreatableTopicResult::populated(version)];
137 }
138 m
139 }
140}
141#[derive(Debug, Clone, PartialEq, Eq)]
142pub struct CreatableTopicResult<'a> {
143 pub name: &'a str,
144 pub topic_id: crate::primitives::uuid::Uuid,
145 pub error_code: i16,
146 pub error_message: Option<&'a str>,
147 pub num_partitions: i32,
148 pub replication_factor: i16,
149 pub configs: Option<Vec<CreatableTopicConfigs<'a>>>,
150 pub topic_config_error_code: i16,
151 pub unknown_tagged_fields: UnknownTaggedFields,
152}
153impl Default for CreatableTopicResult<'_> {
154 fn default() -> Self {
155 Self {
156 name: "",
157 topic_id: crate::primitives::uuid::Uuid::default(),
158 error_code: 0i16,
159 error_message: Some(""),
160 num_partitions: -1i32,
161 replication_factor: -1i16,
162 configs: Some(Vec::new()),
163 topic_config_error_code: 0i16,
164 unknown_tagged_fields: UnknownTaggedFields::default(),
165 }
166 }
167}
168impl<'a> CreatableTopicResult<'a> {
169 #[must_use]
173 pub fn to_owned(&self) -> crate::owned::create_topics_response::CreatableTopicResult {
174 crate::owned::create_topics_response::CreatableTopicResult {
175 name: (self.name).to_string(),
176 topic_id: (self.topic_id),
177 error_code: (self.error_code),
178 error_message: (self.error_message).map(std::string::ToString::to_string),
179 num_partitions: (self.num_partitions),
180 replication_factor: (self.replication_factor),
181 configs: (self.configs)
182 .as_ref()
183 .map(|v| v.iter().map(CreatableTopicConfigs::to_owned).collect()),
184 topic_config_error_code: (self.topic_config_error_code),
185 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
186 }
187 }
188 fn encode_field_0<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
189 if version >= 0 {
190 if flex {
191 let () = put_compact_string(buf, self.name);
192 } else {
193 let () = put_string(buf, self.name);
194 }
195 }
196 }
197 fn encode_field_1<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
198 if version >= 7 {
199 crate::primitives::uuid::put_uuid(buf, self.topic_id);
200 }
201 }
202 fn encode_field_2<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
203 if version >= 0 {
204 put_i16(buf, self.error_code);
205 }
206 }
207 fn encode_field_3<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
208 if version >= 1 {
209 if flex {
210 let () = put_compact_nullable_string(buf, self.error_message);
211 } else {
212 let () = put_nullable_string(buf, self.error_message);
213 }
214 }
215 }
216 fn encode_field_4<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
217 if version >= 5 {
218 put_i32(buf, self.num_partitions);
219 }
220 }
221 fn encode_field_5<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
222 if version >= 5 {
223 put_i16(buf, self.replication_factor);
224 }
225 }
226 fn encode_field_6<B: BufMut>(
227 &self,
228 buf: &mut B,
229 version: i16,
230 flex: bool,
231 ) -> Result<(), ProtocolError> {
232 if version >= 5 {
233 {
234 let len = (self.configs).as_ref().map(Vec::len);
235 crate::primitives::array::put_nullable_array_len(buf, len, flex);
236 if let Some(v) = &self.configs {
237 for it in v {
238 it.encode(buf, version)?;
239 }
240 }
241 };
242 }
243 Ok(())
244 }
245 fn encode_tagged_fields<B: BufMut>(&self, buf: &mut B, _version: i16, flex: bool) {
246 if flex {
247 let mut tagged = WriteTaggedFields::new();
248 if !(crate::codegen_helpers::is_default(&self.topic_config_error_code)) {
249 let payload = encode_to_bytes(2, |b| {
250 put_i16(b, self.topic_config_error_code);
251 Ok(())
252 });
253 tagged.add(0, payload);
254 }
255 tagged.write(buf, &self.unknown_tagged_fields);
256 }
257 }
258 fn decode_field_0(
259 out: &mut Self,
260 buf: &mut &'a [u8],
261 version: i16,
262 flex: bool,
263 ) -> Result<(), ProtocolError> {
264 if version >= 0 {
265 out.name = if flex {
266 get_compact_string_borrowed(buf)?
267 } else {
268 get_string_borrowed(buf)?
269 };
270 }
271 Ok(())
272 }
273 fn decode_field_1(
274 out: &mut Self,
275 buf: &mut &'a [u8],
276 version: i16,
277 _flex: bool,
278 ) -> Result<(), ProtocolError> {
279 if version >= 7 {
280 out.topic_id = crate::primitives::uuid::get_uuid(buf)?;
281 }
282 Ok(())
283 }
284 fn decode_field_2(
285 out: &mut Self,
286 buf: &mut &'a [u8],
287 version: i16,
288 _flex: bool,
289 ) -> Result<(), ProtocolError> {
290 if version >= 0 {
291 out.error_code = get_i16(buf)?;
292 }
293 Ok(())
294 }
295 fn decode_field_3(
296 out: &mut Self,
297 buf: &mut &'a [u8],
298 version: i16,
299 flex: bool,
300 ) -> Result<(), ProtocolError> {
301 if version >= 1 {
302 out.error_message = if flex {
303 get_compact_nullable_string_borrowed(buf)?
304 } else {
305 get_nullable_string_borrowed(buf)?
306 };
307 }
308 Ok(())
309 }
310 fn decode_field_4(
311 out: &mut Self,
312 buf: &mut &'a [u8],
313 version: i16,
314 _flex: bool,
315 ) -> Result<(), ProtocolError> {
316 if version >= 5 {
317 out.num_partitions = get_i32(buf)?;
318 }
319 Ok(())
320 }
321 fn decode_field_5(
322 out: &mut Self,
323 buf: &mut &'a [u8],
324 version: i16,
325 _flex: bool,
326 ) -> Result<(), ProtocolError> {
327 if version >= 5 {
328 out.replication_factor = get_i16(buf)?;
329 }
330 Ok(())
331 }
332 fn decode_field_6(
333 out: &mut Self,
334 buf: &mut &'a [u8],
335 version: i16,
336 flex: bool,
337 ) -> Result<(), ProtocolError> {
338 if version >= 5 {
339 out.configs = {
340 let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?;
341 match opt {
342 None => None,
343 Some(n) => {
344 let mut v = Vec::with_capacity(n);
345 for _ in 0..n {
346 v.push(CreatableTopicConfigs::decode_borrow(buf, version)?);
347 }
348 Some(v)
349 }
350 }
351 };
352 }
353 Ok(())
354 }
355 fn decode_tagged_fields(
356 out: &mut Self,
357 buf: &mut &'a [u8],
358 _version: i16,
359 flex: bool,
360 ) -> Result<(), ProtocolError> {
361 if flex {
362 let mut tag_topic_config_error_code = None;
363 out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| match tag {
364 0 => {
365 tag_topic_config_error_code = Some({
366 let b: &mut &[u8] = payload;
367 get_i16(b)?
368 });
369 Ok(true)
370 }
371 _ => Ok(false),
372 })?;
373 if let Some(v) = tag_topic_config_error_code {
374 out.topic_config_error_code = v;
375 }
376 }
377 Ok(())
378 }
379}
380impl Encode for CreatableTopicResult<'_> {
381 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
382 let flex = version >= 5;
383 self.encode_field_0(buf, version, flex);
384 self.encode_field_1(buf, version, flex);
385 self.encode_field_2(buf, version, flex);
386 self.encode_field_3(buf, version, flex);
387 self.encode_field_4(buf, version, flex);
388 self.encode_field_5(buf, version, flex);
389 self.encode_field_6(buf, version, flex)?;
390 self.encode_tagged_fields(buf, version, flex);
391 Ok(())
392 }
393 fn encoded_len(&self, version: i16) -> usize {
394 let flex = version >= 5;
395 let mut n: usize = 0;
396 if version >= 0 {
397 n += if flex {
398 compact_string_len(self.name)
399 } else {
400 string_len(self.name)
401 };
402 }
403 if version >= 7 {
404 n += 16;
405 }
406 if version >= 0 {
407 n += 2;
408 }
409 if version >= 1 {
410 n += if flex {
411 compact_nullable_string_len(self.error_message)
412 } else {
413 nullable_string_len(self.error_message)
414 };
415 }
416 if version >= 5 {
417 n += 4;
418 }
419 if version >= 5 {
420 n += 2;
421 }
422 if version >= 5 {
423 n += {
424 let opt: Option<&Vec<_>> = (self.configs).as_ref();
425 let prefix = crate::primitives::array::nullable_array_len_prefix_len(
426 opt.map(std::vec::Vec::len),
427 flex,
428 );
429 let body: usize =
430 opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum());
431 prefix + body
432 };
433 }
434 if flex {
435 let mut known_pairs: Vec<(u32, usize)> = Vec::new();
436 if !(crate::codegen_helpers::is_default(&self.topic_config_error_code)) {
437 known_pairs.push((0, 2));
438 }
439 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
440 }
441 n
442 }
443}
444impl<'de> DecodeBorrow<'de> for CreatableTopicResult<'de> {
445 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
446 let flex = version >= 5;
447 let mut out = Self::default();
448 Self::decode_field_0(&mut out, buf, version, flex)?;
449 Self::decode_field_1(&mut out, buf, version, flex)?;
450 Self::decode_field_2(&mut out, buf, version, flex)?;
451 Self::decode_field_3(&mut out, buf, version, flex)?;
452 Self::decode_field_4(&mut out, buf, version, flex)?;
453 Self::decode_field_5(&mut out, buf, version, flex)?;
454 Self::decode_field_6(&mut out, buf, version, flex)?;
455 Self::decode_tagged_fields(&mut out, buf, version, flex)?;
456 Ok(out)
457 }
458}
459#[cfg(test)]
460impl CreatableTopicResult<'_> {
461 #[must_use]
462 pub fn populated(version: i16) -> Self {
463 let mut m = Self::default();
464 if version >= 0 {
465 m.name = "x";
466 }
467 if version >= 7 {
468 m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
469 }
470 if version >= 0 {
471 m.error_code = 1i16;
472 }
473 if version >= 1 {
474 m.error_message = Some("x");
475 }
476 if version >= 5 {
477 m.num_partitions = 1i32;
478 }
479 if version >= 5 {
480 m.replication_factor = 1i16;
481 }
482 if version >= 5 {
483 m.configs = Some(vec![CreatableTopicConfigs::populated(version)]);
484 }
485 if version >= 5 {
486 m.topic_config_error_code = 1i16;
487 }
488 m
489 }
490}
491#[derive(Debug, Clone, PartialEq, Eq)]
492pub struct CreatableTopicConfigs<'a> {
493 pub name: &'a str,
494 pub value: Option<&'a str>,
495 pub read_only: bool,
496 pub config_source: i8,
497 pub is_sensitive: bool,
498 pub unknown_tagged_fields: UnknownTaggedFields,
499}
500impl Default for CreatableTopicConfigs<'_> {
501 fn default() -> Self {
502 Self {
503 name: "",
504 value: Some(""),
505 read_only: false,
506 config_source: -1i8,
507 is_sensitive: false,
508 unknown_tagged_fields: UnknownTaggedFields::default(),
509 }
510 }
511}
512impl CreatableTopicConfigs<'_> {
513 #[must_use]
517 pub fn to_owned(&self) -> crate::owned::create_topics_response::CreatableTopicConfigs {
518 crate::owned::create_topics_response::CreatableTopicConfigs {
519 name: (self.name).to_string(),
520 value: (self.value).map(std::string::ToString::to_string),
521 read_only: (self.read_only),
522 config_source: (self.config_source),
523 is_sensitive: (self.is_sensitive),
524 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
525 }
526 }
527}
528impl Encode for CreatableTopicConfigs<'_> {
529 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
530 let flex = version >= 5;
531 if version >= 5 {
532 if flex {
533 let () = put_compact_string(buf, self.name);
534 } else {
535 let () = put_string(buf, self.name);
536 }
537 }
538 if version >= 5 {
539 if flex {
540 let () = put_compact_nullable_string(buf, self.value);
541 } else {
542 let () = put_nullable_string(buf, self.value);
543 }
544 }
545 if version >= 5 {
546 put_bool(buf, self.read_only);
547 }
548 if version >= 5 {
549 put_i8(buf, self.config_source);
550 }
551 if version >= 5 {
552 put_bool(buf, self.is_sensitive);
553 }
554 if flex {
555 let tagged = WriteTaggedFields::new();
556 tagged.write(buf, &self.unknown_tagged_fields);
557 }
558 Ok(())
559 }
560 fn encoded_len(&self, version: i16) -> usize {
561 let flex = version >= 5;
562 let mut n: usize = 0;
563 if version >= 5 {
564 n += if flex {
565 compact_string_len(self.name)
566 } else {
567 string_len(self.name)
568 };
569 }
570 if version >= 5 {
571 n += if flex {
572 compact_nullable_string_len(self.value)
573 } else {
574 nullable_string_len(self.value)
575 };
576 }
577 if version >= 5 {
578 n += 1;
579 }
580 if version >= 5 {
581 n += 1;
582 }
583 if version >= 5 {
584 n += 1;
585 }
586 if flex {
587 let known_pairs: Vec<(u32, usize)> = Vec::new();
588 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
589 }
590 n
591 }
592}
593impl<'de> DecodeBorrow<'de> for CreatableTopicConfigs<'de> {
594 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
595 let flex = version >= 5;
596 let mut out = Self::default();
597 if version >= 5 {
598 out.name = if flex {
599 get_compact_string_borrowed(buf)?
600 } else {
601 get_string_borrowed(buf)?
602 };
603 }
604 if version >= 5 {
605 out.value = if flex {
606 get_compact_nullable_string_borrowed(buf)?
607 } else {
608 get_nullable_string_borrowed(buf)?
609 };
610 }
611 if version >= 5 {
612 out.read_only = get_bool(buf)?;
613 }
614 if version >= 5 {
615 out.config_source = get_i8(buf)?;
616 }
617 if version >= 5 {
618 out.is_sensitive = get_bool(buf)?;
619 }
620 if flex {
621 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
622 }
623 Ok(out)
624 }
625}
626#[cfg(test)]
627impl CreatableTopicConfigs<'_> {
628 #[must_use]
629 pub fn populated(version: i16) -> Self {
630 let mut m = Self::default();
631 if version >= 5 {
632 m.name = "x";
633 }
634 if version >= 5 {
635 m.value = Some("x");
636 }
637 if version >= 5 {
638 m.read_only = true;
639 }
640 if version >= 5 {
641 m.config_source = 1i8;
642 }
643 if version >= 5 {
644 m.is_sensitive = true;
645 }
646 m
647 }
648}