Skip to main content

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