kafka_protocol/messages/
update_raft_voter_response.rs

1//! UpdateRaftVoterResponse
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/UpdateRaftVoterResponse.json).
4// WARNING: the items of this module are generated and should not be edited directly
5#![allow(unused)]
6
7use std::borrow::Borrow;
8use std::collections::BTreeMap;
9
10use anyhow::{bail, Result};
11use bytes::Bytes;
12use uuid::Uuid;
13
14use crate::protocol::{
15    buf::{ByteBuf, ByteBufMut},
16    compute_unknown_tagged_fields_size, types, write_unknown_tagged_fields, Decodable, Decoder,
17    Encodable, Encoder, HeaderVersion, Message, StrBytes, VersionRange,
18};
19
20/// Valid versions: 0
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct CurrentLeader {
24    /// The replica id of the current leader or -1 if the leader is unknown
25    ///
26    /// Supported API versions: 0
27    pub leader_id: super::BrokerId,
28
29    /// The latest known leader epoch
30    ///
31    /// Supported API versions: 0
32    pub leader_epoch: i32,
33
34    /// The node's hostname
35    ///
36    /// Supported API versions: 0
37    pub host: StrBytes,
38
39    /// The node's port
40    ///
41    /// Supported API versions: 0
42    pub port: i32,
43
44    /// Other tagged fields
45    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
46}
47
48impl CurrentLeader {
49    /// Sets `leader_id` to the passed value.
50    ///
51    /// The replica id of the current leader or -1 if the leader is unknown
52    ///
53    /// Supported API versions: 0
54    pub fn with_leader_id(mut self, value: super::BrokerId) -> Self {
55        self.leader_id = value;
56        self
57    }
58    /// Sets `leader_epoch` to the passed value.
59    ///
60    /// The latest known leader epoch
61    ///
62    /// Supported API versions: 0
63    pub fn with_leader_epoch(mut self, value: i32) -> Self {
64        self.leader_epoch = value;
65        self
66    }
67    /// Sets `host` to the passed value.
68    ///
69    /// The node's hostname
70    ///
71    /// Supported API versions: 0
72    pub fn with_host(mut self, value: StrBytes) -> Self {
73        self.host = value;
74        self
75    }
76    /// Sets `port` to the passed value.
77    ///
78    /// The node's port
79    ///
80    /// Supported API versions: 0
81    pub fn with_port(mut self, value: i32) -> Self {
82        self.port = value;
83        self
84    }
85    /// Sets unknown_tagged_fields to the passed value.
86    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
87        self.unknown_tagged_fields = value;
88        self
89    }
90    /// Inserts an entry into unknown_tagged_fields.
91    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
92        self.unknown_tagged_fields.insert(key, value);
93        self
94    }
95}
96
97#[cfg(feature = "broker")]
98impl Encodable for CurrentLeader {
99    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
100        if version != 0 {
101            bail!("specified version not supported by this message type");
102        }
103        types::Int32.encode(buf, &self.leader_id)?;
104        types::Int32.encode(buf, &self.leader_epoch)?;
105        types::CompactString.encode(buf, &self.host)?;
106        types::Int32.encode(buf, &self.port)?;
107        let num_tagged_fields = self.unknown_tagged_fields.len();
108        if num_tagged_fields > std::u32::MAX as usize {
109            bail!(
110                "Too many tagged fields to encode ({} fields)",
111                num_tagged_fields
112            );
113        }
114        types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
115
116        write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
117        Ok(())
118    }
119    fn compute_size(&self, version: i16) -> Result<usize> {
120        let mut total_size = 0;
121        total_size += types::Int32.compute_size(&self.leader_id)?;
122        total_size += types::Int32.compute_size(&self.leader_epoch)?;
123        total_size += types::CompactString.compute_size(&self.host)?;
124        total_size += types::Int32.compute_size(&self.port)?;
125        let num_tagged_fields = self.unknown_tagged_fields.len();
126        if num_tagged_fields > std::u32::MAX as usize {
127            bail!(
128                "Too many tagged fields to encode ({} fields)",
129                num_tagged_fields
130            );
131        }
132        total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
133
134        total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
135        Ok(total_size)
136    }
137}
138
139#[cfg(feature = "client")]
140impl Decodable for CurrentLeader {
141    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
142        if version != 0 {
143            bail!("specified version not supported by this message type");
144        }
145        let leader_id = types::Int32.decode(buf)?;
146        let leader_epoch = types::Int32.decode(buf)?;
147        let host = types::CompactString.decode(buf)?;
148        let port = types::Int32.decode(buf)?;
149        let mut unknown_tagged_fields = BTreeMap::new();
150        let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
151        for _ in 0..num_tagged_fields {
152            let tag: u32 = types::UnsignedVarInt.decode(buf)?;
153            let size: u32 = types::UnsignedVarInt.decode(buf)?;
154            let unknown_value = buf.try_get_bytes(size as usize)?;
155            unknown_tagged_fields.insert(tag as i32, unknown_value);
156        }
157        Ok(Self {
158            leader_id,
159            leader_epoch,
160            host,
161            port,
162            unknown_tagged_fields,
163        })
164    }
165}
166
167impl Default for CurrentLeader {
168    fn default() -> Self {
169        Self {
170            leader_id: (-1).into(),
171            leader_epoch: -1,
172            host: Default::default(),
173            port: 0,
174            unknown_tagged_fields: BTreeMap::new(),
175        }
176    }
177}
178
179impl Message for CurrentLeader {
180    const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
181    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
182}
183
184/// Valid versions: 0
185#[non_exhaustive]
186#[derive(Debug, Clone, PartialEq)]
187pub struct UpdateRaftVoterResponse {
188    /// The duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota.
189    ///
190    /// Supported API versions: 0
191    pub throttle_time_ms: i32,
192
193    /// The error code, or 0 if there was no error
194    ///
195    /// Supported API versions: 0
196    pub error_code: i16,
197
198    ///
199    ///
200    /// Supported API versions: 0
201    pub current_leader: CurrentLeader,
202
203    /// Other tagged fields
204    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
205}
206
207impl UpdateRaftVoterResponse {
208    /// Sets `throttle_time_ms` to the passed value.
209    ///
210    /// The duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota.
211    ///
212    /// Supported API versions: 0
213    pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
214        self.throttle_time_ms = value;
215        self
216    }
217    /// Sets `error_code` to the passed value.
218    ///
219    /// The error code, or 0 if there was no error
220    ///
221    /// Supported API versions: 0
222    pub fn with_error_code(mut self, value: i16) -> Self {
223        self.error_code = value;
224        self
225    }
226    /// Sets `current_leader` to the passed value.
227    ///
228    ///
229    ///
230    /// Supported API versions: 0
231    pub fn with_current_leader(mut self, value: CurrentLeader) -> Self {
232        self.current_leader = value;
233        self
234    }
235    /// Sets unknown_tagged_fields to the passed value.
236    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
237        self.unknown_tagged_fields = value;
238        self
239    }
240    /// Inserts an entry into unknown_tagged_fields.
241    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
242        self.unknown_tagged_fields.insert(key, value);
243        self
244    }
245}
246
247#[cfg(feature = "broker")]
248impl Encodable for UpdateRaftVoterResponse {
249    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
250        if version != 0 {
251            bail!("specified version not supported by this message type");
252        }
253        types::Int32.encode(buf, &self.throttle_time_ms)?;
254        types::Int16.encode(buf, &self.error_code)?;
255        let mut num_tagged_fields = self.unknown_tagged_fields.len();
256        if &self.current_leader != &Default::default() {
257            num_tagged_fields += 1;
258        }
259        if num_tagged_fields > std::u32::MAX as usize {
260            bail!(
261                "Too many tagged fields to encode ({} fields)",
262                num_tagged_fields
263            );
264        }
265        types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
266        if &self.current_leader != &Default::default() {
267            let computed_size = types::Struct { version }.compute_size(&self.current_leader)?;
268            if computed_size > std::u32::MAX as usize {
269                bail!(
270                    "Tagged field is too large to encode ({} bytes)",
271                    computed_size
272                );
273            }
274            types::UnsignedVarInt.encode(buf, 0)?;
275            types::UnsignedVarInt.encode(buf, computed_size as u32)?;
276            types::Struct { version }.encode(buf, &self.current_leader)?;
277        }
278
279        write_unknown_tagged_fields(buf, 1.., &self.unknown_tagged_fields)?;
280        Ok(())
281    }
282    fn compute_size(&self, version: i16) -> Result<usize> {
283        let mut total_size = 0;
284        total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
285        total_size += types::Int16.compute_size(&self.error_code)?;
286        let mut num_tagged_fields = self.unknown_tagged_fields.len();
287        if &self.current_leader != &Default::default() {
288            num_tagged_fields += 1;
289        }
290        if num_tagged_fields > std::u32::MAX as usize {
291            bail!(
292                "Too many tagged fields to encode ({} fields)",
293                num_tagged_fields
294            );
295        }
296        total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
297        if &self.current_leader != &Default::default() {
298            let computed_size = types::Struct { version }.compute_size(&self.current_leader)?;
299            if computed_size > std::u32::MAX as usize {
300                bail!(
301                    "Tagged field is too large to encode ({} bytes)",
302                    computed_size
303                );
304            }
305            total_size += types::UnsignedVarInt.compute_size(0)?;
306            total_size += types::UnsignedVarInt.compute_size(computed_size as u32)?;
307            total_size += computed_size;
308        }
309
310        total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
311        Ok(total_size)
312    }
313}
314
315#[cfg(feature = "client")]
316impl Decodable for UpdateRaftVoterResponse {
317    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
318        if version != 0 {
319            bail!("specified version not supported by this message type");
320        }
321        let throttle_time_ms = types::Int32.decode(buf)?;
322        let error_code = types::Int16.decode(buf)?;
323        let mut current_leader = Default::default();
324        let mut unknown_tagged_fields = BTreeMap::new();
325        let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
326        for _ in 0..num_tagged_fields {
327            let tag: u32 = types::UnsignedVarInt.decode(buf)?;
328            let size: u32 = types::UnsignedVarInt.decode(buf)?;
329            match tag {
330                0 => {
331                    current_leader = types::Struct { version }.decode(buf)?;
332                }
333                _ => {
334                    let unknown_value = buf.try_get_bytes(size as usize)?;
335                    unknown_tagged_fields.insert(tag as i32, unknown_value);
336                }
337            }
338        }
339        Ok(Self {
340            throttle_time_ms,
341            error_code,
342            current_leader,
343            unknown_tagged_fields,
344        })
345    }
346}
347
348impl Default for UpdateRaftVoterResponse {
349    fn default() -> Self {
350        Self {
351            throttle_time_ms: 0,
352            error_code: 0,
353            current_leader: Default::default(),
354            unknown_tagged_fields: BTreeMap::new(),
355        }
356    }
357}
358
359impl Message for UpdateRaftVoterResponse {
360    const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
361    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
362}
363
364impl HeaderVersion for UpdateRaftVoterResponse {
365    fn header_version(version: i16) -> i16 {
366        1
367    }
368}