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