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