krabka_protocol/opt/rustwide/workdir/generated/
InitProducerIdRequest.owned.rs1use bytes::{Buf, BufMut};
4
5use crate::{
6 Decode, Encode, ProtocolError, UnknownTaggedFields,
7 primitives::{
8 fixed::{get_bool, get_i16, get_i32, get_i64, put_bool, put_i16, put_i32, put_i64},
9 string_bytes::{
10 compact_nullable_string_len, get_compact_nullable_string_owned,
11 get_nullable_string_owned, nullable_string_len, put_compact_nullable_string,
12 put_nullable_string,
13 },
14 },
15 tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
16};
17pub const API_KEY: i16 = 22;
18pub const MIN_VERSION: i16 = 0;
19pub const MAX_VERSION: i16 = 6;
20pub const LATEST_STABLE_VERSION: i16 = 5;
21pub const FLEXIBLE_MIN: i16 = 2;
22#[inline]
23#[must_use]
24pub fn is_flexible(version: i16) -> bool {
25 version >= FLEXIBLE_MIN
26}
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct InitProducerIdRequest {
29 pub transactional_id: Option<String>,
30 pub transaction_timeout_ms: i32,
31 pub producer_id: i64,
32 pub producer_epoch: i16,
33 pub enable2_pc: bool,
34 pub keep_prepared_txn: bool,
35 pub unknown_tagged_fields: UnknownTaggedFields,
36}
37impl Default for InitProducerIdRequest {
38 fn default() -> Self {
39 Self {
40 transactional_id: Some(String::new()),
41 transaction_timeout_ms: 0i32,
42 producer_id: -1i64,
43 producer_epoch: -1i16,
44 enable2_pc: false,
45 keep_prepared_txn: false,
46 unknown_tagged_fields: UnknownTaggedFields::default(),
47 }
48 }
49}
50impl Encode for InitProducerIdRequest {
51 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
52 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
53 return Err(ProtocolError::UnsupportedVersion {
54 api_key: API_KEY,
55 version,
56 });
57 }
58 let flex = is_flexible(version);
59 if version >= 0 {
60 if flex {
61 let () = put_compact_nullable_string(buf, self.transactional_id.as_deref());
62 } else {
63 let () = put_nullable_string(buf, self.transactional_id.as_deref());
64 }
65 }
66 if version >= 0 {
67 put_i32(buf, self.transaction_timeout_ms);
68 }
69 if version >= 3 {
70 put_i64(buf, self.producer_id);
71 }
72 if version >= 3 {
73 put_i16(buf, self.producer_epoch);
74 }
75 if version >= 6 {
76 put_bool(buf, self.enable2_pc);
77 }
78 if version >= 6 {
79 put_bool(buf, self.keep_prepared_txn);
80 }
81 if flex {
82 let tagged = WriteTaggedFields::new();
83 tagged.write(buf, &self.unknown_tagged_fields);
84 }
85 Ok(())
86 }
87 fn encoded_len(&self, version: i16) -> usize {
88 let flex = is_flexible(version);
89 let mut n: usize = 0;
90 if version >= 0 {
91 n += if flex {
92 compact_nullable_string_len(self.transactional_id.as_deref())
93 } else {
94 nullable_string_len(self.transactional_id.as_deref())
95 };
96 }
97 if version >= 0 {
98 n += 4;
99 }
100 if version >= 3 {
101 n += 8;
102 }
103 if version >= 3 {
104 n += 2;
105 }
106 if version >= 6 {
107 n += 1;
108 }
109 if version >= 6 {
110 n += 1;
111 }
112 if flex {
113 let known_pairs: Vec<(u32, usize)> = Vec::new();
114 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
115 }
116 n
117 }
118}
119impl Decode<'_> for InitProducerIdRequest {
120 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
121 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
122 return Err(ProtocolError::UnsupportedVersion {
123 api_key: API_KEY,
124 version,
125 });
126 }
127 let flex = is_flexible(version);
128 let mut out = Self::default();
129 if version >= 0 {
130 out.transactional_id = if flex {
131 get_compact_nullable_string_owned(buf)?
132 } else {
133 get_nullable_string_owned(buf)?
134 };
135 }
136 if version >= 0 {
137 out.transaction_timeout_ms = get_i32(buf)?;
138 }
139 if version >= 3 {
140 out.producer_id = get_i64(buf)?;
141 }
142 if version >= 3 {
143 out.producer_epoch = get_i16(buf)?;
144 }
145 if version >= 6 {
146 out.enable2_pc = get_bool(buf)?;
147 }
148 if version >= 6 {
149 out.keep_prepared_txn = get_bool(buf)?;
150 }
151 if flex {
152 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
153 }
154 Ok(out)
155 }
156}
157#[cfg(test)]
158impl InitProducerIdRequest {
159 #[must_use]
160 pub fn populated(version: i16) -> Self {
161 let mut m = Self::default();
162 if version >= 0 {
163 m.transactional_id = Some("x".to_string());
164 }
165 if version >= 0 {
166 m.transaction_timeout_ms = 1i32;
167 }
168 if version >= 3 {
169 m.producer_id = 1i64;
170 }
171 if version >= 3 {
172 m.producer_epoch = 1i16;
173 }
174 if version >= 6 {
175 m.enable2_pc = true;
176 }
177 if version >= 6 {
178 m.keep_prepared_txn = true;
179 }
180 m
181 }
182}
183#[must_use]
186#[allow(unused_comparisons)]
187pub fn default_json(version: i16) -> ::serde_json::Value {
188 let mut obj = ::serde_json::Map::new();
189 obj.insert(
190 "transactionalId".to_string(),
191 ::serde_json::Value::String(String::new()),
192 );
193 obj.insert("transactionTimeoutMs".to_string(), ::serde_json::json!(0));
194 if version >= 3 {
195 obj.insert("producerId".to_string(), ::serde_json::json!(-1));
196 }
197 if version >= 3 {
198 obj.insert("producerEpoch".to_string(), ::serde_json::json!(-1));
199 }
200 if version >= 6 {
201 obj.insert("enable2Pc".to_string(), ::serde_json::Value::Bool(false));
202 }
203 if version >= 6 {
204 obj.insert(
205 "keepPreparedTxn".to_string(),
206 ::serde_json::Value::Bool(false),
207 );
208 }
209 ::serde_json::Value::Object(obj)
210}
211impl crate::ProtocolRequest for InitProducerIdRequest {
212 const API_KEY: i16 = API_KEY;
213 const MIN_VERSION: i16 = MIN_VERSION;
214 const MAX_VERSION: i16 = MAX_VERSION;
215 const LATEST_STABLE_VERSION: i16 = LATEST_STABLE_VERSION;
216 const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
217 type Response = super::init_producer_id_response::InitProducerIdResponse;
218}