Skip to main content

odradek_protocol/messages/
find_coordinator_response.rs

1//! `FindCoordinatorResponse` — generated from `schemas/FindCoordinatorResponse.json`.
2//!
3//! @generated by `cargo xtask codegen`. Do not edit by hand.
4
5#![allow(
6    clippy::field_reassign_with_default,
7    clippy::nonminimal_bool,
8    clippy::derivable_impls,
9    clippy::borrow_deref_ref,
10    clippy::double_parens,
11    unused_parens
12)]
13
14use bytes::{Buf, BufMut};
15
16use crate::budget::{Budget, Limits};
17#[allow(unused_imports)]
18use crate::error::{DecodeError, EncodeError};
19use crate::wire::{self, RawTaggedField};
20
21/// Whether `version` of this message uses the flexible (compact) encoding.
22pub fn is_flexible(version: i16) -> bool {
23    let _ = version;
24    version >= 3
25}
26
27/// `FindCoordinatorResponse`, versions 0-6.
28#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct FindCoordinatorResponse {
31    /// 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.
32    pub throttle_time_ms: i32,
33    /// The error code, or 0 if there was no error.
34    pub error_code: i16,
35    /// The error message, or null if there was no error.
36    pub error_message: Option<String>,
37    /// The node id.
38    pub node_id: i32,
39    /// The host name.
40    pub host: String,
41    /// The port.
42    pub port: i32,
43    /// Each coordinator result in the response.
44    pub coordinators: Vec<Coordinator>,
45    /// Tagged fields not (yet) known to this crate, kept raw.
46    pub unknown_tagged_fields: Vec<RawTaggedField>,
47}
48
49impl Default for FindCoordinatorResponse {
50    fn default() -> Self {
51        Self {
52            throttle_time_ms: 0,
53            error_code: 0,
54            error_message: Some(String::new()),
55            node_id: 0,
56            host: String::new(),
57            port: 0,
58            coordinators: Vec::new(),
59            unknown_tagged_fields: Vec::new(),
60        }
61    }
62}
63
64impl FindCoordinatorResponse {
65    /// This message's api key in Kafka's registry.
66    pub const API_KEY: i16 = 10;
67    /// The lowest schema version this snapshot can speak.
68    pub const MIN_VERSION: i16 = 0;
69    /// The highest schema version this snapshot can speak.
70    pub const MAX_VERSION: i16 = 6;
71
72    /// Encode this `FindCoordinatorResponse` at `version`.
73    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
74        if version >= 1 {
75            buf.put_i32(self.throttle_time_ms);
76        }
77        if version <= 3 {
78            buf.put_i16(self.error_code);
79        }
80        if (1..=3).contains(&version) {
81            if is_flexible(version) {
82                wire::put_compact_nullable_string(buf, self.error_message.as_deref());
83            } else {
84                wire::put_nullable_string(buf, self.error_message.as_deref())?;
85            }
86        }
87        if version <= 3 {
88            buf.put_i32(self.node_id);
89        }
90        if version <= 3 {
91            if is_flexible(version) {
92                wire::put_compact_string(buf, &self.host);
93            } else {
94                wire::put_string(buf, &self.host)?;
95            }
96        }
97        if version <= 3 {
98            buf.put_i32(self.port);
99        }
100        if version >= 4 {
101            if is_flexible(version) {
102                wire::put_compact_array_len(buf, Some(self.coordinators.len()));
103            } else {
104                wire::put_array_len(buf, Some(self.coordinators.len()))?;
105            }
106            for item in &self.coordinators {
107                item.encode(buf, version)?;
108            }
109        }
110        if is_flexible(version) {
111            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
112        }
113        Ok(())
114    }
115
116    /// Decode one `FindCoordinatorResponse`, bounding allocation by the default
117    /// [`Limits`] derived from `buf`'s remaining length.
118    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
119        Self::decode_with_limits(buf, version, Limits::default())
120    }
121
122    /// Decode one `FindCoordinatorResponse` under `limits`.
123    pub fn decode_with_limits(
124        buf: &mut impl Buf,
125        version: i16,
126        limits: Limits,
127    ) -> Result<Self, DecodeError> {
128        let mut budget = limits.budget(buf.remaining());
129        Self::decode_with_budget(buf, version, &mut budget)
130    }
131
132    /// Decode one `FindCoordinatorResponse` against an existing `budget`.
133    pub fn decode_with_budget(
134        buf: &mut impl Buf,
135        version: i16,
136        budget: &mut Budget,
137    ) -> Result<Self, DecodeError> {
138        let mut this = Self::default();
139        if version >= 1 {
140            this.throttle_time_ms = wire::get_i32(buf)?;
141        }
142        if version <= 3 {
143            this.error_code = wire::get_i16(buf)?;
144        }
145        if (1..=3).contains(&version) {
146            this.error_message = if is_flexible(version) {
147                wire::get_compact_nullable_string(buf)?
148            } else {
149                wire::get_nullable_string(buf)?
150            };
151        }
152        if version <= 3 {
153            this.node_id = wire::get_i32(buf)?;
154        }
155        if version <= 3 {
156            this.host = if is_flexible(version) {
157                wire::get_compact_string(buf)?
158            } else {
159                wire::get_string(buf)?
160            };
161        }
162        if version <= 3 {
163            this.port = wire::get_i32(buf)?;
164        }
165        if version >= 4 {
166            this.coordinators = {
167                let len = if is_flexible(version) {
168                    wire::get_compact_array_len(buf)?
169                } else {
170                    wire::get_array_len(buf)?
171                };
172                match len {
173                    None => return Err(DecodeError::InvalidLength(-1)),
174                    Some(n) => {
175                        let mut items = Vec::new();
176                        for _ in 0..n {
177                            let mark = buf.remaining();
178                            let item = Coordinator::decode_with_budget(buf, version, budget)?;
179                            budget.progress(mark, buf.remaining())?;
180                            budget.push(&mut items, item)?;
181                        }
182                        items
183                    }
184                }
185            };
186        }
187        if is_flexible(version) {
188            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
189        }
190        Ok(this)
191    }
192}
193
194impl crate::Message for FindCoordinatorResponse {
195    const API_KEY: i16 = 10;
196    const MIN_VERSION: i16 = 0;
197    const MAX_VERSION: i16 = 6;
198
199    fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
200        FindCoordinatorResponse::encode(self, buf, version)
201    }
202    fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
203        FindCoordinatorResponse::decode(buf, version)
204    }
205    fn decode_with_limits(
206        buf: &mut impl Buf,
207        version: i16,
208        limits: Limits,
209    ) -> Result<Self, DecodeError> {
210        FindCoordinatorResponse::decode_with_limits(buf, version, limits)
211    }
212    fn throttle_time_ms(&self) -> Option<i32> {
213        Some(self.throttle_time_ms)
214    }
215}
216
217/// `Coordinator` (nested in [`FindCoordinatorResponse`]).
218#[derive(Debug, Clone, PartialEq)]
219#[non_exhaustive]
220pub struct Coordinator {
221    /// The coordinator key.
222    pub key: String,
223    /// The node id.
224    pub node_id: i32,
225    /// The host name.
226    pub host: String,
227    /// The port.
228    pub port: i32,
229    /// The error code, or 0 if there was no error.
230    pub error_code: i16,
231    /// The error message, or null if there was no error.
232    pub error_message: Option<String>,
233    /// Tagged fields not (yet) known to this crate, kept raw.
234    pub unknown_tagged_fields: Vec<RawTaggedField>,
235}
236
237impl Default for Coordinator {
238    fn default() -> Self {
239        Self {
240            key: String::new(),
241            node_id: 0,
242            host: String::new(),
243            port: 0,
244            error_code: 0,
245            error_message: Some(String::new()),
246            unknown_tagged_fields: Vec::new(),
247        }
248    }
249}
250
251impl Coordinator {
252    /// Encode this `Coordinator` at `version`.
253    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
254        if version >= 4 {
255            if is_flexible(version) {
256                wire::put_compact_string(buf, &self.key);
257            } else {
258                wire::put_string(buf, &self.key)?;
259            }
260        }
261        if version >= 4 {
262            buf.put_i32(self.node_id);
263        }
264        if version >= 4 {
265            if is_flexible(version) {
266                wire::put_compact_string(buf, &self.host);
267            } else {
268                wire::put_string(buf, &self.host)?;
269            }
270        }
271        if version >= 4 {
272            buf.put_i32(self.port);
273        }
274        if version >= 4 {
275            buf.put_i16(self.error_code);
276        }
277        if version >= 4 {
278            if is_flexible(version) {
279                wire::put_compact_nullable_string(buf, self.error_message.as_deref());
280            } else {
281                wire::put_nullable_string(buf, self.error_message.as_deref())?;
282            }
283        }
284        if is_flexible(version) {
285            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
286        }
287        Ok(())
288    }
289
290    /// Decode one `Coordinator`, bounding allocation by the default
291    /// [`Limits`] derived from `buf`'s remaining length.
292    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
293        Self::decode_with_limits(buf, version, Limits::default())
294    }
295
296    /// Decode one `Coordinator` under `limits`.
297    pub fn decode_with_limits(
298        buf: &mut impl Buf,
299        version: i16,
300        limits: Limits,
301    ) -> Result<Self, DecodeError> {
302        let mut budget = limits.budget(buf.remaining());
303        Self::decode_with_budget(buf, version, &mut budget)
304    }
305
306    /// Decode one `Coordinator` against an existing `budget`.
307    pub fn decode_with_budget(
308        buf: &mut impl Buf,
309        version: i16,
310        budget: &mut Budget,
311    ) -> Result<Self, DecodeError> {
312        let mut this = Self::default();
313        if version >= 4 {
314            this.key = if is_flexible(version) {
315                wire::get_compact_string(buf)?
316            } else {
317                wire::get_string(buf)?
318            };
319        }
320        if version >= 4 {
321            this.node_id = wire::get_i32(buf)?;
322        }
323        if version >= 4 {
324            this.host = if is_flexible(version) {
325                wire::get_compact_string(buf)?
326            } else {
327                wire::get_string(buf)?
328            };
329        }
330        if version >= 4 {
331            this.port = wire::get_i32(buf)?;
332        }
333        if version >= 4 {
334            this.error_code = wire::get_i16(buf)?;
335        }
336        if version >= 4 {
337            this.error_message = if is_flexible(version) {
338                wire::get_compact_nullable_string(buf)?
339            } else {
340                wire::get_nullable_string(buf)?
341            };
342        }
343        if is_flexible(version) {
344            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
345        }
346        Ok(this)
347    }
348}