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