Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
CreatePartitionsRequest.borrowed.rs

1// AUTO-GENERATED by krabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2use bytes::BufMut;
3
4use crate::{
5    DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6    primitives::{
7        fixed::{get_bool, get_i32, put_bool, put_i32},
8        string_bytes::{compact_string_len, put_compact_string, put_string, string_len},
9        string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed},
10    },
11    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
12};
13pub const API_KEY: i16 = 37;
14pub const MIN_VERSION: i16 = 0;
15pub const MAX_VERSION: i16 = 3;
16pub const LATEST_STABLE_VERSION: i16 = 3;
17pub const FLEXIBLE_MIN: i16 = 2;
18#[inline]
19#[must_use]
20pub fn is_flexible(version: i16) -> bool {
21    version >= FLEXIBLE_MIN
22}
23#[derive(Debug, Clone, PartialEq, Eq, Default)]
24pub struct CreatePartitionsRequest<'a> {
25    pub topics: Vec<CreatePartitionsTopic<'a>>,
26    pub timeout_ms: i32,
27    pub validate_only: bool,
28    pub unknown_tagged_fields: UnknownTaggedFields,
29}
30impl CreatePartitionsRequest<'_> {
31    /// # Panics
32    ///
33    /// Panics if a records field contains an invalid encoded record batch.
34    #[must_use]
35    pub fn to_owned(&self) -> crate::owned::create_partitions_request::CreatePartitionsRequest {
36        crate::owned::create_partitions_request::CreatePartitionsRequest {
37            topics: (self.topics)
38                .iter()
39                .map(CreatePartitionsTopic::to_owned)
40                .collect(),
41            timeout_ms: (self.timeout_ms),
42            validate_only: (self.validate_only),
43            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
44        }
45    }
46}
47impl Encode for CreatePartitionsRequest<'_> {
48    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
49        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
50            return Err(ProtocolError::UnsupportedVersion {
51                api_key: API_KEY,
52                version,
53            });
54        }
55        let flex = is_flexible(version);
56        if version >= 0 {
57            {
58                crate::primitives::array::put_array_len(buf, (self.topics).len(), flex);
59                for it in &self.topics {
60                    it.encode(buf, version)?;
61                }
62            };
63        }
64        if version >= 0 {
65            put_i32(buf, self.timeout_ms);
66        }
67        if version >= 0 {
68            put_bool(buf, self.validate_only);
69        }
70        if flex {
71            let tagged = WriteTaggedFields::new();
72            tagged.write(buf, &self.unknown_tagged_fields);
73        }
74        Ok(())
75    }
76    fn encoded_len(&self, version: i16) -> usize {
77        let flex = is_flexible(version);
78        let mut n: usize = 0;
79        if version >= 0 {
80            n += {
81                let prefix =
82                    crate::primitives::array::array_len_prefix_len((self.topics).len(), flex);
83                let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
84                prefix + body
85            };
86        }
87        if version >= 0 {
88            n += 4;
89        }
90        if version >= 0 {
91            n += 1;
92        }
93        if flex {
94            let known_pairs: Vec<(u32, usize)> = Vec::new();
95            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
96        }
97        n
98    }
99}
100impl<'de> DecodeBorrow<'de> for CreatePartitionsRequest<'de> {
101    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
102        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
103            return Err(ProtocolError::UnsupportedVersion {
104                api_key: API_KEY,
105                version,
106            });
107        }
108        let flex = is_flexible(version);
109        let mut out = Self::default();
110        if version >= 0 {
111            out.topics = {
112                let n = crate::primitives::array::get_array_len(buf, flex)?;
113                let mut v = Vec::with_capacity(n);
114                for _ in 0..n {
115                    v.push(CreatePartitionsTopic::decode_borrow(buf, version)?);
116                }
117                v
118            };
119        }
120        if version >= 0 {
121            out.timeout_ms = get_i32(buf)?;
122        }
123        if version >= 0 {
124            out.validate_only = get_bool(buf)?;
125        }
126        if flex {
127            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
128        }
129        Ok(out)
130    }
131}
132#[cfg(test)]
133impl CreatePartitionsRequest<'_> {
134    #[must_use]
135    pub fn populated(version: i16) -> Self {
136        let mut m = Self::default();
137        if version >= 0 {
138            m.topics = vec![CreatePartitionsTopic::populated(version)];
139        }
140        if version >= 0 {
141            m.timeout_ms = 1i32;
142        }
143        if version >= 0 {
144            m.validate_only = true;
145        }
146        m
147    }
148}
149#[derive(Debug, Clone, PartialEq, Eq)]
150pub struct CreatePartitionsTopic<'a> {
151    pub name: &'a str,
152    pub count: i32,
153    pub assignments: Option<Vec<CreatePartitionsAssignment>>,
154    pub unknown_tagged_fields: UnknownTaggedFields,
155}
156impl Default for CreatePartitionsTopic<'_> {
157    fn default() -> Self {
158        Self {
159            name: "",
160            count: 0i32,
161            assignments: Some(Vec::new()),
162            unknown_tagged_fields: UnknownTaggedFields::default(),
163        }
164    }
165}
166impl CreatePartitionsTopic<'_> {
167    /// # Panics
168    ///
169    /// Panics if a records field contains an invalid encoded record batch.
170    #[must_use]
171    pub fn to_owned(&self) -> crate::owned::create_partitions_request::CreatePartitionsTopic {
172        crate::owned::create_partitions_request::CreatePartitionsTopic {
173            name: (self.name).to_string(),
174            count: (self.count),
175            assignments: (self.assignments)
176                .as_ref()
177                .map(|v| v.iter().map(CreatePartitionsAssignment::to_owned).collect()),
178            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
179        }
180    }
181}
182impl Encode for CreatePartitionsTopic<'_> {
183    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
184        let flex = version >= 2;
185        if version >= 0 {
186            if flex {
187                let () = put_compact_string(buf, self.name);
188            } else {
189                let () = put_string(buf, self.name);
190            }
191        }
192        if version >= 0 {
193            put_i32(buf, self.count);
194        }
195        if version >= 0 {
196            {
197                let len = (self.assignments).as_ref().map(Vec::len);
198                crate::primitives::array::put_nullable_array_len(buf, len, flex);
199                if let Some(v) = &self.assignments {
200                    for it in v {
201                        it.encode(buf, version)?;
202                    }
203                }
204            };
205        }
206        if flex {
207            let tagged = WriteTaggedFields::new();
208            tagged.write(buf, &self.unknown_tagged_fields);
209        }
210        Ok(())
211    }
212    fn encoded_len(&self, version: i16) -> usize {
213        let flex = version >= 2;
214        let mut n: usize = 0;
215        if version >= 0 {
216            n += if flex {
217                compact_string_len(self.name)
218            } else {
219                string_len(self.name)
220            };
221        }
222        if version >= 0 {
223            n += 4;
224        }
225        if version >= 0 {
226            n += {
227                let opt: Option<&Vec<_>> = (self.assignments).as_ref();
228                let prefix = crate::primitives::array::nullable_array_len_prefix_len(
229                    opt.map(std::vec::Vec::len),
230                    flex,
231                );
232                let body: usize =
233                    opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum());
234                prefix + body
235            };
236        }
237        if flex {
238            let known_pairs: Vec<(u32, usize)> = Vec::new();
239            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
240        }
241        n
242    }
243}
244impl<'de> DecodeBorrow<'de> for CreatePartitionsTopic<'de> {
245    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
246        let flex = version >= 2;
247        let mut out = Self::default();
248        if version >= 0 {
249            out.name = if flex {
250                get_compact_string_borrowed(buf)?
251            } else {
252                get_string_borrowed(buf)?
253            };
254        }
255        if version >= 0 {
256            out.count = get_i32(buf)?;
257        }
258        if version >= 0 {
259            out.assignments = {
260                let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?;
261                match opt {
262                    None => None,
263                    Some(n) => {
264                        let mut v = Vec::with_capacity(n);
265                        for _ in 0..n {
266                            v.push(CreatePartitionsAssignment::decode_borrow(buf, version)?);
267                        }
268                        Some(v)
269                    }
270                }
271            };
272        }
273        if flex {
274            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
275        }
276        Ok(out)
277    }
278}
279#[cfg(test)]
280impl CreatePartitionsTopic<'_> {
281    #[must_use]
282    pub fn populated(version: i16) -> Self {
283        let mut m = Self::default();
284        if version >= 0 {
285            m.name = "x";
286        }
287        if version >= 0 {
288            m.count = 1i32;
289        }
290        if version >= 0 {
291            m.assignments = Some(vec![CreatePartitionsAssignment::populated(version)]);
292        }
293        m
294    }
295}
296#[derive(Debug, Clone, PartialEq, Eq, Default)]
297pub struct CreatePartitionsAssignment {
298    pub broker_ids: Vec<i32>,
299    pub unknown_tagged_fields: UnknownTaggedFields,
300}
301impl CreatePartitionsAssignment {
302    /// # Panics
303    ///
304    /// Panics if a records field contains an invalid encoded record batch.
305    #[must_use]
306    pub fn to_owned(&self) -> crate::owned::create_partitions_request::CreatePartitionsAssignment {
307        crate::owned::create_partitions_request::CreatePartitionsAssignment {
308            broker_ids: (self.broker_ids).clone(),
309            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
310        }
311    }
312}
313impl Encode for CreatePartitionsAssignment {
314    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
315        let flex = version >= 2;
316        if version >= 0 {
317            {
318                crate::primitives::array::put_array_len(buf, (self.broker_ids).len(), flex);
319                for it in &self.broker_ids {
320                    put_i32(buf, *it);
321                }
322            };
323        }
324        if flex {
325            let tagged = WriteTaggedFields::new();
326            tagged.write(buf, &self.unknown_tagged_fields);
327        }
328        Ok(())
329    }
330    fn encoded_len(&self, version: i16) -> usize {
331        let flex = version >= 2;
332        let mut n: usize = 0;
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<'de> DecodeBorrow<'de> for CreatePartitionsAssignment {
349    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
350        let flex = version >= 2;
351        let mut out = Self::default();
352        if version >= 0 {
353            out.broker_ids = {
354                let n = crate::primitives::array::get_array_len(buf, flex)?;
355                let mut v = Vec::with_capacity(n);
356                for _ in 0..n {
357                    v.push(get_i32(buf)?);
358                }
359                v
360            };
361        }
362        if flex {
363            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
364        }
365        Ok(out)
366    }
367}
368#[cfg(test)]
369impl CreatePartitionsAssignment {
370    #[must_use]
371    pub fn populated(version: i16) -> Self {
372        let mut m = Self::default();
373        if version >= 0 {
374            m.broker_ids = vec![1i32];
375        }
376        m
377    }
378}