Skip to main content

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

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