1use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_bool, get_i16, get_i32, get_i8, put_bool, put_i16, put_i32};
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 = 75;
16pub const MIN_VERSION: i16 = 0;
17pub const MAX_VERSION: i16 = 0;
18pub const FLEXIBLE_MIN: i16 = 0;
19
20#[inline]
21fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
22
23#[derive(Debug, Clone, PartialEq, Eq, Default)]
24pub struct DescribeTopicPartitionsResponse {
25 pub throttle_time_ms: i32,
26 pub topics: Vec<DescribeTopicPartitionsResponseTopic>,
27 pub next_cursor: Option<Cursor>,
28 pub unknown_tagged_fields: UnknownTaggedFields,
29}
30
31impl Encode for DescribeTopicPartitionsResponse {
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 { put_i32(buf, self.throttle_time_ms) }
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 >= 0 { match &self.next_cursor { None => { buf.put_i8(-1); }, Some(v) => { buf.put_i8(1); v.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 += 4; }
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 >= 0 { n += 1 + self.next_cursor.as_ref().map_or(0, |v| v.encoded_len(version)); }
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 DescribeTopicPartitionsResponse {
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.throttle_time_ms = get_i32(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(DescribeTopicPartitionsResponseTopic::decode(buf, version)?); } v }; }
69 if version >= 0 { out.next_cursor = if get_i8(buf)? < 0 { None } else { Some(Cursor::decode(buf, version)?) }; }
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)]
80pub struct DescribeTopicPartitionsResponseTopic {
81 pub error_code: i16,
82 pub name: Option<String>,
83 pub topic_id: crate::primitives::uuid::Uuid,
84 pub is_internal: bool,
85 pub partitions: Vec<DescribeTopicPartitionsResponsePartition>,
86 pub topic_authorized_operations: i32,
87 pub unknown_tagged_fields: UnknownTaggedFields,
88}
89
90impl Default for DescribeTopicPartitionsResponseTopic {
91 fn default() -> Self {
92 Self {
93 error_code: 0i16,
94 name: None,
95 topic_id: Default::default(),
96 is_internal: false,
97 partitions: Vec::new(),
98 topic_authorized_operations: -2_147_483_648i32,
99 unknown_tagged_fields: Default::default(),
100 }
101 }
102}
103
104impl Encode for DescribeTopicPartitionsResponseTopic {
105 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
106 let flex = version >= 0;
107 if version >= 0 { put_i16(buf, self.error_code) }
108 if version >= 0 { if flex { put_compact_nullable_string(buf, self.name.as_deref()) } else { put_nullable_string(buf, self.name.as_deref()) } }
109 if version >= 0 { crate::primitives::uuid::put_uuid(buf, self.topic_id) }
110 if version >= 0 { put_bool(buf, self.is_internal) }
111 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { it.encode(buf, version)?; } } }
112 if version >= 0 { put_i32(buf, self.topic_authorized_operations) }
113 if flex {
114 let tagged = WriteTaggedFields::new();
115 tagged.write(buf, &self.unknown_tagged_fields);
116 }
117 Ok(())
118 }
119 fn encoded_len(&self, version: i16) -> usize {
120 let flex = version >= 0;
121 let mut n: usize = 0;
122 if version >= 0 { n += 2; }
123 if version >= 0 { n += if flex { compact_nullable_string_len(self.name.as_deref()) } else { nullable_string_len(self.name.as_deref()) }; }
124 if version >= 0 { n += 16; }
125 if version >= 0 { n += 1; }
126 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 }; }
127 if version >= 0 { n += 4; }
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}
135
136impl<'de> Decode<'de> for DescribeTopicPartitionsResponseTopic {
137 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
138 let flex = version >= 0;
139 let mut out = Self::default();
140 if version >= 0 { out.error_code = get_i16(buf)?; }
141 if version >= 0 { out.name = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
142 if version >= 0 { out.topic_id = crate::primitives::uuid::get_uuid(buf)?; }
143 if version >= 0 { out.is_internal = get_bool(buf)?; }
144 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(DescribeTopicPartitionsResponsePartition::decode(buf, version)?); } v }; }
145 if version >= 0 { out.topic_authorized_operations = get_i32(buf)?; }
146 if flex {
147 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
148 Ok(false)
149 })?;
150 }
151 Ok(out)
152 }
153}
154
155#[derive(Debug, Clone, PartialEq, Eq)]
156pub struct DescribeTopicPartitionsResponsePartition {
157 pub error_code: i16,
158 pub partition_index: i32,
159 pub leader_id: i32,
160 pub leader_epoch: i32,
161 pub replica_nodes: Vec<i32>,
162 pub isr_nodes: Vec<i32>,
163 pub eligible_leader_replicas: Option<Vec<i32>>,
164 pub last_known_elr: Option<Vec<i32>>,
165 pub offline_replicas: Vec<i32>,
166 pub unknown_tagged_fields: UnknownTaggedFields,
167}
168
169impl Default for DescribeTopicPartitionsResponsePartition {
170 fn default() -> Self {
171 Self {
172 error_code: 0i16,
173 partition_index: 0i32,
174 leader_id: 0i32,
175 leader_epoch: -1i32,
176 replica_nodes: Vec::new(),
177 isr_nodes: Vec::new(),
178 eligible_leader_replicas: None,
179 last_known_elr: None,
180 offline_replicas: Vec::new(),
181 unknown_tagged_fields: Default::default(),
182 }
183 }
184}
185
186impl Encode for DescribeTopicPartitionsResponsePartition {
187 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
188 let flex = version >= 0;
189 if version >= 0 { put_i16(buf, self.error_code) }
190 if version >= 0 { put_i32(buf, self.partition_index) }
191 if version >= 0 { put_i32(buf, self.leader_id) }
192 if version >= 0 { put_i32(buf, self.leader_epoch) }
193 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); } } }
194 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); } } }
195 if version >= 0 { { let len = (self.eligible_leader_replicas).as_ref().map(Vec::len); crate::primitives::array::put_nullable_array_len(buf, len, flex); if let Some(v) = &self.eligible_leader_replicas { for it in v { put_i32(buf, *it); } } } }
196 if version >= 0 { { let len = (self.last_known_elr).as_ref().map(Vec::len); crate::primitives::array::put_nullable_array_len(buf, len, flex); if let Some(v) = &self.last_known_elr { for it in v { put_i32(buf, *it); } } } }
197 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.offline_replicas).len(), flex); for it in &self.offline_replicas { put_i32(buf, *it); } } }
198 if flex {
199 let tagged = WriteTaggedFields::new();
200 tagged.write(buf, &self.unknown_tagged_fields);
201 }
202 Ok(())
203 }
204 fn encoded_len(&self, version: i16) -> usize {
205 let flex = version >= 0;
206 let mut n: usize = 0;
207 if version >= 0 { n += 2; }
208 if version >= 0 { n += 4; }
209 if version >= 0 { n += 4; }
210 if version >= 0 { n += 4; }
211 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 }; }
212 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 }; }
213 if version >= 0 { n += { let opt: Option<&Vec<_>> = (self.eligible_leader_replicas).as_ref(); let prefix = crate::primitives::array::nullable_array_len_prefix_len(opt.map(|v| v.len()), flex); let body: usize = opt.map_or(0, |v| v.iter().map(|_| 4).sum()); prefix + body }; }
214 if version >= 0 { n += { let opt: Option<&Vec<_>> = (self.last_known_elr).as_ref(); let prefix = crate::primitives::array::nullable_array_len_prefix_len(opt.map(|v| v.len()), flex); let body: usize = opt.map_or(0, |v| v.iter().map(|_| 4).sum()); prefix + body }; }
215 if version >= 0 { 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 }; }
216 if flex {
217 let known_pairs: Vec<(u32, usize)> = Vec::new();
218 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
219 }
220 n
221 }
222}
223
224impl<'de> Decode<'de> for DescribeTopicPartitionsResponsePartition {
225 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
226 let flex = version >= 0;
227 let mut out = Self::default();
228 if version >= 0 { out.error_code = get_i16(buf)?; }
229 if version >= 0 { out.partition_index = get_i32(buf)?; }
230 if version >= 0 { out.leader_id = get_i32(buf)?; }
231 if version >= 0 { out.leader_epoch = get_i32(buf)?; }
232 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 }; }
233 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 }; }
234 if version >= 0 { out.eligible_leader_replicas = { let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?; match opt { None => None, Some(n) => { let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(get_i32(buf)?); } Some(v) } } }; }
235 if version >= 0 { out.last_known_elr = { let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?; match opt { None => None, Some(n) => { let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(get_i32(buf)?); } Some(v) } } }; }
236 if version >= 0 { 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 }; }
237 if flex {
238 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
239 Ok(false)
240 })?;
241 }
242 Ok(out)
243 }
244}
245
246#[derive(Debug, Clone, PartialEq, Eq, Default)]
247pub struct Cursor {
248 pub topic_name: String,
249 pub partition_index: i32,
250 pub unknown_tagged_fields: UnknownTaggedFields,
251}
252
253impl Encode for Cursor {
254 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
255 let flex = version >= 0;
256 if version >= 0 { if flex { put_compact_string(buf, &self.topic_name) } else { put_string(buf, &self.topic_name) } }
257 if version >= 0 { put_i32(buf, self.partition_index) }
258 if flex {
259 let tagged = WriteTaggedFields::new();
260 tagged.write(buf, &self.unknown_tagged_fields);
261 }
262 Ok(())
263 }
264 fn encoded_len(&self, version: i16) -> usize {
265 let flex = version >= 0;
266 let mut n: usize = 0;
267 if version >= 0 { n += if flex { compact_string_len(&self.topic_name) } else { string_len(&self.topic_name) }; }
268 if version >= 0 { 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> Decode<'de> for Cursor {
278 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
279 let flex = version >= 0;
280 let mut out = Self::default();
281 if version >= 0 { out.topic_name = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
282 if version >= 0 { out.partition_index = get_i32(buf)?; }
283 if flex {
284 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
285 Ok(false)
286 })?;
287 }
288 Ok(out)
289 }
290}
291
292#[must_use]
295#[allow(unused_comparisons)]
296pub fn default_json(version: i16) -> ::serde_json::Value {
297 let mut obj = ::serde_json::Map::new();
298 obj.insert("throttleTimeMs".to_string(), ::serde_json::json!(0));
299 obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
300 obj.insert("nextCursor".to_string(), ::serde_json::Value::Null);
301 ::serde_json::Value::Object(obj)
302}