odradek_protocol/messages/
find_coordinator_response.rs1#![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
21pub fn is_flexible(version: i16) -> bool {
23 let _ = version;
24 version >= 3
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct FindCoordinatorResponse {
31 pub throttle_time_ms: i32,
33 pub error_code: i16,
35 pub error_message: Option<String>,
37 pub node_id: i32,
39 pub host: String,
41 pub port: i32,
43 pub coordinators: Vec<Coordinator>,
45 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 pub const API_KEY: i16 = 10;
67 pub const MIN_VERSION: i16 = 0;
69 pub const MAX_VERSION: i16 = 6;
71
72 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 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
119 Self::decode_with_limits(buf, version, Limits::default())
120 }
121
122 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 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#[derive(Debug, Clone, PartialEq)]
219#[non_exhaustive]
220pub struct Coordinator {
221 pub key: String,
223 pub node_id: i32,
225 pub host: String,
227 pub port: i32,
229 pub error_code: i16,
231 pub error_message: Option<String>,
233 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 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 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
293 Self::decode_with_limits(buf, version, Limits::default())
294 }
295
296 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 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}