1#![allow(
9 clippy::field_reassign_with_default,
10 clippy::nonminimal_bool,
11 clippy::derivable_impls,
12 clippy::borrow_deref_ref,
13 clippy::double_parens,
14 unused_parens
15)]
16
17use bytes::{Buf, BufMut};
18
19use crate::budget::{Budget, Limits};
20#[allow(unused_imports)]
21use crate::error::{DecodeError, EncodeError};
22use crate::wire::{self, RawTaggedField};
23
24pub fn is_flexible(version: i16) -> bool {
26 let _ = version;
27 version >= 3
28}
29
30#[derive(Debug, Clone, PartialEq)]
32#[non_exhaustive]
33pub struct ApiVersionsResponse {
34 pub error_code: i16,
36 pub api_keys: Vec<ApiVersion>,
38 pub throttle_time_ms: i32,
40 pub supported_features: Option<Vec<SupportedFeatureKey>>,
43 pub finalized_features_epoch: Option<i64>,
46 pub finalized_features: Option<Vec<FinalizedFeatureKey>>,
49 pub zk_migration_ready: Option<bool>,
52 pub unknown_tagged_fields: Vec<RawTaggedField>,
54}
55
56impl Default for ApiVersionsResponse {
57 fn default() -> Self {
58 Self {
59 error_code: 0,
60 api_keys: Vec::new(),
61 throttle_time_ms: 0,
62 supported_features: None,
63 finalized_features_epoch: None,
64 finalized_features: None,
65 zk_migration_ready: None,
66 unknown_tagged_fields: Vec::new(),
67 }
68 }
69}
70
71impl ApiVersionsResponse {
72 pub const API_KEY: i16 = 18;
74 pub const MIN_VERSION: i16 = 0;
76 pub const MAX_VERSION: i16 = 4;
78
79 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
81 buf.put_i16(self.error_code);
82 if is_flexible(version) {
83 wire::put_compact_array_len(buf, Some(self.api_keys.len()));
84 } else {
85 wire::put_array_len(buf, Some(self.api_keys.len()))?;
86 }
87 for item in &self.api_keys {
88 item.encode(buf, version)?;
89 }
90 if version >= 1 {
91 buf.put_i32(self.throttle_time_ms);
92 }
93 if is_flexible(version) {
94 let mut tagged = self.unknown_tagged_fields.clone();
95 if version >= 3 {
96 if let Some(v) = &self.supported_features {
97 let mut tag_data = bytes::BytesMut::new();
98 {
99 let buf = &mut tag_data;
100 if is_flexible(version) {
101 wire::put_compact_array_len(buf, Some((*v).len()));
102 } else {
103 wire::put_array_len(buf, Some((*v).len()))?;
104 }
105 for item in &(*v) {
106 item.encode(buf, version)?;
107 }
108 }
109 tagged.push(RawTaggedField {
110 tag: 0,
111 data: tag_data.freeze(),
112 });
113 }
114 }
115 if version >= 3 {
116 if let Some(v) = &self.finalized_features_epoch {
117 let mut tag_data = bytes::BytesMut::new();
118 {
119 let buf = &mut tag_data;
120 buf.put_i64((*v));
121 }
122 tagged.push(RawTaggedField {
123 tag: 1,
124 data: tag_data.freeze(),
125 });
126 }
127 }
128 if version >= 3 {
129 if let Some(v) = &self.finalized_features {
130 let mut tag_data = bytes::BytesMut::new();
131 {
132 let buf = &mut tag_data;
133 if is_flexible(version) {
134 wire::put_compact_array_len(buf, Some((*v).len()));
135 } else {
136 wire::put_array_len(buf, Some((*v).len()))?;
137 }
138 for item in &(*v) {
139 item.encode(buf, version)?;
140 }
141 }
142 tagged.push(RawTaggedField {
143 tag: 2,
144 data: tag_data.freeze(),
145 });
146 }
147 }
148 if version >= 3 {
149 if let Some(v) = &self.zk_migration_ready {
150 let mut tag_data = bytes::BytesMut::new();
151 {
152 let buf = &mut tag_data;
153 buf.put_u8(u8::from((*v)));
154 }
155 tagged.push(RawTaggedField {
156 tag: 3,
157 data: tag_data.freeze(),
158 });
159 }
160 }
161 tagged.sort_by_key(|f| f.tag);
162 wire::put_tagged_fields(buf, &tagged);
163 }
164 Ok(())
165 }
166
167 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
170 Self::decode_with_limits(buf, version, Limits::default())
171 }
172
173 pub fn decode_with_limits(
175 buf: &mut impl Buf,
176 version: i16,
177 limits: Limits,
178 ) -> Result<Self, DecodeError> {
179 let mut budget = limits.budget(buf.remaining());
180 Self::decode_with_budget(buf, version, &mut budget)
181 }
182
183 pub fn decode_with_budget(
185 buf: &mut impl Buf,
186 version: i16,
187 budget: &mut Budget,
188 ) -> Result<Self, DecodeError> {
189 let mut this = Self::default();
190 this.error_code = wire::get_i16(buf)?;
191 this.api_keys = {
192 let len = if is_flexible(version) {
193 wire::get_compact_array_len(buf)?
194 } else {
195 wire::get_array_len(buf)?
196 };
197 match len {
198 None => return Err(DecodeError::InvalidLength(-1)),
199 Some(n) => {
200 let mut items = Vec::new();
201 for _ in 0..n {
202 let mark = buf.remaining();
203 let item = ApiVersion::decode_with_budget(buf, version, budget)?;
204 budget.progress(mark, buf.remaining())?;
205 budget.push(&mut items, item)?;
206 }
207 items
208 }
209 }
210 };
211 if version >= 1 {
212 this.throttle_time_ms = wire::get_i32(buf)?;
213 }
214 if is_flexible(version) {
215 for raw in wire::get_tagged_fields_with_budget(buf, budget)? {
216 if raw.tag == 0 && (version >= 3) {
217 let mut tag_data = raw.data;
218 let buf = &mut tag_data;
219 this.supported_features = Some({
220 let len = if is_flexible(version) {
221 wire::get_compact_array_len(buf)?
222 } else {
223 wire::get_array_len(buf)?
224 };
225 match len {
226 None => return Err(DecodeError::InvalidLength(-1)),
227 Some(n) => {
228 let mut items = Vec::new();
229 for _ in 0..n {
230 let mark = buf.remaining();
231 let item = SupportedFeatureKey::decode_with_budget(
232 buf, version, budget,
233 )?;
234 budget.progress(mark, buf.remaining())?;
235 budget.push(&mut items, item)?;
236 }
237 items
238 }
239 }
240 });
241 if buf.has_remaining() {
242 return Err(DecodeError::InvalidLength(
243 i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
244 ));
245 }
246 } else if raw.tag == 1 && (version >= 3) {
247 let mut tag_data = raw.data;
248 let buf = &mut tag_data;
249 this.finalized_features_epoch = Some(wire::get_i64(buf)?);
250 if buf.has_remaining() {
251 return Err(DecodeError::InvalidLength(
252 i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
253 ));
254 }
255 } else if raw.tag == 2 && (version >= 3) {
256 let mut tag_data = raw.data;
257 let buf = &mut tag_data;
258 this.finalized_features = Some({
259 let len = if is_flexible(version) {
260 wire::get_compact_array_len(buf)?
261 } else {
262 wire::get_array_len(buf)?
263 };
264 match len {
265 None => return Err(DecodeError::InvalidLength(-1)),
266 Some(n) => {
267 let mut items = Vec::new();
268 for _ in 0..n {
269 let mark = buf.remaining();
270 let item = FinalizedFeatureKey::decode_with_budget(
271 buf, version, budget,
272 )?;
273 budget.progress(mark, buf.remaining())?;
274 budget.push(&mut items, item)?;
275 }
276 items
277 }
278 }
279 });
280 if buf.has_remaining() {
281 return Err(DecodeError::InvalidLength(
282 i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
283 ));
284 }
285 } else if raw.tag == 3 && (version >= 3) {
286 let mut tag_data = raw.data;
287 let buf = &mut tag_data;
288 this.zk_migration_ready = Some(wire::get_bool(buf)?);
289 if buf.has_remaining() {
290 return Err(DecodeError::InvalidLength(
291 i64::try_from(buf.remaining()).unwrap_or(i64::MAX),
292 ));
293 }
294 } else {
295 budget.push(&mut this.unknown_tagged_fields, raw)?;
296 }
297 }
298 }
299 Ok(this)
300 }
301}
302
303impl crate::Message for ApiVersionsResponse {
304 const API_KEY: i16 = 18;
305 const MIN_VERSION: i16 = 0;
306 const MAX_VERSION: i16 = 4;
307
308 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
309 ApiVersionsResponse::encode(self, buf, version)
310 }
311 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
312 ApiVersionsResponse::decode(buf, version)
313 }
314 fn decode_with_limits(
315 buf: &mut impl Buf,
316 version: i16,
317 limits: Limits,
318 ) -> Result<Self, DecodeError> {
319 ApiVersionsResponse::decode_with_limits(buf, version, limits)
320 }
321 fn throttle_time_ms(&self) -> Option<i32> {
322 Some(self.throttle_time_ms)
323 }
324}
325
326#[derive(Debug, Clone, PartialEq)]
328#[non_exhaustive]
329pub struct ApiVersion {
330 pub api_key: i16,
332 pub min_version: i16,
334 pub max_version: i16,
336 pub unknown_tagged_fields: Vec<RawTaggedField>,
338}
339
340impl Default for ApiVersion {
341 fn default() -> Self {
342 Self {
343 api_key: 0,
344 min_version: 0,
345 max_version: 0,
346 unknown_tagged_fields: Vec::new(),
347 }
348 }
349}
350
351impl ApiVersion {
352 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
354 buf.put_i16(self.api_key);
355 buf.put_i16(self.min_version);
356 buf.put_i16(self.max_version);
357 if is_flexible(version) {
358 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
359 }
360 Ok(())
361 }
362
363 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
366 Self::decode_with_limits(buf, version, Limits::default())
367 }
368
369 pub fn decode_with_limits(
371 buf: &mut impl Buf,
372 version: i16,
373 limits: Limits,
374 ) -> Result<Self, DecodeError> {
375 let mut budget = limits.budget(buf.remaining());
376 Self::decode_with_budget(buf, version, &mut budget)
377 }
378
379 pub fn decode_with_budget(
381 buf: &mut impl Buf,
382 version: i16,
383 budget: &mut Budget,
384 ) -> Result<Self, DecodeError> {
385 let mut this = Self::default();
386 this.api_key = wire::get_i16(buf)?;
387 this.min_version = wire::get_i16(buf)?;
388 this.max_version = wire::get_i16(buf)?;
389 if is_flexible(version) {
390 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
391 }
392 Ok(this)
393 }
394}
395
396#[derive(Debug, Clone, PartialEq)]
398#[non_exhaustive]
399pub struct SupportedFeatureKey {
400 pub name: String,
402 pub min_version: i16,
404 pub max_version: i16,
406 pub unknown_tagged_fields: Vec<RawTaggedField>,
408}
409
410impl Default for SupportedFeatureKey {
411 fn default() -> Self {
412 Self {
413 name: String::new(),
414 min_version: 0,
415 max_version: 0,
416 unknown_tagged_fields: Vec::new(),
417 }
418 }
419}
420
421impl SupportedFeatureKey {
422 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
424 if version >= 3 {
425 if is_flexible(version) {
426 wire::put_compact_string(buf, &self.name);
427 } else {
428 wire::put_string(buf, &self.name)?;
429 }
430 }
431 if version >= 3 {
432 buf.put_i16(self.min_version);
433 }
434 if version >= 3 {
435 buf.put_i16(self.max_version);
436 }
437 if is_flexible(version) {
438 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
439 }
440 Ok(())
441 }
442
443 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
446 Self::decode_with_limits(buf, version, Limits::default())
447 }
448
449 pub fn decode_with_limits(
451 buf: &mut impl Buf,
452 version: i16,
453 limits: Limits,
454 ) -> Result<Self, DecodeError> {
455 let mut budget = limits.budget(buf.remaining());
456 Self::decode_with_budget(buf, version, &mut budget)
457 }
458
459 pub fn decode_with_budget(
461 buf: &mut impl Buf,
462 version: i16,
463 budget: &mut Budget,
464 ) -> Result<Self, DecodeError> {
465 let mut this = Self::default();
466 if version >= 3 {
467 this.name = if is_flexible(version) {
468 wire::get_compact_string(buf)?
469 } else {
470 wire::get_string(buf)?
471 };
472 }
473 if version >= 3 {
474 this.min_version = wire::get_i16(buf)?;
475 }
476 if version >= 3 {
477 this.max_version = wire::get_i16(buf)?;
478 }
479 if is_flexible(version) {
480 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
481 }
482 Ok(this)
483 }
484}
485
486#[derive(Debug, Clone, PartialEq)]
488#[non_exhaustive]
489pub struct FinalizedFeatureKey {
490 pub name: String,
492 pub max_version_level: i16,
494 pub min_version_level: i16,
496 pub unknown_tagged_fields: Vec<RawTaggedField>,
498}
499
500impl Default for FinalizedFeatureKey {
501 fn default() -> Self {
502 Self {
503 name: String::new(),
504 max_version_level: 0,
505 min_version_level: 0,
506 unknown_tagged_fields: Vec::new(),
507 }
508 }
509}
510
511impl FinalizedFeatureKey {
512 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
514 if version >= 3 {
515 if is_flexible(version) {
516 wire::put_compact_string(buf, &self.name);
517 } else {
518 wire::put_string(buf, &self.name)?;
519 }
520 }
521 if version >= 3 {
522 buf.put_i16(self.max_version_level);
523 }
524 if version >= 3 {
525 buf.put_i16(self.min_version_level);
526 }
527 if is_flexible(version) {
528 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
529 }
530 Ok(())
531 }
532
533 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
536 Self::decode_with_limits(buf, version, Limits::default())
537 }
538
539 pub fn decode_with_limits(
541 buf: &mut impl Buf,
542 version: i16,
543 limits: Limits,
544 ) -> Result<Self, DecodeError> {
545 let mut budget = limits.budget(buf.remaining());
546 Self::decode_with_budget(buf, version, &mut budget)
547 }
548
549 pub fn decode_with_budget(
551 buf: &mut impl Buf,
552 version: i16,
553 budget: &mut Budget,
554 ) -> Result<Self, DecodeError> {
555 let mut this = Self::default();
556 if version >= 3 {
557 this.name = if is_flexible(version) {
558 wire::get_compact_string(buf)?
559 } else {
560 wire::get_string(buf)?
561 };
562 }
563 if version >= 3 {
564 this.max_version_level = wire::get_i16(buf)?;
565 }
566 if version >= 3 {
567 this.min_version_level = wire::get_i16(buf)?;
568 }
569 if is_flexible(version) {
570 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
571 }
572 Ok(this)
573 }
574}