1use bytes::BufMut;
4
5use crate::primitives::fixed::{get_bool, get_i16, get_i32, put_bool, put_i16, put_i32};
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 = 3;
19pub const MIN_VERSION: i16 = 0;
20pub const MAX_VERSION: i16 = 13;
21pub const FLEXIBLE_MIN: i16 = 9;
22
23#[inline]
24fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct MetadataResponse<'a> {
28 pub throttle_time_ms: i32,
29 pub brokers: Vec<MetadataResponseBroker<'a>>,
30 pub cluster_id: Option<&'a str>,
31 pub controller_id: i32,
32 pub topics: Vec<MetadataResponseTopic<'a>>,
33 pub cluster_authorized_operations: i32,
34 pub error_code: i16,
35 pub unknown_tagged_fields: UnknownTaggedFields,
36}
37
38impl<'a> Default for MetadataResponse<'a> {
39 fn default() -> Self {
40 Self {
41 throttle_time_ms: 0i32,
42 brokers: Vec::new(),
43 cluster_id: None,
44 controller_id: -1i32,
45 topics: Vec::new(),
46 cluster_authorized_operations: -2_147_483_648i32,
47 error_code: 0i16,
48 unknown_tagged_fields: Default::default(),
49 }
50 }
51}
52
53impl<'a> MetadataResponse<'a> {
54 pub fn to_owned(&self) -> crate::owned::metadata_response::MetadataResponse {
55 crate::owned::metadata_response::MetadataResponse {
56 throttle_time_ms: (self.throttle_time_ms),
57 brokers: (self.brokers).iter().map(|it| it.to_owned()).collect(),
58 cluster_id: (self.cluster_id).map(|s| s.to_string()),
59 controller_id: (self.controller_id),
60 topics: (self.topics).iter().map(|it| it.to_owned()).collect(),
61 cluster_authorized_operations: (self.cluster_authorized_operations),
62 error_code: (self.error_code),
63 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
64 }
65 }
66}
67
68impl<'a> Encode for MetadataResponse<'a> {
69 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
70 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
71 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
72 }
73 let flex = is_flexible(version);
74 if version >= 3 { put_i32(buf, self.throttle_time_ms) }
75 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.brokers).len(), flex); for it in &self.brokers { it.encode(buf, version)?; } } }
76 if version >= 2 { if flex { put_compact_nullable_string(buf, self.cluster_id) } else { put_nullable_string(buf, self.cluster_id) } }
77 if version >= 1 { put_i32(buf, self.controller_id) }
78 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { it.encode(buf, version)?; } } }
79 if version >= 8 && version <= 10 { put_i32(buf, self.cluster_authorized_operations) }
80 if version >= 13 { put_i16(buf, self.error_code) }
81 if flex {
82 let tagged = WriteTaggedFields::new();
83 tagged.write(buf, &self.unknown_tagged_fields);
84 }
85 Ok(())
86 }
87 fn encoded_len(&self, version: i16) -> usize {
88 let flex = is_flexible(version);
89 let mut n: usize = 0;
90 if version >= 3 { n += 4; }
91 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.brokers).len(), flex); let body: usize = (self.brokers).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
92 if version >= 2 { n += if flex { compact_nullable_string_len(self.cluster_id) } else { nullable_string_len(self.cluster_id) }; }
93 if version >= 1 { n += 4; }
94 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 }; }
95 if version >= 8 && version <= 10 { n += 4; }
96 if version >= 13 { n += 2; }
97 if flex {
98 let known_pairs: Vec<(u32, usize)> = Vec::new();
99 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
100 }
101 n
102 }
103}
104
105impl<'de> DecodeBorrow<'de> for MetadataResponse<'de> {
106 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
107 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
108 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
109 }
110 let flex = is_flexible(version);
111 let mut out = Self::default();
112 if version >= 3 { out.throttle_time_ms = get_i32(buf)?; }
113 if version >= 0 { out.brokers = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(MetadataResponseBroker::decode_borrow(buf, version)?); } v }; }
114 if version >= 2 { out.cluster_id = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
115 if version >= 1 { out.controller_id = get_i32(buf)?; }
116 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(MetadataResponseTopic::decode_borrow(buf, version)?); } v }; }
117 if version >= 8 && version <= 10 { out.cluster_authorized_operations = get_i32(buf)?; }
118 if version >= 13 { out.error_code = get_i16(buf)?; }
119 if flex {
120 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
121 Ok(false)
122 })?;
123 }
124 Ok(out)
125 }
126}
127
128#[derive(Debug, Clone, PartialEq, Eq)]
129pub struct MetadataResponseBroker<'a> {
130 pub node_id: i32,
131 pub host: &'a str,
132 pub port: i32,
133 pub rack: Option<&'a str>,
134 pub unknown_tagged_fields: UnknownTaggedFields,
135}
136
137impl<'a> Default for MetadataResponseBroker<'a> {
138 fn default() -> Self {
139 Self {
140 node_id: 0i32,
141 host: "",
142 port: 0i32,
143 rack: None,
144 unknown_tagged_fields: Default::default(),
145 }
146 }
147}
148
149impl<'a> MetadataResponseBroker<'a> {
150 pub fn to_owned(&self) -> crate::owned::metadata_response::MetadataResponseBroker {
151 crate::owned::metadata_response::MetadataResponseBroker {
152 node_id: (self.node_id),
153 host: (self.host).to_string(),
154 port: (self.port),
155 rack: (self.rack).map(|s| s.to_string()),
156 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
157 }
158 }
159}
160
161impl<'a> Encode for MetadataResponseBroker<'a> {
162 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
163 let flex = version >= 9;
164 if version >= 0 { put_i32(buf, self.node_id) }
165 if version >= 0 { if flex { put_compact_string(buf, self.host) } else { put_string(buf, self.host) } }
166 if version >= 0 { put_i32(buf, self.port) }
167 if version >= 1 { if flex { put_compact_nullable_string(buf, self.rack) } else { put_nullable_string(buf, self.rack) } }
168 if flex {
169 let tagged = WriteTaggedFields::new();
170 tagged.write(buf, &self.unknown_tagged_fields);
171 }
172 Ok(())
173 }
174 fn encoded_len(&self, version: i16) -> usize {
175 let flex = version >= 9;
176 let mut n: usize = 0;
177 if version >= 0 { n += 4; }
178 if version >= 0 { n += if flex { compact_string_len(self.host) } else { string_len(self.host) }; }
179 if version >= 0 { n += 4; }
180 if version >= 1 { n += if flex { compact_nullable_string_len(self.rack) } else { nullable_string_len(self.rack) }; }
181 if flex {
182 let known_pairs: Vec<(u32, usize)> = Vec::new();
183 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
184 }
185 n
186 }
187}
188
189impl<'de> DecodeBorrow<'de> for MetadataResponseBroker<'de> {
190 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
191 let flex = version >= 9;
192 let mut out = Self::default();
193 if version >= 0 { out.node_id = get_i32(buf)?; }
194 if version >= 0 { out.host = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
195 if version >= 0 { out.port = get_i32(buf)?; }
196 if version >= 1 { out.rack = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
197 if flex {
198 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
199 Ok(false)
200 })?;
201 }
202 Ok(out)
203 }
204}
205
206#[derive(Debug, Clone, PartialEq, Eq)]
207pub struct MetadataResponseTopic<'a> {
208 pub error_code: i16,
209 pub name: Option<&'a str>,
210 pub topic_id: crate::primitives::uuid::Uuid,
211 pub is_internal: bool,
212 pub partitions: Vec<MetadataResponsePartition>,
213 pub topic_authorized_operations: i32,
214 pub unknown_tagged_fields: UnknownTaggedFields,
215}
216
217impl<'a> Default for MetadataResponseTopic<'a> {
218 fn default() -> Self {
219 Self {
220 error_code: 0i16,
221 name: None,
222 topic_id: Default::default(),
223 is_internal: false,
224 partitions: Vec::new(),
225 topic_authorized_operations: -2_147_483_648i32,
226 unknown_tagged_fields: Default::default(),
227 }
228 }
229}
230
231impl<'a> MetadataResponseTopic<'a> {
232 pub fn to_owned(&self) -> crate::owned::metadata_response::MetadataResponseTopic {
233 crate::owned::metadata_response::MetadataResponseTopic {
234 error_code: (self.error_code),
235 name: (self.name).map(|s| s.to_string()),
236 topic_id: (self.topic_id),
237 is_internal: (self.is_internal),
238 partitions: (self.partitions).iter().map(|it| it.to_owned()).collect(),
239 topic_authorized_operations: (self.topic_authorized_operations),
240 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
241 }
242 }
243}
244
245impl<'a> Encode for MetadataResponseTopic<'a> {
246 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
247 let flex = version >= 9;
248 if version >= 0 { put_i16(buf, self.error_code) }
249 if version >= 0 { if version >= 12 { if flex { put_compact_nullable_string(buf, self.name) } else { put_nullable_string(buf, self.name) } } else { if flex { put_compact_string(buf, (self.name).unwrap_or("")) } else { put_string(buf, (self.name).unwrap_or("")) } } }
250 if version >= 10 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
251 if version >= 1 { put_bool(buf, self.is_internal) }
252 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { it.encode(buf, version)?; } } }
253 if version >= 8 { put_i32(buf, self.topic_authorized_operations) }
254 if flex {
255 let tagged = WriteTaggedFields::new();
256 tagged.write(buf, &self.unknown_tagged_fields);
257 }
258 Ok(())
259 }
260 fn encoded_len(&self, version: i16) -> usize {
261 let flex = version >= 9;
262 let mut n: usize = 0;
263 if version >= 0 { n += 2; }
264 if version >= 0 { n += if version >= 12 { if flex { compact_nullable_string_len(self.name) } else { nullable_string_len(self.name) } } else { if flex { compact_string_len((self.name).unwrap_or("")) } else { string_len((self.name).unwrap_or("")) } }; }
265 if version >= 10 { n += 16; }
266 if version >= 1 { n += 1; }
267 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 }; }
268 if version >= 8 { n += 4; }
269 if flex {
270 let known_pairs: Vec<(u32, usize)> = Vec::new();
271 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
272 }
273 n
274 }
275}
276
277impl<'de> DecodeBorrow<'de> for MetadataResponseTopic<'de> {
278 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
279 let flex = version >= 9;
280 let mut out = Self::default();
281 if version >= 0 { out.error_code = get_i16(buf)?; }
282 if version >= 0 { out.name = if version >= 12 { if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? } } else { Some(if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }) }; }
283 if version >= 10 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
284 if version >= 1 { out.is_internal = get_bool(buf)?; }
285 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(MetadataResponsePartition::decode_borrow(buf, version)?); } v }; }
286 if version >= 8 { out.topic_authorized_operations = get_i32(buf)?; }
287 if flex {
288 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
289 Ok(false)
290 })?;
291 }
292 Ok(out)
293 }
294}
295
296#[derive(Debug, Clone, PartialEq, Eq)]
297pub struct MetadataResponsePartition {
298 pub error_code: i16,
299 pub partition_index: i32,
300 pub leader_id: i32,
301 pub leader_epoch: i32,
302 pub replica_nodes: Vec<i32>,
303 pub isr_nodes: Vec<i32>,
304 pub offline_replicas: Vec<i32>,
305 pub unknown_tagged_fields: UnknownTaggedFields,
306}
307
308impl Default for MetadataResponsePartition {
309 fn default() -> Self {
310 Self {
311 error_code: 0i16,
312 partition_index: 0i32,
313 leader_id: 0i32,
314 leader_epoch: -1i32,
315 replica_nodes: Vec::new(),
316 isr_nodes: Vec::new(),
317 offline_replicas: Vec::new(),
318 unknown_tagged_fields: Default::default(),
319 }
320 }
321}
322
323impl MetadataResponsePartition {
324 pub fn to_owned(&self) -> crate::owned::metadata_response::MetadataResponsePartition {
325 crate::owned::metadata_response::MetadataResponsePartition {
326 error_code: (self.error_code),
327 partition_index: (self.partition_index),
328 leader_id: (self.leader_id),
329 leader_epoch: (self.leader_epoch),
330 replica_nodes: (self.replica_nodes).clone(),
331 isr_nodes: (self.isr_nodes).clone(),
332 offline_replicas: (self.offline_replicas).clone(),
333 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
334 }
335 }
336}
337
338impl Encode for MetadataResponsePartition {
339 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
340 let flex = version >= 9;
341 if version >= 0 { put_i16(buf, self.error_code) }
342 if version >= 0 { put_i32(buf, self.partition_index) }
343 if version >= 0 { put_i32(buf, self.leader_id) }
344 if version >= 7 { put_i32(buf, self.leader_epoch) }
345 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.replica_nodes).len(), flex); for it in &self.replica_nodes { put_i32(buf, *it); } } }
346 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.isr_nodes).len(), flex); for it in &self.isr_nodes { put_i32(buf, *it); } } }
347 if version >= 5 { { crate::primitives::array::put_array_len(buf, (self.offline_replicas).len(), flex); for it in &self.offline_replicas { put_i32(buf, *it); } } }
348 if flex {
349 let tagged = WriteTaggedFields::new();
350 tagged.write(buf, &self.unknown_tagged_fields);
351 }
352 Ok(())
353 }
354 fn encoded_len(&self, version: i16) -> usize {
355 let flex = version >= 9;
356 let mut n: usize = 0;
357 if version >= 0 { n += 2; }
358 if version >= 0 { n += 4; }
359 if version >= 0 { n += 4; }
360 if version >= 7 { n += 4; }
361 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.replica_nodes).len(), flex); let body: usize = (self.replica_nodes).iter().map(|_| 4).sum(); prefix + body }; }
362 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.isr_nodes).len(), flex); let body: usize = (self.isr_nodes).iter().map(|_| 4).sum(); prefix + body }; }
363 if version >= 5 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.offline_replicas).len(), flex); let body: usize = (self.offline_replicas).iter().map(|_| 4).sum(); prefix + body }; }
364 if flex {
365 let known_pairs: Vec<(u32, usize)> = Vec::new();
366 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
367 }
368 n
369 }
370}
371
372impl<'de> DecodeBorrow<'de> for MetadataResponsePartition {
373 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
374 let flex = version >= 9;
375 let mut out = Self::default();
376 if version >= 0 { out.error_code = get_i16(buf)?; }
377 if version >= 0 { out.partition_index = get_i32(buf)?; }
378 if version >= 0 { out.leader_id = get_i32(buf)?; }
379 if version >= 7 { out.leader_epoch = get_i32(buf)?; }
380 if version >= 0 { out.replica_nodes = { 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 }; }
381 if version >= 0 { out.isr_nodes = { 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 }; }
382 if version >= 5 { out.offline_replicas = { 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 }; }
383 if flex {
384 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
385 Ok(false)
386 })?;
387 }
388 Ok(out)
389 }
390}