odradek_protocol/messages/
metadata_request.rs1#![allow(
6 clippy::field_reassign_with_default,
7 clippy::nonminimal_bool,
8 clippy::derivable_impls,
9 clippy::borrow_deref_ref,
10 clippy::double_parens,
11 unused_parens
12)]
13
14use bytes::{Buf, BufMut};
15
16use crate::budget::{Budget, Limits};
17#[allow(unused_imports)]
18use crate::error::{DecodeError, EncodeError};
19use crate::wire::{self, RawTaggedField};
20
21pub fn is_flexible(version: i16) -> bool {
23 let _ = version;
24 version >= 9
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct MetadataRequest {
31 pub topics: Option<Vec<MetadataRequestTopic>>,
33 pub allow_auto_topic_creation: bool,
35 pub include_cluster_authorized_operations: bool,
37 pub include_topic_authorized_operations: bool,
39 pub unknown_tagged_fields: Vec<RawTaggedField>,
41}
42
43impl Default for MetadataRequest {
44 fn default() -> Self {
45 Self {
46 topics: Some(Vec::new()),
47 allow_auto_topic_creation: true,
48 include_cluster_authorized_operations: false,
49 include_topic_authorized_operations: false,
50 unknown_tagged_fields: Vec::new(),
51 }
52 }
53}
54
55impl MetadataRequest {
56 pub const API_KEY: i16 = 3;
58 pub const MIN_VERSION: i16 = 0;
60 pub const MAX_VERSION: i16 = 13;
62
63 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
65 match &self.topics {
66 Some(items) => {
67 if is_flexible(version) {
68 wire::put_compact_array_len(buf, Some(items.len()));
69 } else {
70 wire::put_array_len(buf, Some(items.len()))?;
71 }
72 for item in items {
73 item.encode(buf, version)?;
74 }
75 }
76 None => {
77 if !(version >= 1) {
78 return Err(EncodeError::NullField("Topics"));
79 }
80 if is_flexible(version) {
81 wire::put_compact_array_len(buf, None);
82 } else {
83 wire::put_array_len(buf, None)?;
84 }
85 }
86 }
87 if version >= 4 {
88 buf.put_u8(u8::from(self.allow_auto_topic_creation));
89 }
90 if (8..=10).contains(&version) {
91 buf.put_u8(u8::from(self.include_cluster_authorized_operations));
92 }
93 if version >= 8 {
94 buf.put_u8(u8::from(self.include_topic_authorized_operations));
95 }
96 if is_flexible(version) {
97 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
98 }
99 Ok(())
100 }
101
102 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
105 Self::decode_with_limits(buf, version, Limits::default())
106 }
107
108 pub fn decode_with_limits(
110 buf: &mut impl Buf,
111 version: i16,
112 limits: Limits,
113 ) -> Result<Self, DecodeError> {
114 let mut budget = limits.budget(buf.remaining());
115 Self::decode_with_budget(buf, version, &mut budget)
116 }
117
118 pub fn decode_with_budget(
120 buf: &mut impl Buf,
121 version: i16,
122 budget: &mut Budget,
123 ) -> Result<Self, DecodeError> {
124 let mut this = Self::default();
125 this.topics = {
126 let len = if is_flexible(version) {
127 wire::get_compact_array_len(buf)?
128 } else {
129 wire::get_array_len(buf)?
130 };
131 match len {
132 None => {
133 if !(version >= 1) {
134 return Err(DecodeError::InvalidLength(-1));
135 }
136 None
137 }
138 Some(n) => {
139 let mut items = Vec::new();
140 for _ in 0..n {
141 let mark = buf.remaining();
142 let item = MetadataRequestTopic::decode_with_budget(buf, version, budget)?;
143 budget.progress(mark, buf.remaining())?;
144 budget.push(&mut items, item)?;
145 }
146 Some(items)
147 }
148 }
149 };
150 if version >= 4 {
151 this.allow_auto_topic_creation = wire::get_bool(buf)?;
152 }
153 if (8..=10).contains(&version) {
154 this.include_cluster_authorized_operations = wire::get_bool(buf)?;
155 }
156 if version >= 8 {
157 this.include_topic_authorized_operations = wire::get_bool(buf)?;
158 }
159 if is_flexible(version) {
160 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
161 }
162 Ok(this)
163 }
164}
165
166impl crate::Message for MetadataRequest {
167 const API_KEY: i16 = 3;
168 const MIN_VERSION: i16 = 0;
169 const MAX_VERSION: i16 = 13;
170
171 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
172 MetadataRequest::encode(self, buf, version)
173 }
174 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
175 MetadataRequest::decode(buf, version)
176 }
177 fn decode_with_limits(
178 buf: &mut impl Buf,
179 version: i16,
180 limits: Limits,
181 ) -> Result<Self, DecodeError> {
182 MetadataRequest::decode_with_limits(buf, version, limits)
183 }
184}
185
186#[derive(Debug, Clone, PartialEq)]
188#[non_exhaustive]
189pub struct MetadataRequestTopic {
190 pub topic_id: [u8; 16],
192 pub name: Option<String>,
194 pub unknown_tagged_fields: Vec<RawTaggedField>,
196}
197
198impl Default for MetadataRequestTopic {
199 fn default() -> Self {
200 Self {
201 topic_id: [0u8; 16],
202 name: Some(String::new()),
203 unknown_tagged_fields: Vec::new(),
204 }
205 }
206}
207
208impl MetadataRequestTopic {
209 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
211 if version >= 10 {
212 wire::put_uuid(buf, self.topic_id);
213 }
214 if self.name.is_none() && !(version >= 10) {
215 return Err(EncodeError::NullField("Name"));
216 }
217 if is_flexible(version) {
218 wire::put_compact_nullable_string(buf, self.name.as_deref());
219 } else {
220 wire::put_nullable_string(buf, self.name.as_deref())?;
221 }
222 if is_flexible(version) {
223 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
224 }
225 Ok(())
226 }
227
228 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
231 Self::decode_with_limits(buf, version, Limits::default())
232 }
233
234 pub fn decode_with_limits(
236 buf: &mut impl Buf,
237 version: i16,
238 limits: Limits,
239 ) -> Result<Self, DecodeError> {
240 let mut budget = limits.budget(buf.remaining());
241 Self::decode_with_budget(buf, version, &mut budget)
242 }
243
244 pub fn decode_with_budget(
246 buf: &mut impl Buf,
247 version: i16,
248 budget: &mut Budget,
249 ) -> Result<Self, DecodeError> {
250 let mut this = Self::default();
251 if version >= 10 {
252 this.topic_id = wire::get_uuid(buf)?;
253 }
254 this.name = {
255 let raw = if is_flexible(version) {
256 wire::get_compact_nullable_string(buf)?
257 } else {
258 wire::get_nullable_string(buf)?
259 };
260 if raw.is_none() && !(version >= 10) {
261 return Err(DecodeError::InvalidLength(-1));
262 }
263 raw
264 };
265 if is_flexible(version) {
266 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
267 }
268 Ok(this)
269 }
270}