Skip to main content

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

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