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