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