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    owned::create_partitions_request,
7    primitives::{
8        array::{
9            array_len_prefix_len, get_array_len, get_nullable_array_len,
10            nullable_array_len_prefix_len, put_array_len, put_nullable_array_len,
11        },
12        fixed::{get_bool, get_i32, put_bool, put_i32},
13        string_bytes::{compact_string_len, put_compact_string, put_string, string_len},
14        string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed},
15    },
16    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
17};
18pub const API_KEY: i16 = 37;
19pub const MIN_VERSION: i16 = 0;
20pub const MAX_VERSION: i16 = 3;
21pub const LATEST_STABLE_VERSION: i16 = 3;
22pub const FLEXIBLE_MIN: i16 = 2;
23#[inline]
24#[must_use]
25pub fn is_flexible(version: i16) -> bool {
26    version >= FLEXIBLE_MIN
27}
28#[derive(Debug, Clone, PartialEq, Eq, Default)]
29pub struct CreatePartitionsRequest<'a> {
30    pub topics: Vec<CreatePartitionsTopic<'a>>,
31    pub timeout_ms: i32,
32    pub validate_only: bool,
33    pub unknown_tagged_fields: UnknownTaggedFields,
34}
35impl CreatePartitionsRequest<'_> {
36    #[must_use]
37    pub fn to_owned(&self) -> create_partitions_request::CreatePartitionsRequest {
38        create_partitions_request::CreatePartitionsRequest {
39            topics: (self.topics)
40                .iter()
41                .map(CreatePartitionsTopic::to_owned)
42                .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 CreatePartitionsRequest<'_> {
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                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 >= 0 {
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 = array_len_prefix_len((self.topics).len(), flex);
84                let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum();
85                prefix + body
86            };
87        }
88        if version >= 0 {
89            n += 4;
90        }
91        if version >= 0 {
92            n += 1;
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 CreatePartitionsRequest<'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 >= 0 {
112            out.topics = {
113                let n = get_array_len(buf, flex)?;
114                let mut v = Vec::with_capacity(n);
115                for _ in 0..n {
116                    v.push(CreatePartitionsTopic::decode_borrow(buf, version)?);
117                }
118                v
119            };
120        }
121        if version >= 0 {
122            out.timeout_ms = get_i32(buf)?;
123        }
124        if version >= 0 {
125            out.validate_only = get_bool(buf)?;
126        }
127        if flex {
128            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
129        }
130        Ok(out)
131    }
132}
133#[cfg(test)]
134impl CreatePartitionsRequest<'_> {
135    #[must_use]
136    pub fn populated(version: i16) -> Self {
137        let mut m = Self::default();
138        if version >= 0 {
139            m.topics = vec![CreatePartitionsTopic::populated(version)];
140        }
141        if version >= 0 {
142            m.timeout_ms = 1i32;
143        }
144        if version >= 0 {
145            m.validate_only = true;
146        }
147        m
148    }
149}
150#[derive(Debug, Clone, PartialEq, Eq)]
151pub struct CreatePartitionsTopic<'a> {
152    pub name: &'a str,
153    pub count: i32,
154    pub assignments: Option<Vec<CreatePartitionsAssignment>>,
155    pub unknown_tagged_fields: UnknownTaggedFields,
156}
157impl Default for CreatePartitionsTopic<'_> {
158    fn default() -> Self {
159        Self {
160            name: "",
161            count: 0i32,
162            assignments: Some(Vec::new()),
163            unknown_tagged_fields: UnknownTaggedFields::default(),
164        }
165    }
166}
167impl CreatePartitionsTopic<'_> {
168    #[must_use]
169    pub fn to_owned(&self) -> create_partitions_request::CreatePartitionsTopic {
170        create_partitions_request::CreatePartitionsTopic {
171            name: (self.name).to_owned(),
172            count: (self.count),
173            assignments: (self.assignments)
174                .as_ref()
175                .map(|v| v.iter().map(CreatePartitionsAssignment::to_owned).collect()),
176            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
177        }
178    }
179}
180impl Encode for CreatePartitionsTopic<'_> {
181    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
182        let flex = version >= 2;
183        if version >= 0 {
184            if flex {
185                put_compact_string(buf, self.name)?;
186            } else {
187                put_string(buf, self.name)?;
188            }
189        }
190        if version >= 0 {
191            put_i32(buf, self.count);
192        }
193        if version >= 0 {
194            {
195                let len = (self.assignments).as_ref().map(Vec::len);
196                put_nullable_array_len(buf, len, flex)?;
197                if let Some(v) = &self.assignments {
198                    for it in v {
199                        it.encode(buf, version)?;
200                    }
201                }
202            }
203        }
204        if flex {
205            let tagged = WriteTaggedFields::new();
206            tagged.write(buf, &self.unknown_tagged_fields)?;
207        }
208        Ok(())
209    }
210    fn encoded_len(&self, version: i16) -> usize {
211        let flex = version >= 2;
212        let mut n: usize = 0;
213        if version >= 0 {
214            n += if flex {
215                compact_string_len(self.name)
216            } else {
217                string_len(self.name)
218            };
219        }
220        if version >= 0 {
221            n += 4;
222        }
223        if version >= 0 {
224            n += {
225                let opt: Option<&Vec<_>> = (self.assignments).as_ref();
226                let prefix = nullable_array_len_prefix_len(opt.map(Vec::len), flex);
227                let body: usize =
228                    opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum());
229                prefix + body
230            };
231        }
232        if flex {
233            let known_pairs: Vec<(u32, usize)> = Vec::new();
234            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
235        }
236        n
237    }
238}
239impl<'de> DecodeBorrow<'de> for CreatePartitionsTopic<'de> {
240    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
241        let flex = version >= 2;
242        let mut out = Self::default();
243        if version >= 0 {
244            out.name = if flex {
245                get_compact_string_borrowed(buf)?
246            } else {
247                get_string_borrowed(buf)?
248            };
249        }
250        if version >= 0 {
251            out.count = get_i32(buf)?;
252        }
253        if version >= 0 {
254            out.assignments = {
255                let opt = get_nullable_array_len(buf, flex)?;
256                match opt {
257                    None => None,
258                    Some(n) => {
259                        let mut v = Vec::with_capacity(n);
260                        for _ in 0..n {
261                            v.push(CreatePartitionsAssignment::decode_borrow(buf, version)?);
262                        }
263                        Some(v)
264                    }
265                }
266            };
267        }
268        if flex {
269            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
270        }
271        Ok(out)
272    }
273}
274#[cfg(test)]
275impl CreatePartitionsTopic<'_> {
276    #[must_use]
277    pub fn populated(version: i16) -> Self {
278        let mut m = Self::default();
279        if version >= 0 {
280            m.name = "x";
281        }
282        if version >= 0 {
283            m.count = 1i32;
284        }
285        if version >= 0 {
286            m.assignments = Some(vec![CreatePartitionsAssignment::populated(version)]);
287        }
288        m
289    }
290}
291#[derive(Debug, Clone, PartialEq, Eq, Default)]
292pub struct CreatePartitionsAssignment {
293    pub broker_ids: Vec<i32>,
294    pub unknown_tagged_fields: UnknownTaggedFields,
295}
296impl CreatePartitionsAssignment {
297    #[must_use]
298    pub fn to_owned(&self) -> create_partitions_request::CreatePartitionsAssignment {
299        create_partitions_request::CreatePartitionsAssignment {
300            broker_ids: (self.broker_ids).clone(),
301            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
302        }
303    }
304}
305impl Encode for CreatePartitionsAssignment {
306    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
307        let flex = version >= 2;
308        if version >= 0 {
309            {
310                put_array_len(buf, (self.broker_ids).len(), flex)?;
311                for it in &self.broker_ids {
312                    put_i32(buf, *it);
313                }
314            }
315        }
316        if flex {
317            let tagged = WriteTaggedFields::new();
318            tagged.write(buf, &self.unknown_tagged_fields)?;
319        }
320        Ok(())
321    }
322    fn encoded_len(&self, version: i16) -> usize {
323        let flex = version >= 2;
324        let mut n: usize = 0;
325        if version >= 0 {
326            n += {
327                let prefix = array_len_prefix_len((self.broker_ids).len(), flex);
328                let body: usize = (self.broker_ids).iter().map(|_| 4).sum();
329                prefix + body
330            };
331        }
332        if flex {
333            let known_pairs: Vec<(u32, usize)> = Vec::new();
334            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
335        }
336        n
337    }
338}
339impl<'de> DecodeBorrow<'de> for CreatePartitionsAssignment {
340    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
341        let flex = version >= 2;
342        let mut out = Self::default();
343        if version >= 0 {
344            out.broker_ids = {
345                let n = get_array_len(buf, flex)?;
346                let mut v = Vec::with_capacity(n);
347                for _ in 0..n {
348                    v.push(get_i32(buf)?);
349                }
350                v
351            };
352        }
353        if flex {
354            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
355        }
356        Ok(out)
357    }
358}
359#[cfg(test)]
360impl CreatePartitionsAssignment {
361    #[must_use]
362    pub fn populated(version: i16) -> Self {
363        let mut m = Self::default();
364        if version >= 0 {
365            m.broker_ids = vec![1i32];
366        }
367        m
368    }
369}