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