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