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