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