Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
CreateTopicsRequest.borrowed.rs

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