crabka_protocol/opt/rustwide/workdir/generated/
MetadataRequest.owned.rs1use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_bool, put_bool};
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, string_len,
10};
11use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
12use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
13
14pub const API_KEY: i16 = 3;
15pub const MIN_VERSION: i16 = 0;
16pub const MAX_VERSION: i16 = 13;
17pub const FLEXIBLE_MIN: i16 = 9;
18
19#[inline]
20fn is_flexible(version: i16) -> bool {
21 version >= FLEXIBLE_MIN
22}
23
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct MetadataRequest {
26 pub topics: Option<Vec<MetadataRequestTopic>>,
27 pub allow_auto_topic_creation: bool,
28 pub include_cluster_authorized_operations: bool,
29 pub include_topic_authorized_operations: bool,
30 pub unknown_tagged_fields: UnknownTaggedFields,
31}
32impl Default for MetadataRequest {
33 fn default() -> Self {
34 Self {
35 topics: None,
36 allow_auto_topic_creation: true,
37 include_cluster_authorized_operations: false,
38 include_topic_authorized_operations: false,
39 unknown_tagged_fields: Default::default(),
40 }
41 }
42}
43impl Encode for MetadataRequest {
44 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
45 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
46 return Err(ProtocolError::UnsupportedVersion {
47 api_key: API_KEY,
48 version,
49 });
50 }
51 let flex = is_flexible(version);
52 if version >= 0 {
53 if version >= 1 {
54 {
55 let len = (self.topics).as_ref().map(Vec::len);
56 crate::primitives::array::put_nullable_array_len(buf, len, flex);
57 if let Some(v) = &self.topics {
58 for it in v {
59 it.encode(buf, version)?;
60 }
61 }
62 }
63 } else {
64 {
65 let v = (self.topics).as_deref().unwrap_or(&[]);
66 crate::primitives::array::put_array_len(buf, v.len(), flex);
67 for it in v {
68 it.encode(buf, version)?;
69 }
70 }
71 }
72 }
73 if version >= 4 {
74 put_bool(buf, self.allow_auto_topic_creation);
75 }
76 if (8..=10).contains(&version) {
77 put_bool(buf, self.include_cluster_authorized_operations);
78 }
79 if version >= 8 {
80 put_bool(buf, self.include_topic_authorized_operations);
81 }
82 if flex {
83 let tagged = WriteTaggedFields::new();
84 tagged.write(buf, &self.unknown_tagged_fields);
85 }
86 Ok(())
87 }
88 fn encoded_len(&self, version: i16) -> usize {
89 let flex = is_flexible(version);
90 let mut n: usize = 0;
91 if version >= 0 {
92 n += if version >= 1 {
93 {
94 let opt: Option<&Vec<_>> = (self.topics).as_ref();
95 let prefix = crate::primitives::array::nullable_array_len_prefix_len(
96 opt.map(std::vec::Vec::len),
97 flex,
98 );
99 let body: usize =
100 opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum());
101 prefix + body
102 }
103 } else {
104 {
105 let v = (self.topics).as_deref().unwrap_or(&[]);
106 let prefix = crate::primitives::array::array_len_prefix_len(v.len(), flex);
107 let body: usize = v.iter().map(|it| it.encoded_len(version)).sum();
108 prefix + body
109 }
110 };
111 }
112 if version >= 4 {
113 n += 1;
114 }
115 if (8..=10).contains(&version) {
116 n += 1;
117 }
118 if version >= 8 {
119 n += 1;
120 }
121 if flex {
122 let known_pairs: Vec<(u32, usize)> = Vec::new();
123 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
124 }
125 n
126 }
127}
128impl Decode<'_> for MetadataRequest {
129 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
130 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
131 return Err(ProtocolError::UnsupportedVersion {
132 api_key: API_KEY,
133 version,
134 });
135 }
136 let flex = is_flexible(version);
137 let mut out = Self::default();
138 if version >= 0 {
139 out.topics = if version >= 1 {
140 {
141 let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?;
142 match opt {
143 None => None,
144 Some(n) => {
145 let mut v = Vec::with_capacity(n);
146 for _ in 0..n {
147 v.push(MetadataRequestTopic::decode(buf, version)?);
148 }
149 Some(v)
150 }
151 }
152 }
153 } else {
154 Some({
155 let n = crate::primitives::array::get_array_len(buf, flex)?;
156 let mut v = Vec::with_capacity(n);
157 for _ in 0..n {
158 v.push(MetadataRequestTopic::decode(buf, version)?);
159 }
160 v
161 })
162 };
163 }
164 if version >= 4 {
165 out.allow_auto_topic_creation = get_bool(buf)?;
166 }
167 if (8..=10).contains(&version) {
168 out.include_cluster_authorized_operations = get_bool(buf)?;
169 }
170 if version >= 8 {
171 out.include_topic_authorized_operations = get_bool(buf)?;
172 }
173 if flex {
174 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
175 }
176 Ok(out)
177 }
178}
179#[cfg(test)]
180impl MetadataRequest {
181 #[must_use]
182 pub fn populated(version: i16) -> Self {
183 let mut m = Self::default();
184 if version >= 0 {
185 m.topics = Some(vec![MetadataRequestTopic::populated(version)]);
186 }
187 if version >= 4 {
188 m.allow_auto_topic_creation = true;
189 }
190 if (8..=10).contains(&version) {
191 m.include_cluster_authorized_operations = true;
192 }
193 if version >= 8 {
194 m.include_topic_authorized_operations = true;
195 }
196 m
197 }
198}
199#[derive(Debug, Clone, PartialEq, Eq, Default)]
200pub struct MetadataRequestTopic {
201 pub topic_id: crate::primitives::uuid::Uuid,
202 pub name: Option<String>,
203 pub unknown_tagged_fields: UnknownTaggedFields,
204}
205impl Encode for MetadataRequestTopic {
206 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
207 let flex = version >= 9;
208 if version >= 10 {
209 crate::primitives::uuid::put_uuid(buf, self.topic_id);
210 }
211 if version >= 0 {
212 if version >= 10 {
213 if flex {
214 put_compact_nullable_string(buf, self.name.as_deref());
215 } else {
216 put_nullable_string(buf, self.name.as_deref());
217 }
218 } else {
219 if flex {
220 put_compact_string(buf, (self.name).as_deref().unwrap_or(""));
221 } else {
222 put_string(buf, (self.name).as_deref().unwrap_or(""));
223 }
224 }
225 }
226 if flex {
227 let tagged = WriteTaggedFields::new();
228 tagged.write(buf, &self.unknown_tagged_fields);
229 }
230 Ok(())
231 }
232 fn encoded_len(&self, version: i16) -> usize {
233 let flex = version >= 9;
234 let mut n: usize = 0;
235 if version >= 10 {
236 n += 16;
237 }
238 if version >= 0 {
239 n += if version >= 10 {
240 if flex {
241 compact_nullable_string_len(self.name.as_deref())
242 } else {
243 nullable_string_len(self.name.as_deref())
244 }
245 } else {
246 if flex {
247 compact_string_len((self.name).as_deref().unwrap_or(""))
248 } else {
249 string_len((self.name).as_deref().unwrap_or(""))
250 }
251 };
252 }
253 if flex {
254 let known_pairs: Vec<(u32, usize)> = Vec::new();
255 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
256 }
257 n
258 }
259}
260impl Decode<'_> for MetadataRequestTopic {
261 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
262 let flex = version >= 9;
263 let mut out = Self::default();
264 if version >= 10 {
265 out.topic_id = crate::primitives::uuid::get_uuid(buf)?;
266 }
267 if version >= 0 {
268 out.name = if version >= 10 {
269 if flex {
270 get_compact_nullable_string_owned(buf)?
271 } else {
272 get_nullable_string_owned(buf)?
273 }
274 } else {
275 Some(if flex {
276 get_compact_string_owned(buf)?
277 } else {
278 get_string_owned(buf)?
279 })
280 };
281 }
282 if flex {
283 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
284 }
285 Ok(out)
286 }
287}
288#[cfg(test)]
289impl MetadataRequestTopic {
290 #[must_use]
291 pub fn populated(version: i16) -> Self {
292 let mut m = Self::default();
293 if version >= 10 {
294 m.topic_id = crate::primitives::uuid::Uuid([1u8; 16]);
295 }
296 if version >= 0 {
297 m.name = Some("x".to_string());
298 }
299 m
300 }
301}
302
303#[must_use]
306#[allow(unused_comparisons)]
307pub fn default_json(version: i16) -> ::serde_json::Value {
308 let mut obj = ::serde_json::Map::new();
309 obj.insert(
310 "topics".to_string(),
311 if version >= 1 {
312 ::serde_json::Value::Null
313 } else {
314 ::serde_json::Value::Array(vec![])
315 },
316 );
317 if version >= 4 {
318 obj.insert(
319 "allowAutoTopicCreation".to_string(),
320 ::serde_json::Value::Bool(true),
321 );
322 }
323 if (8..=10).contains(&version) {
324 obj.insert(
325 "includeClusterAuthorizedOperations".to_string(),
326 ::serde_json::Value::Bool(false),
327 );
328 }
329 if version >= 8 {
330 obj.insert(
331 "includeTopicAuthorizedOperations".to_string(),
332 ::serde_json::Value::Bool(false),
333 );
334 }
335 ::serde_json::Value::Object(obj)
336}
337
338impl crate::ProtocolRequest for MetadataRequest {
339 const API_KEY: i16 = API_KEY;
340 const MIN_VERSION: i16 = MIN_VERSION;
341 const MAX_VERSION: i16 = MAX_VERSION;
342 const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
343 type Response = super::metadata_response::MetadataResponse;
344}