Skip to main content

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