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