Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::BufMut;
4
5use crate::primitives::fixed::{get_bool, get_i16, get_i32, get_u16, put_bool, put_i16, put_i32, put_u16};
6use crate::primitives::string_bytes::{
7    compact_string_len, put_compact_string, put_string, string_len,
8};
9use crate::primitives::string_bytes_borrowed::{
10    get_compact_string_borrowed, get_string_borrowed,
11};
12use crate::tagged_fields::{encode_to_bytes, read_tagged_fields, tagged_fields_len, WriteTaggedFields};
13use crate::{Decode, DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
14
15pub const API_KEY: i16 = 52;
16pub const MIN_VERSION: i16 = 0;
17pub const MAX_VERSION: i16 = 2;
18pub const FLEXIBLE_MIN: i16 = 0;
19
20#[inline]
21fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
22
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct VoteResponse<'a> {
25    pub error_code: i16,
26    pub topics: Vec<TopicData<'a>>,
27    pub node_endpoints: Vec<crate::owned::vote_response::NodeEndpoint>,
28    pub unknown_tagged_fields: UnknownTaggedFields,
29}
30
31impl<'a> Default for VoteResponse<'a> {
32    fn default() -> Self {
33        Self {
34            error_code: 0i16,
35            topics: Vec::new(),
36            node_endpoints: Vec::new(),
37            unknown_tagged_fields: Default::default(),
38        }
39    }
40}
41
42impl<'a> VoteResponse<'a> {
43    pub fn to_owned(&self) -> crate::owned::vote_response::VoteResponse {
44        crate::owned::vote_response::VoteResponse {
45            error_code: (self.error_code),
46            topics: (self.topics).iter().map(|it| it.to_owned()).collect(),
47            node_endpoints: self.node_endpoints.clone(),
48            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
49        }
50    }
51}
52
53impl<'a> Encode for VoteResponse<'a> {
54    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
55        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
56            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
57        }
58        let flex = is_flexible(version);
59        if version >= 0 { put_i16(buf, self.error_code) }
60        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { it.encode(buf, version)?; } } }
61        if flex {
62            let mut tagged = WriteTaggedFields::new();
63            if !(crate::codegen_helpers::is_default(&self.node_endpoints)) {
64                let payload = encode_to_bytes({ let prefix = crate::primitives::array::array_len_prefix_len((self.node_endpoints).len(), flex); let body: usize = (self.node_endpoints).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }, |b| { { crate::primitives::array::put_array_len(b, (self.node_endpoints).len(), flex); for it in &self.node_endpoints { it.encode(b, version)?; } }; Ok(()) });
65                tagged.add(0, payload);
66            }
67            tagged.write(buf, &self.unknown_tagged_fields);
68        }
69        Ok(())
70    }
71    fn encoded_len(&self, version: i16) -> usize {
72        let flex = is_flexible(version);
73        let mut n: usize = 0;
74        if version >= 0 { n += 2; }
75        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.topics).len(), flex); let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
76        if flex {
77            let mut known_pairs: Vec<(u32, usize)> = Vec::new();
78            if !(crate::codegen_helpers::is_default(&self.node_endpoints)) {
79                known_pairs.push((0, { let prefix = crate::primitives::array::array_len_prefix_len((self.node_endpoints).len(), flex); let body: usize = (self.node_endpoints).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }));
80            }
81            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
82        }
83        n
84    }
85}
86
87impl<'de> DecodeBorrow<'de> for VoteResponse<'de> {
88    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
89        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
90            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
91        }
92        let flex = is_flexible(version);
93        let mut out = Self::default();
94        if version >= 0 { out.error_code = get_i16(buf)?; }
95        if version >= 0 { out.topics = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(TopicData::decode_borrow(buf, version)?); } v }; }
96        if flex {
97            // Pre-declare typed slots for known tagged fields.
98            let mut tag_node_endpoints = None;
99            out.unknown_tagged_fields = read_tagged_fields(buf, |tag, payload| {
100                match tag {
101                0 => { tag_node_endpoints = Some({ let b: &mut &[u8] = payload; { let n = crate::primitives::array::get_array_len(b, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(crate::owned::vote_response::NodeEndpoint::decode(b, version)?); } v } }); Ok(true) }
102                    _ => Ok(false),
103                }
104            })?;
105            if let Some(v) = tag_node_endpoints { out.node_endpoints = v; }
106        }
107        Ok(out)
108    }
109}
110
111#[derive(Debug, Clone, PartialEq, Eq)]
112pub struct TopicData<'a> {
113    pub topic_name: &'a str,
114    pub partitions: Vec<PartitionData>,
115    pub unknown_tagged_fields: UnknownTaggedFields,
116}
117
118impl<'a> Default for TopicData<'a> {
119    fn default() -> Self {
120        Self {
121            topic_name: "",
122            partitions: Vec::new(),
123            unknown_tagged_fields: Default::default(),
124        }
125    }
126}
127
128impl<'a> TopicData<'a> {
129    pub fn to_owned(&self) -> crate::owned::vote_response::TopicData {
130        crate::owned::vote_response::TopicData {
131            topic_name: (self.topic_name).to_string(),
132            partitions: (self.partitions).iter().map(|it| it.to_owned()).collect(),
133            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
134        }
135    }
136}
137
138impl<'a> Encode for TopicData<'a> {
139    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
140        let flex = version >= 0;
141        if version >= 0 { if flex { put_compact_string(buf, self.topic_name) } else { put_string(buf, self.topic_name) } }
142        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { it.encode(buf, version)?; } } }
143        if flex {
144            let tagged = WriteTaggedFields::new();
145            tagged.write(buf, &self.unknown_tagged_fields);
146        }
147        Ok(())
148    }
149    fn encoded_len(&self, version: i16) -> usize {
150        let flex = version >= 0;
151        let mut n: usize = 0;
152        if version >= 0 { n += if flex { compact_string_len(self.topic_name) } else { string_len(self.topic_name) }; }
153        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex); let body: usize = (self.partitions).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
154        if flex {
155            let known_pairs: Vec<(u32, usize)> = Vec::new();
156            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
157        }
158        n
159    }
160}
161
162impl<'de> DecodeBorrow<'de> for TopicData<'de> {
163    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
164        let flex = version >= 0;
165        let mut out = Self::default();
166        if version >= 0 { out.topic_name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
167        if version >= 0 { out.partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(PartitionData::decode_borrow(buf, version)?); } v }; }
168        if flex {
169            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
170                Ok(false)
171            })?;
172        }
173        Ok(out)
174    }
175}
176
177#[derive(Debug, Clone, PartialEq, Eq)]
178pub struct PartitionData {
179    pub partition_index: i32,
180    pub error_code: i16,
181    pub leader_id: i32,
182    pub leader_epoch: i32,
183    pub vote_granted: bool,
184    pub unknown_tagged_fields: UnknownTaggedFields,
185}
186
187impl Default for PartitionData {
188    fn default() -> Self {
189        Self {
190            partition_index: 0i32,
191            error_code: 0i16,
192            leader_id: 0i32,
193            leader_epoch: 0i32,
194            vote_granted: false,
195            unknown_tagged_fields: Default::default(),
196        }
197    }
198}
199
200impl PartitionData {
201    pub fn to_owned(&self) -> crate::owned::vote_response::PartitionData {
202        crate::owned::vote_response::PartitionData {
203            partition_index: (self.partition_index),
204            error_code: (self.error_code),
205            leader_id: (self.leader_id),
206            leader_epoch: (self.leader_epoch),
207            vote_granted: (self.vote_granted),
208            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
209        }
210    }
211}
212
213impl Encode for PartitionData {
214    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
215        let flex = version >= 0;
216        if version >= 0 { put_i32(buf, self.partition_index) }
217        if version >= 0 { put_i16(buf, self.error_code) }
218        if version >= 0 { put_i32(buf, self.leader_id) }
219        if version >= 0 { put_i32(buf, self.leader_epoch) }
220        if version >= 0 { put_bool(buf, self.vote_granted) }
221        if flex {
222            let tagged = WriteTaggedFields::new();
223            tagged.write(buf, &self.unknown_tagged_fields);
224        }
225        Ok(())
226    }
227    fn encoded_len(&self, version: i16) -> usize {
228        let flex = version >= 0;
229        let mut n: usize = 0;
230        if version >= 0 { n += 4; }
231        if version >= 0 { n += 2; }
232        if version >= 0 { n += 4; }
233        if version >= 0 { n += 4; }
234        if version >= 0 { n += 1; }
235        if flex {
236            let known_pairs: Vec<(u32, usize)> = Vec::new();
237            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
238        }
239        n
240    }
241}
242
243impl<'de> DecodeBorrow<'de> for PartitionData {
244    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
245        let flex = version >= 0;
246        let mut out = Self::default();
247        if version >= 0 { out.partition_index = get_i32(buf)?; }
248        if version >= 0 { out.error_code = get_i16(buf)?; }
249        if version >= 0 { out.leader_id = get_i32(buf)?; }
250        if version >= 0 { out.leader_epoch = get_i32(buf)?; }
251        if version >= 0 { out.vote_granted = get_bool(buf)?; }
252        if flex {
253            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
254                Ok(false)
255            })?;
256        }
257        Ok(out)
258    }
259}
260
261#[derive(Debug, Clone, PartialEq, Eq)]
262pub struct NodeEndpoint<'a> {
263    pub node_id: i32,
264    pub host: &'a str,
265    pub port: u16,
266    pub unknown_tagged_fields: UnknownTaggedFields,
267}
268
269impl<'a> Default for NodeEndpoint<'a> {
270    fn default() -> Self {
271        Self {
272            node_id: 0i32,
273            host: "",
274            port: 0u16,
275            unknown_tagged_fields: Default::default(),
276        }
277    }
278}
279
280impl<'a> NodeEndpoint<'a> {
281    pub fn to_owned(&self) -> crate::owned::vote_response::NodeEndpoint {
282        crate::owned::vote_response::NodeEndpoint {
283            node_id: (self.node_id),
284            host: (self.host).to_string(),
285            port: (self.port),
286            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
287        }
288    }
289}
290
291impl<'a> Encode for NodeEndpoint<'a> {
292    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
293        let flex = version >= 0;
294        if version >= 1 { put_i32(buf, self.node_id) }
295        if version >= 1 { if flex { put_compact_string(buf, self.host) } else { put_string(buf, self.host) } }
296        if version >= 1 { put_u16(buf, self.port) }
297        if flex {
298            let tagged = WriteTaggedFields::new();
299            tagged.write(buf, &self.unknown_tagged_fields);
300        }
301        Ok(())
302    }
303    fn encoded_len(&self, version: i16) -> usize {
304        let flex = version >= 0;
305        let mut n: usize = 0;
306        if version >= 1 { n += 4; }
307        if version >= 1 { n += if flex { compact_string_len(self.host) } else { string_len(self.host) }; }
308        if version >= 1 { n += 2; }
309        if flex {
310            let known_pairs: Vec<(u32, usize)> = Vec::new();
311            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
312        }
313        n
314    }
315}
316
317impl<'de> DecodeBorrow<'de> for NodeEndpoint<'de> {
318    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
319        let flex = version >= 0;
320        let mut out = Self::default();
321        if version >= 1 { out.node_id = get_i32(buf)?; }
322        if version >= 1 { out.host = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
323        if version >= 1 { out.port = get_u16(buf)?; }
324        if flex {
325            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
326                Ok(false)
327            })?;
328        }
329        Ok(out)
330    }
331}