1use bytes::BufMut;
4
5use crate::primitives::fixed::{get_i32, get_u16, put_i32, put_u16};
6use crate::primitives::string_bytes::{
7 compact_nullable_string_len, compact_string_len, nullable_string_len,
8 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
9 string_len,
10};
11use crate::primitives::string_bytes_borrowed::{
12 get_compact_nullable_string_borrowed, get_compact_string_borrowed,
13 get_nullable_string_borrowed, get_string_borrowed,
14};
15use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
16use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
17
18pub const API_KEY: i16 = 53;
19pub const MIN_VERSION: i16 = 0;
20pub const MAX_VERSION: i16 = 1;
21pub const FLEXIBLE_MIN: i16 = 1;
22
23#[inline]
24fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct BeginQuorumEpochRequest<'a> {
28 pub cluster_id: Option<&'a str>,
29 pub voter_id: i32,
30 pub topics: Vec<TopicData<'a>>,
31 pub leader_endpoints: Vec<LeaderEndpoint<'a>>,
32 pub unknown_tagged_fields: UnknownTaggedFields,
33}
34
35impl<'a> Default for BeginQuorumEpochRequest<'a> {
36 fn default() -> Self {
37 Self {
38 cluster_id: None,
39 voter_id: -1i32,
40 topics: Vec::new(),
41 leader_endpoints: Vec::new(),
42 unknown_tagged_fields: Default::default(),
43 }
44 }
45}
46
47impl<'a> BeginQuorumEpochRequest<'a> {
48 pub fn to_owned(&self) -> crate::owned::begin_quorum_epoch_request::BeginQuorumEpochRequest {
49 crate::owned::begin_quorum_epoch_request::BeginQuorumEpochRequest {
50 cluster_id: (self.cluster_id).map(|s| s.to_string()),
51 voter_id: (self.voter_id),
52 topics: (self.topics).iter().map(|it| it.to_owned()).collect(),
53 leader_endpoints: (self.leader_endpoints).iter().map(|it| it.to_owned()).collect(),
54 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
55 }
56 }
57}
58
59impl<'a> Encode for BeginQuorumEpochRequest<'a> {
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 { api_key: API_KEY, version });
63 }
64 let flex = is_flexible(version);
65 if version >= 0 { if flex { put_compact_nullable_string(buf, self.cluster_id) } else { put_nullable_string(buf, self.cluster_id) } }
66 if version >= 1 { put_i32(buf, self.voter_id) }
67 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { it.encode(buf, version)?; } } }
68 if version >= 1 { { crate::primitives::array::put_array_len(buf, (self.leader_endpoints).len(), flex); for it in &self.leader_endpoints { it.encode(buf, version)?; } } }
69 if flex {
70 let tagged = WriteTaggedFields::new();
71 tagged.write(buf, &self.unknown_tagged_fields);
72 }
73 Ok(())
74 }
75 fn encoded_len(&self, version: i16) -> usize {
76 let flex = is_flexible(version);
77 let mut n: usize = 0;
78 if version >= 0 { n += if flex { compact_nullable_string_len(self.cluster_id) } else { nullable_string_len(self.cluster_id) }; }
79 if version >= 1 { n += 4; }
80 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.topics).len(), flex); let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
81 if version >= 1 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.leader_endpoints).len(), flex); let body: usize = (self.leader_endpoints).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
82 if flex {
83 let known_pairs: Vec<(u32, usize)> = Vec::new();
84 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
85 }
86 n
87 }
88}
89
90impl<'de> DecodeBorrow<'de> for BeginQuorumEpochRequest<'de> {
91 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
92 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
93 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
94 }
95 let flex = is_flexible(version);
96 let mut out = Self::default();
97 if version >= 0 { out.cluster_id = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
98 if version >= 1 { out.voter_id = get_i32(buf)?; }
99 if version >= 0 { out.topics = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(TopicData::decode_borrow(buf, version)?); } v }; }
100 if version >= 1 { out.leader_endpoints = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(LeaderEndpoint::decode_borrow(buf, version)?); } v }; }
101 if flex {
102 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
103 Ok(false)
104 })?;
105 }
106 Ok(out)
107 }
108}
109
110#[derive(Debug, Clone, PartialEq, Eq)]
111pub struct TopicData<'a> {
112 pub topic_name: &'a str,
113 pub partitions: Vec<PartitionData>,
114 pub unknown_tagged_fields: UnknownTaggedFields,
115}
116
117impl<'a> Default for TopicData<'a> {
118 fn default() -> Self {
119 Self {
120 topic_name: "",
121 partitions: Vec::new(),
122 unknown_tagged_fields: Default::default(),
123 }
124 }
125}
126
127impl<'a> TopicData<'a> {
128 pub fn to_owned(&self) -> crate::owned::begin_quorum_epoch_request::TopicData {
129 crate::owned::begin_quorum_epoch_request::TopicData {
130 topic_name: (self.topic_name).to_string(),
131 partitions: (self.partitions).iter().map(|it| it.to_owned()).collect(),
132 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
133 }
134 }
135}
136
137impl<'a> Encode for TopicData<'a> {
138 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
139 let flex = version >= 1;
140 if version >= 0 { if flex { put_compact_string(buf, self.topic_name) } else { put_string(buf, self.topic_name) } }
141 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { it.encode(buf, version)?; } } }
142 if flex {
143 let tagged = WriteTaggedFields::new();
144 tagged.write(buf, &self.unknown_tagged_fields);
145 }
146 Ok(())
147 }
148 fn encoded_len(&self, version: i16) -> usize {
149 let flex = version >= 1;
150 let mut n: usize = 0;
151 if version >= 0 { n += if flex { compact_string_len(self.topic_name) } else { string_len(self.topic_name) }; }
152 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex); let body: usize = (self.partitions).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
153 if flex {
154 let known_pairs: Vec<(u32, usize)> = Vec::new();
155 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
156 }
157 n
158 }
159}
160
161impl<'de> DecodeBorrow<'de> for TopicData<'de> {
162 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
163 let flex = version >= 1;
164 let mut out = Self::default();
165 if version >= 0 { out.topic_name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
166 if version >= 0 { out.partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(PartitionData::decode_borrow(buf, version)?); } v }; }
167 if flex {
168 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
169 Ok(false)
170 })?;
171 }
172 Ok(out)
173 }
174}
175
176#[derive(Debug, Clone, PartialEq, Eq)]
177pub struct PartitionData {
178 pub partition_index: i32,
179 pub voter_directory_id: crate::primitives::uuid::Uuid,
180 pub leader_id: i32,
181 pub leader_epoch: i32,
182 pub unknown_tagged_fields: UnknownTaggedFields,
183}
184
185impl Default for PartitionData {
186 fn default() -> Self {
187 Self {
188 partition_index: 0i32,
189 voter_directory_id: Default::default(),
190 leader_id: 0i32,
191 leader_epoch: 0i32,
192 unknown_tagged_fields: Default::default(),
193 }
194 }
195}
196
197impl PartitionData {
198 pub fn to_owned(&self) -> crate::owned::begin_quorum_epoch_request::PartitionData {
199 crate::owned::begin_quorum_epoch_request::PartitionData {
200 partition_index: (self.partition_index),
201 voter_directory_id: (self.voter_directory_id),
202 leader_id: (self.leader_id),
203 leader_epoch: (self.leader_epoch),
204 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
205 }
206 }
207}
208
209impl Encode for PartitionData {
210 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
211 let flex = version >= 1;
212 if version >= 0 { put_i32(buf, self.partition_index) }
213 if version >= 1 { crate::primitives::uuid::put_uuid(buf, self.voter_directory_id) }
214 if version >= 0 { put_i32(buf, self.leader_id) }
215 if version >= 0 { put_i32(buf, self.leader_epoch) }
216 if flex {
217 let tagged = WriteTaggedFields::new();
218 tagged.write(buf, &self.unknown_tagged_fields);
219 }
220 Ok(())
221 }
222 fn encoded_len(&self, version: i16) -> usize {
223 let flex = version >= 1;
224 let mut n: usize = 0;
225 if version >= 0 { n += 4; }
226 if version >= 1 { n += 16; }
227 if version >= 0 { n += 4; }
228 if version >= 0 { n += 4; }
229 if flex {
230 let known_pairs: Vec<(u32, usize)> = Vec::new();
231 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
232 }
233 n
234 }
235}
236
237impl<'de> DecodeBorrow<'de> for PartitionData {
238 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
239 let flex = version >= 1;
240 let mut out = Self::default();
241 if version >= 0 { out.partition_index = get_i32(buf)?; }
242 if version >= 1 { out.voter_directory_id = crate::primitives::uuid::get_uuid(buf)?; }
243 if version >= 0 { out.leader_id = get_i32(buf)?; }
244 if version >= 0 { out.leader_epoch = get_i32(buf)?; }
245 if flex {
246 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
247 Ok(false)
248 })?;
249 }
250 Ok(out)
251 }
252}
253
254#[derive(Debug, Clone, PartialEq, Eq)]
255pub struct LeaderEndpoint<'a> {
256 pub name: &'a str,
257 pub host: &'a str,
258 pub port: u16,
259 pub unknown_tagged_fields: UnknownTaggedFields,
260}
261
262impl<'a> Default for LeaderEndpoint<'a> {
263 fn default() -> Self {
264 Self {
265 name: "",
266 host: "",
267 port: 0u16,
268 unknown_tagged_fields: Default::default(),
269 }
270 }
271}
272
273impl<'a> LeaderEndpoint<'a> {
274 pub fn to_owned(&self) -> crate::owned::begin_quorum_epoch_request::LeaderEndpoint {
275 crate::owned::begin_quorum_epoch_request::LeaderEndpoint {
276 name: (self.name).to_string(),
277 host: (self.host).to_string(),
278 port: (self.port),
279 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
280 }
281 }
282}
283
284impl<'a> Encode for LeaderEndpoint<'a> {
285 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
286 let flex = version >= 1;
287 if version >= 1 { if flex { put_compact_string(buf, self.name) } else { put_string(buf, self.name) } }
288 if version >= 1 { if flex { put_compact_string(buf, self.host) } else { put_string(buf, self.host) } }
289 if version >= 1 { put_u16(buf, self.port) }
290 if flex {
291 let tagged = WriteTaggedFields::new();
292 tagged.write(buf, &self.unknown_tagged_fields);
293 }
294 Ok(())
295 }
296 fn encoded_len(&self, version: i16) -> usize {
297 let flex = version >= 1;
298 let mut n: usize = 0;
299 if version >= 1 { n += if flex { compact_string_len(self.name) } else { string_len(self.name) }; }
300 if version >= 1 { n += if flex { compact_string_len(self.host) } else { string_len(self.host) }; }
301 if version >= 1 { n += 2; }
302 if flex {
303 let known_pairs: Vec<(u32, usize)> = Vec::new();
304 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
305 }
306 n
307 }
308}
309
310impl<'de> DecodeBorrow<'de> for LeaderEndpoint<'de> {
311 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
312 let flex = version >= 1;
313 let mut out = Self::default();
314 if version >= 1 { out.name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
315 if version >= 1 { out.host = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
316 if version >= 1 { out.port = get_u16(buf)?; }
317 if flex {
318 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
319 Ok(false)
320 })?;
321 }
322 Ok(out)
323 }
324}