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