odradek_protocol/messages/
init_producer_id_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 >= 2
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct InitProducerIdResponse {
31 pub throttle_time_ms: i32,
33 pub error_code: i16,
35 pub producer_id: i64,
37 pub producer_epoch: i16,
39 pub ongoing_txn_producer_id: i64,
41 pub ongoing_txn_producer_epoch: i16,
43 pub unknown_tagged_fields: Vec<RawTaggedField>,
45}
46
47impl Default for InitProducerIdResponse {
48 fn default() -> Self {
49 Self {
50 throttle_time_ms: 0,
51 error_code: 0,
52 producer_id: -1,
53 producer_epoch: 0,
54 ongoing_txn_producer_id: -1,
55 ongoing_txn_producer_epoch: -1,
56 unknown_tagged_fields: Vec::new(),
57 }
58 }
59}
60
61impl InitProducerIdResponse {
62 pub const API_KEY: i16 = 22;
64 pub const MIN_VERSION: i16 = 0;
66 pub const MAX_VERSION: i16 = 6;
68
69 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
71 buf.put_i32(self.throttle_time_ms);
72 buf.put_i16(self.error_code);
73 buf.put_i64(self.producer_id);
74 buf.put_i16(self.producer_epoch);
75 if version >= 6 {
76 buf.put_i64(self.ongoing_txn_producer_id);
77 }
78 if version >= 6 {
79 buf.put_i16(self.ongoing_txn_producer_epoch);
80 }
81 if is_flexible(version) {
82 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
83 }
84 Ok(())
85 }
86
87 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
90 Self::decode_with_limits(buf, version, Limits::default())
91 }
92
93 pub fn decode_with_limits(
95 buf: &mut impl Buf,
96 version: i16,
97 limits: Limits,
98 ) -> Result<Self, DecodeError> {
99 let mut budget = limits.budget(buf.remaining());
100 Self::decode_with_budget(buf, version, &mut budget)
101 }
102
103 pub fn decode_with_budget(
105 buf: &mut impl Buf,
106 version: i16,
107 budget: &mut Budget,
108 ) -> Result<Self, DecodeError> {
109 let mut this = Self::default();
110 this.throttle_time_ms = wire::get_i32(buf)?;
111 this.error_code = wire::get_i16(buf)?;
112 this.producer_id = wire::get_i64(buf)?;
113 this.producer_epoch = wire::get_i16(buf)?;
114 if version >= 6 {
115 this.ongoing_txn_producer_id = wire::get_i64(buf)?;
116 }
117 if version >= 6 {
118 this.ongoing_txn_producer_epoch = wire::get_i16(buf)?;
119 }
120 if is_flexible(version) {
121 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
122 }
123 Ok(this)
124 }
125}
126
127impl crate::Message for InitProducerIdResponse {
128 const API_KEY: i16 = 22;
129 const MIN_VERSION: i16 = 0;
130 const MAX_VERSION: i16 = 6;
131
132 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
133 InitProducerIdResponse::encode(self, buf, version)
134 }
135 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
136 InitProducerIdResponse::decode(buf, version)
137 }
138 fn decode_with_limits(
139 buf: &mut impl Buf,
140 version: i16,
141 limits: Limits,
142 ) -> Result<Self, DecodeError> {
143 InitProducerIdResponse::decode_with_limits(buf, version, limits)
144 }
145 fn throttle_time_ms(&self) -> Option<i32> {
146 Some(self.throttle_time_ms)
147 }
148}