Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
ElectLeadersRequest.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_i8, get_i32, put_i8, 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 = 43;
14pub const MIN_VERSION: i16 = 0;
15pub const MAX_VERSION: i16 = 2;
16pub const LATEST_STABLE_VERSION: i16 = 2;
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)]
24pub struct ElectLeadersRequest<'a> {
25    pub election_type: i8,
26    pub topic_partitions: Option<Vec<TopicPartitions<'a>>>,
27    pub timeout_ms: i32,
28    pub unknown_tagged_fields: UnknownTaggedFields,
29}
30impl Default for ElectLeadersRequest<'_> {
31    fn default() -> Self {
32        Self {
33            election_type: 0i8,
34            topic_partitions: Some(Vec::new()),
35            timeout_ms: 60_000i32,
36            unknown_tagged_fields: UnknownTaggedFields::default(),
37        }
38    }
39}
40impl ElectLeadersRequest<'_> {
41    /// # Panics
42    ///
43    /// Panics if a records field contains an invalid encoded record batch.
44    #[must_use]
45    pub fn to_owned(&self) -> crate::owned::elect_leaders_request::ElectLeadersRequest {
46        crate::owned::elect_leaders_request::ElectLeadersRequest {
47            election_type: (self.election_type),
48            topic_partitions: (self.topic_partitions)
49                .as_ref()
50                .map(|v| v.iter().map(TopicPartitions::to_owned).collect()),
51            timeout_ms: (self.timeout_ms),
52            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
53        }
54    }
55}
56impl Encode for ElectLeadersRequest<'_> {
57    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
58        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
59            return Err(ProtocolError::UnsupportedVersion {
60                api_key: API_KEY,
61                version,
62            });
63        }
64        let flex = is_flexible(version);
65        if version >= 1 {
66            put_i8(buf, self.election_type);
67        }
68        if version >= 0 {
69            {
70                let len = (self.topic_partitions).as_ref().map(Vec::len);
71                crate::primitives::array::put_nullable_array_len(buf, len, flex);
72                if let Some(v) = &self.topic_partitions {
73                    for it in v {
74                        it.encode(buf, version)?;
75                    }
76                }
77            };
78        }
79        if version >= 0 {
80            put_i32(buf, self.timeout_ms);
81        }
82        if flex {
83            let tagged = WriteTaggedFields::new();
84            tagged.write(buf, &self.unknown_tagged_fields);
85        }
86        Ok(())
87    }
88    fn encoded_len(&self, version: i16) -> usize {
89        let flex = is_flexible(version);
90        let mut n: usize = 0;
91        if version >= 1 {
92            n += 1;
93        }
94        if version >= 0 {
95            n += {
96                let opt: Option<&Vec<_>> = (self.topic_partitions).as_ref();
97                let prefix = crate::primitives::array::nullable_array_len_prefix_len(
98                    opt.map(std::vec::Vec::len),
99                    flex,
100                );
101                let body: usize =
102                    opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum());
103                prefix + body
104            };
105        }
106        if version >= 0 {
107            n += 4;
108        }
109        if flex {
110            let known_pairs: Vec<(u32, usize)> = Vec::new();
111            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
112        }
113        n
114    }
115}
116impl<'de> DecodeBorrow<'de> for ElectLeadersRequest<'de> {
117    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
118        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
119            return Err(ProtocolError::UnsupportedVersion {
120                api_key: API_KEY,
121                version,
122            });
123        }
124        let flex = is_flexible(version);
125        let mut out = Self::default();
126        if version >= 1 {
127            out.election_type = get_i8(buf)?;
128        }
129        if version >= 0 {
130            out.topic_partitions = {
131                let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?;
132                match opt {
133                    None => None,
134                    Some(n) => {
135                        let mut v = Vec::with_capacity(n);
136                        for _ in 0..n {
137                            v.push(TopicPartitions::decode_borrow(buf, version)?);
138                        }
139                        Some(v)
140                    }
141                }
142            };
143        }
144        if version >= 0 {
145            out.timeout_ms = get_i32(buf)?;
146        }
147        if flex {
148            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
149        }
150        Ok(out)
151    }
152}
153#[cfg(test)]
154impl ElectLeadersRequest<'_> {
155    #[must_use]
156    pub fn populated(version: i16) -> Self {
157        let mut m = Self::default();
158        if version >= 1 {
159            m.election_type = 1i8;
160        }
161        if version >= 0 {
162            m.topic_partitions = Some(vec![TopicPartitions::populated(version)]);
163        }
164        if version >= 0 {
165            m.timeout_ms = 1i32;
166        }
167        m
168    }
169}
170#[derive(Debug, Clone, PartialEq, Eq, Default)]
171pub struct TopicPartitions<'a> {
172    pub topic: &'a str,
173    pub partitions: Vec<i32>,
174    pub unknown_tagged_fields: UnknownTaggedFields,
175}
176impl TopicPartitions<'_> {
177    /// # Panics
178    ///
179    /// Panics if a records field contains an invalid encoded record batch.
180    #[must_use]
181    pub fn to_owned(&self) -> crate::owned::elect_leaders_request::TopicPartitions {
182        crate::owned::elect_leaders_request::TopicPartitions {
183            topic: (self.topic).to_string(),
184            partitions: (self.partitions).clone(),
185            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
186        }
187    }
188}
189impl Encode for TopicPartitions<'_> {
190    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
191        let flex = version >= 2;
192        if version >= 0 {
193            if flex {
194                let () = put_compact_string(buf, self.topic);
195            } else {
196                let () = put_string(buf, self.topic);
197            }
198        }
199        if version >= 0 {
200            {
201                crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
202                for it in &self.partitions {
203                    put_i32(buf, *it);
204                }
205            };
206        }
207        if flex {
208            let tagged = WriteTaggedFields::new();
209            tagged.write(buf, &self.unknown_tagged_fields);
210        }
211        Ok(())
212    }
213    fn encoded_len(&self, version: i16) -> usize {
214        let flex = version >= 2;
215        let mut n: usize = 0;
216        if version >= 0 {
217            n += if flex {
218                compact_string_len(self.topic)
219            } else {
220                string_len(self.topic)
221            };
222        }
223        if version >= 0 {
224            n += {
225                let prefix =
226                    crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
227                let body: usize = (self.partitions).iter().map(|_| 4).sum();
228                prefix + body
229            };
230        }
231        if flex {
232            let known_pairs: Vec<(u32, usize)> = Vec::new();
233            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
234        }
235        n
236    }
237}
238impl<'de> DecodeBorrow<'de> for TopicPartitions<'de> {
239    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
240        let flex = version >= 2;
241        let mut out = Self::default();
242        if version >= 0 {
243            out.topic = if flex {
244                get_compact_string_borrowed(buf)?
245            } else {
246                get_string_borrowed(buf)?
247            };
248        }
249        if version >= 0 {
250            out.partitions = {
251                let n = crate::primitives::array::get_array_len(buf, flex)?;
252                let mut v = Vec::with_capacity(n);
253                for _ in 0..n {
254                    v.push(get_i32(buf)?);
255                }
256                v
257            };
258        }
259        if flex {
260            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
261        }
262        Ok(out)
263    }
264}
265#[cfg(test)]
266impl TopicPartitions<'_> {
267    #[must_use]
268    pub fn populated(version: i16) -> Self {
269        let mut m = Self::default();
270        if version >= 0 {
271            m.topic = "x";
272        }
273        if version >= 0 {
274            m.partitions = vec![1i32];
275        }
276        m
277    }
278}