crabka_protocol/opt/rustwide/workdir/generated/
CreateTopicsRequest.borrowed.rs1use crate::primitives::fixed::{get_bool, get_i16, get_i32, put_bool, put_i16, put_i32};
3use crate::primitives::string_bytes::{
4 compact_nullable_string_len, compact_string_len, nullable_string_len,
5 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string, string_len,
6};
7use crate::primitives::string_bytes_borrowed::{
8 get_compact_nullable_string_borrowed, get_compact_string_borrowed,
9 get_nullable_string_borrowed, get_string_borrowed,
10};
11use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
12use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
13use bytes::BufMut;
14pub const API_KEY: i16 = 19;
15pub const MIN_VERSION: i16 = 2;
16pub const MAX_VERSION: i16 = 7;
17pub const FLEXIBLE_MIN: i16 = 5;
18#[inline]
19fn is_flexible(version: i16) -> bool {
20 version >= FLEXIBLE_MIN
21}
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct CreateTopicsRequest<'a> {
24 pub topics: Vec<CreatableTopic<'a>>,
25 pub timeout_ms: i32,
26 pub validate_only: bool,
27 pub unknown_tagged_fields: UnknownTaggedFields,
28}
29impl Default for CreateTopicsRequest<'_> {
30 fn default() -> Self {
31 Self {
32 topics: Vec::new(),
33 timeout_ms: 60_000i32,
34 validate_only: false,
35 unknown_tagged_fields: Default::default(),
36 }
37 }
38}
39impl CreateTopicsRequest<'_> {
40 pub fn to_owned(&self) -> crate::owned::create_topics_request::CreateTopicsRequest {
41 crate::owned::create_topics_request::CreateTopicsRequest {
42 topics: (self.topics).iter().map(CreatableTopic::to_owned).collect(),
43 timeout_ms: (self.timeout_ms),
44 validate_only: (self.validate_only),
45 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
46 }
47 }
48}
49impl Encode for CreateTopicsRequest<'_> {
50 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
51 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
52 return Err(ProtocolError::UnsupportedVersion {
53 api_key: API_KEY,
54 version,
55 });
56 }
57 let flex = is_flexible(version);
58 if version >= 0 {
59 {
60 crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
61 for it in &self.topics {
62 it.encode(buf, version)?;
63 }
64 }
65 }
66 if version >= 0 {
67 put_i32(buf, self.timeout_ms);
68 }
69 if version >= 1 {
70 put_bool(buf, self.validate_only);
71 }
72 if flex {
73 let tagged = WriteTaggedFields::new();
74 tagged.write(buf, &self.unknown_tagged_fields);
75 }
76 Ok(())
77 }
78 fn encoded_len(&self, version: i16) -> usize {
79 let flex = is_flexible(version);
80 let mut n: usize = 0;
81 if version >= 0 {
82 n += {
83 let prefix =
84 crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
85 let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
86 prefix + body
87 };
88 }
89 if version >= 0 {
90 n += 4;
91 }
92 if version >= 1 {
93 n += 1;
94 }
95 if flex {
96 let known_pairs: Vec<(u32, usize)> = Vec::new();
97 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
98 }
99 n
100 }
101}
102impl<'de> DecodeBorrow<'de> for CreateTopicsRequest<'de> {
103 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
104 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
105 return Err(ProtocolError::UnsupportedVersion {
106 api_key: API_KEY,
107 version,
108 });
109 }
110 let flex = is_flexible(version);
111 let mut out = Self::default();
112 if version >= 0 {
113 out.topics = {
114 let n = crate::primitives::array::get_array_len(buf, flex)?;
115 let mut v = Vec::with_capacity(n);
116 for _ in 0..n {
117 v.push(CreatableTopic::decode_borrow(buf, version)?);
118 }
119 v
120 };
121 }
122 if version >= 0 {
123 out.timeout_ms = get_i32(buf)?;
124 }
125 if version >= 1 {
126 out.validate_only = get_bool(buf)?;
127 }
128 if flex {
129 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
130 }
131 Ok(out)
132 }
133}
134#[cfg(test)]
135impl CreateTopicsRequest<'_> {
136 #[must_use]
137 pub fn populated(version: i16) -> Self {
138 let mut m = Self::default();
139 if version >= 0 {
140 m.topics = vec![CreatableTopic::populated(version)];
141 }
142 if version >= 0 {
143 m.timeout_ms = 1i32;
144 }
145 if version >= 1 {
146 m.validate_only = true;
147 }
148 m
149 }
150}
151#[derive(Debug, Clone, PartialEq, Eq, Default)]
152pub struct CreatableTopic<'a> {
153 pub name: &'a str,
154 pub num_partitions: i32,
155 pub replication_factor: i16,
156 pub assignments: Vec<CreatableReplicaAssignment>,
157 pub configs: Vec<CreatableTopicConfig<'a>>,
158 pub unknown_tagged_fields: UnknownTaggedFields,
159}
160impl CreatableTopic<'_> {
161 pub fn to_owned(&self) -> crate::owned::create_topics_request::CreatableTopic {
162 crate::owned::create_topics_request::CreatableTopic {
163 name: (self.name).to_string(),
164 num_partitions: (self.num_partitions),
165 replication_factor: (self.replication_factor),
166 assignments: (self.assignments)
167 .iter()
168 .map(CreatableReplicaAssignment::to_owned)
169 .collect(),
170 configs: (self.configs)
171 .iter()
172 .map(CreatableTopicConfig::to_owned)
173 .collect(),
174 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
175 }
176 }
177}
178impl Encode for CreatableTopic<'_> {
179 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
180 let flex = version >= 5;
181 if version >= 0 {
182 if flex {
183 put_compact_string(buf, self.name);
184 } else {
185 put_string(buf, self.name);
186 }
187 }
188 if version >= 0 {
189 put_i32(buf, self.num_partitions);
190 }
191 if version >= 0 {
192 put_i16(buf, self.replication_factor);
193 }
194 if version >= 0 {
195 {
196 crate::primitives::array::put_array_len(buf, (self.assignments).len(), flex);
197 for it in &self.assignments {
198 it.encode(buf, version)?;
199 }
200 }
201 }
202 if version >= 0 {
203 {
204 crate::primitives::array::put_array_len(buf, (self.configs).len(), flex);
205 for it in &self.configs {
206 it.encode(buf, version)?;
207 }
208 }
209 }
210 if flex {
211 let tagged = WriteTaggedFields::new();
212 tagged.write(buf, &self.unknown_tagged_fields);
213 }
214 Ok(())
215 }
216 fn encoded_len(&self, version: i16) -> usize {
217 let flex = version >= 5;
218 let mut n: usize = 0;
219 if version >= 0 {
220 n += if flex {
221 compact_string_len(self.name)
222 } else {
223 string_len(self.name)
224 };
225 }
226 if version >= 0 {
227 n += 4;
228 }
229 if version >= 0 {
230 n += 2;
231 }
232 if version >= 0 {
233 n += {
234 let prefix =
235 crate::primitives::array::array_len_prefix_len((self.assignments).len(), flex);
236 let body: usize = (self.assignments)
237 .iter()
238 .map(|it| it.encoded_len(version))
239 .sum();
240 prefix + body
241 };
242 }
243 if version >= 0 {
244 n += {
245 let prefix =
246 crate::primitives::array::array_len_prefix_len((self.configs).len(), flex);
247 let body: usize = (self.configs)
248 .iter()
249 .map(|it| it.encoded_len(version))
250 .sum();
251 prefix + body
252 };
253 }
254 if flex {
255 let known_pairs: Vec<(u32, usize)> = Vec::new();
256 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
257 }
258 n
259 }
260}
261impl<'de> DecodeBorrow<'de> for CreatableTopic<'de> {
262 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
263 let flex = version >= 5;
264 let mut out = Self::default();
265 if version >= 0 {
266 out.name = if flex {
267 get_compact_string_borrowed(buf)?
268 } else {
269 get_string_borrowed(buf)?
270 };
271 }
272 if version >= 0 {
273 out.num_partitions = get_i32(buf)?;
274 }
275 if version >= 0 {
276 out.replication_factor = get_i16(buf)?;
277 }
278 if version >= 0 {
279 out.assignments = {
280 let n = crate::primitives::array::get_array_len(buf, flex)?;
281 let mut v = Vec::with_capacity(n);
282 for _ in 0..n {
283 v.push(CreatableReplicaAssignment::decode_borrow(buf, version)?);
284 }
285 v
286 };
287 }
288 if version >= 0 {
289 out.configs = {
290 let n = crate::primitives::array::get_array_len(buf, flex)?;
291 let mut v = Vec::with_capacity(n);
292 for _ in 0..n {
293 v.push(CreatableTopicConfig::decode_borrow(buf, version)?);
294 }
295 v
296 };
297 }
298 if flex {
299 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
300 }
301 Ok(out)
302 }
303}
304#[cfg(test)]
305impl CreatableTopic<'_> {
306 #[must_use]
307 pub fn populated(version: i16) -> Self {
308 let mut m = Self::default();
309 if version >= 0 {
310 m.name = "x";
311 }
312 if version >= 0 {
313 m.num_partitions = 1i32;
314 }
315 if version >= 0 {
316 m.replication_factor = 1i16;
317 }
318 if version >= 0 {
319 m.assignments = vec![CreatableReplicaAssignment::populated(version)];
320 }
321 if version >= 0 {
322 m.configs = vec![CreatableTopicConfig::populated(version)];
323 }
324 m
325 }
326}
327#[derive(Debug, Clone, PartialEq, Eq, Default)]
328pub struct CreatableReplicaAssignment {
329 pub partition_index: i32,
330 pub broker_ids: Vec<i32>,
331 pub unknown_tagged_fields: UnknownTaggedFields,
332}
333impl CreatableReplicaAssignment {
334 pub fn to_owned(&self) -> crate::owned::create_topics_request::CreatableReplicaAssignment {
335 crate::owned::create_topics_request::CreatableReplicaAssignment {
336 partition_index: (self.partition_index),
337 broker_ids: (self.broker_ids).clone(),
338 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
339 }
340 }
341}
342impl Encode for CreatableReplicaAssignment {
343 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
344 let flex = version >= 5;
345 if version >= 0 {
346 put_i32(buf, self.partition_index);
347 }
348 if version >= 0 {
349 {
350 crate::primitives::array::put_array_len(buf, (self.broker_ids).len(), flex);
351 for it in &self.broker_ids {
352 put_i32(buf, *it);
353 }
354 }
355 }
356 if flex {
357 let tagged = WriteTaggedFields::new();
358 tagged.write(buf, &self.unknown_tagged_fields);
359 }
360 Ok(())
361 }
362 fn encoded_len(&self, version: i16) -> usize {
363 let flex = version >= 5;
364 let mut n: usize = 0;
365 if version >= 0 {
366 n += 4;
367 }
368 if version >= 0 {
369 n += {
370 let prefix =
371 crate::primitives::array::array_len_prefix_len((self.broker_ids).len(), flex);
372 let body: usize = (self.broker_ids).iter().map(|_| 4).sum();
373 prefix + body
374 };
375 }
376 if flex {
377 let known_pairs: Vec<(u32, usize)> = Vec::new();
378 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
379 }
380 n
381 }
382}
383impl<'de> DecodeBorrow<'de> for CreatableReplicaAssignment {
384 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
385 let flex = version >= 5;
386 let mut out = Self::default();
387 if version >= 0 {
388 out.partition_index = get_i32(buf)?;
389 }
390 if version >= 0 {
391 out.broker_ids = {
392 let n = crate::primitives::array::get_array_len(buf, flex)?;
393 let mut v = Vec::with_capacity(n);
394 for _ in 0..n {
395 v.push(get_i32(buf)?);
396 }
397 v
398 };
399 }
400 if flex {
401 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
402 }
403 Ok(out)
404 }
405}
406#[cfg(test)]
407impl CreatableReplicaAssignment {
408 #[must_use]
409 pub fn populated(version: i16) -> Self {
410 let mut m = Self::default();
411 if version >= 0 {
412 m.partition_index = 1i32;
413 }
414 if version >= 0 {
415 m.broker_ids = vec![1i32];
416 }
417 m
418 }
419}
420#[derive(Debug, Clone, PartialEq, Eq, Default)]
421pub struct CreatableTopicConfig<'a> {
422 pub name: &'a str,
423 pub value: Option<&'a str>,
424 pub unknown_tagged_fields: UnknownTaggedFields,
425}
426impl CreatableTopicConfig<'_> {
427 pub fn to_owned(&self) -> crate::owned::create_topics_request::CreatableTopicConfig {
428 crate::owned::create_topics_request::CreatableTopicConfig {
429 name: (self.name).to_string(),
430 value: (self.value).map(std::string::ToString::to_string),
431 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
432 }
433 }
434}
435impl Encode for CreatableTopicConfig<'_> {
436 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
437 let flex = version >= 5;
438 if version >= 0 {
439 if flex {
440 put_compact_string(buf, self.name);
441 } else {
442 put_string(buf, self.name);
443 }
444 }
445 if version >= 0 {
446 if flex {
447 put_compact_nullable_string(buf, self.value);
448 } else {
449 put_nullable_string(buf, self.value);
450 }
451 }
452 if flex {
453 let tagged = WriteTaggedFields::new();
454 tagged.write(buf, &self.unknown_tagged_fields);
455 }
456 Ok(())
457 }
458 fn encoded_len(&self, version: i16) -> usize {
459 let flex = version >= 5;
460 let mut n: usize = 0;
461 if version >= 0 {
462 n += if flex {
463 compact_string_len(self.name)
464 } else {
465 string_len(self.name)
466 };
467 }
468 if version >= 0 {
469 n += if flex {
470 compact_nullable_string_len(self.value)
471 } else {
472 nullable_string_len(self.value)
473 };
474 }
475 if flex {
476 let known_pairs: Vec<(u32, usize)> = Vec::new();
477 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
478 }
479 n
480 }
481}
482impl<'de> DecodeBorrow<'de> for CreatableTopicConfig<'de> {
483 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
484 let flex = version >= 5;
485 let mut out = Self::default();
486 if version >= 0 {
487 out.name = if flex {
488 get_compact_string_borrowed(buf)?
489 } else {
490 get_string_borrowed(buf)?
491 };
492 }
493 if version >= 0 {
494 out.value = if flex {
495 get_compact_nullable_string_borrowed(buf)?
496 } else {
497 get_nullable_string_borrowed(buf)?
498 };
499 }
500 if flex {
501 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
502 }
503 Ok(out)
504 }
505}
506#[cfg(test)]
507impl CreatableTopicConfig<'_> {
508 #[must_use]
509 pub fn populated(version: i16) -> Self {
510 let mut m = Self::default();
511 if version >= 0 {
512 m.name = "x";
513 }
514 if version >= 0 {
515 m.value = Some("x");
516 }
517 m
518 }
519}