odradek_protocol/messages/
init_producer_id_request.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 InitProducerIdRequest {
31 pub transactional_id: Option<String>,
33 pub transaction_timeout_ms: i32,
35 pub producer_id: i64,
37 pub producer_epoch: i16,
39 pub enable2_pc: bool,
41 pub keep_prepared_txn: bool,
43 pub unknown_tagged_fields: Vec<RawTaggedField>,
45}
46
47impl Default for InitProducerIdRequest {
48 fn default() -> Self {
49 Self {
50 transactional_id: Some(String::new()),
51 transaction_timeout_ms: 0,
52 producer_id: -1,
53 producer_epoch: -1,
54 enable2_pc: false,
55 keep_prepared_txn: false,
56 unknown_tagged_fields: Vec::new(),
57 }
58 }
59}
60
61impl InitProducerIdRequest {
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 if is_flexible(version) {
72 wire::put_compact_nullable_string(buf, self.transactional_id.as_deref());
73 } else {
74 wire::put_nullable_string(buf, self.transactional_id.as_deref())?;
75 }
76 buf.put_i32(self.transaction_timeout_ms);
77 if version >= 3 {
78 buf.put_i64(self.producer_id);
79 }
80 if version >= 3 {
81 buf.put_i16(self.producer_epoch);
82 }
83 if version >= 6 {
84 buf.put_u8(u8::from(self.enable2_pc));
85 }
86 if version >= 6 {
87 buf.put_u8(u8::from(self.keep_prepared_txn));
88 }
89 if is_flexible(version) {
90 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
91 }
92 Ok(())
93 }
94
95 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
98 Self::decode_with_limits(buf, version, Limits::default())
99 }
100
101 pub fn decode_with_limits(
103 buf: &mut impl Buf,
104 version: i16,
105 limits: Limits,
106 ) -> Result<Self, DecodeError> {
107 let mut budget = limits.budget(buf.remaining());
108 Self::decode_with_budget(buf, version, &mut budget)
109 }
110
111 pub fn decode_with_budget(
113 buf: &mut impl Buf,
114 version: i16,
115 budget: &mut Budget,
116 ) -> Result<Self, DecodeError> {
117 let mut this = Self::default();
118 this.transactional_id = if is_flexible(version) {
119 wire::get_compact_nullable_string(buf)?
120 } else {
121 wire::get_nullable_string(buf)?
122 };
123 this.transaction_timeout_ms = wire::get_i32(buf)?;
124 if version >= 3 {
125 this.producer_id = wire::get_i64(buf)?;
126 }
127 if version >= 3 {
128 this.producer_epoch = wire::get_i16(buf)?;
129 }
130 if version >= 6 {
131 this.enable2_pc = wire::get_bool(buf)?;
132 }
133 if version >= 6 {
134 this.keep_prepared_txn = wire::get_bool(buf)?;
135 }
136 if is_flexible(version) {
137 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
138 }
139 Ok(this)
140 }
141}
142
143impl crate::Message for InitProducerIdRequest {
144 const API_KEY: i16 = 22;
145 const MIN_VERSION: i16 = 0;
146 const MAX_VERSION: i16 = 6;
147
148 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
149 InitProducerIdRequest::encode(self, buf, version)
150 }
151 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
152 InitProducerIdRequest::decode(buf, version)
153 }
154 fn decode_with_limits(
155 buf: &mut impl Buf,
156 version: i16,
157 limits: Limits,
158 ) -> Result<Self, DecodeError> {
159 InitProducerIdRequest::decode_with_limits(buf, version, limits)
160 }
161}