1#![allow(unused)]
6
7use std::borrow::Borrow;
8use std::collections::BTreeMap;
9
10use anyhow::{bail, Result};
11use bytes::Bytes;
12use uuid::Uuid;
13
14use crate::protocol::{
15 buf::{ByteBuf, ByteBufMut},
16 compute_unknown_tagged_fields_size, types, write_unknown_tagged_fields, Decodable, Decoder,
17 Encodable, Encoder, HeaderVersion, Message, StrBytes, VersionRange,
18};
19
20#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct ComponentData {
24 pub entity_type: StrBytes,
28
29 pub match_type: i8,
33
34 pub _match: Option<StrBytes>,
38
39 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
41}
42
43impl ComponentData {
44 pub fn with_entity_type(mut self, value: StrBytes) -> Self {
50 self.entity_type = value;
51 self
52 }
53 pub fn with_match_type(mut self, value: i8) -> Self {
59 self.match_type = value;
60 self
61 }
62 pub fn with_match(mut self, value: Option<StrBytes>) -> Self {
68 self._match = value;
69 self
70 }
71 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
73 self.unknown_tagged_fields = value;
74 self
75 }
76 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
78 self.unknown_tagged_fields.insert(key, value);
79 self
80 }
81}
82
83#[cfg(feature = "client")]
84impl Encodable for ComponentData {
85 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
86 if version >= 1 {
87 types::CompactString.encode(buf, &self.entity_type)?;
88 } else {
89 types::String.encode(buf, &self.entity_type)?;
90 }
91 types::Int8.encode(buf, &self.match_type)?;
92 if version >= 1 {
93 types::CompactString.encode(buf, &self._match)?;
94 } else {
95 types::String.encode(buf, &self._match)?;
96 }
97 if version >= 1 {
98 let num_tagged_fields = self.unknown_tagged_fields.len();
99 if num_tagged_fields > std::u32::MAX as usize {
100 bail!(
101 "Too many tagged fields to encode ({} fields)",
102 num_tagged_fields
103 );
104 }
105 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
106
107 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
108 }
109 Ok(())
110 }
111 fn compute_size(&self, version: i16) -> Result<usize> {
112 let mut total_size = 0;
113 if version >= 1 {
114 total_size += types::CompactString.compute_size(&self.entity_type)?;
115 } else {
116 total_size += types::String.compute_size(&self.entity_type)?;
117 }
118 total_size += types::Int8.compute_size(&self.match_type)?;
119 if version >= 1 {
120 total_size += types::CompactString.compute_size(&self._match)?;
121 } else {
122 total_size += types::String.compute_size(&self._match)?;
123 }
124 if version >= 1 {
125 let num_tagged_fields = self.unknown_tagged_fields.len();
126 if num_tagged_fields > std::u32::MAX as usize {
127 bail!(
128 "Too many tagged fields to encode ({} fields)",
129 num_tagged_fields
130 );
131 }
132 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
133
134 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
135 }
136 Ok(total_size)
137 }
138}
139
140#[cfg(feature = "broker")]
141impl Decodable for ComponentData {
142 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
143 let entity_type = if version >= 1 {
144 types::CompactString.decode(buf)?
145 } else {
146 types::String.decode(buf)?
147 };
148 let match_type = types::Int8.decode(buf)?;
149 let _match = if version >= 1 {
150 types::CompactString.decode(buf)?
151 } else {
152 types::String.decode(buf)?
153 };
154 let mut unknown_tagged_fields = BTreeMap::new();
155 if version >= 1 {
156 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
157 for _ in 0..num_tagged_fields {
158 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
159 let size: u32 = types::UnsignedVarInt.decode(buf)?;
160 let unknown_value = buf.try_get_bytes(size as usize)?;
161 unknown_tagged_fields.insert(tag as i32, unknown_value);
162 }
163 }
164 Ok(Self {
165 entity_type,
166 match_type,
167 _match,
168 unknown_tagged_fields,
169 })
170 }
171}
172
173impl Default for ComponentData {
174 fn default() -> Self {
175 Self {
176 entity_type: Default::default(),
177 match_type: 0,
178 _match: Some(Default::default()),
179 unknown_tagged_fields: BTreeMap::new(),
180 }
181 }
182}
183
184impl Message for ComponentData {
185 const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
186 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
187}
188
189#[non_exhaustive]
191#[derive(Debug, Clone, PartialEq)]
192pub struct DescribeClientQuotasRequest {
193 pub components: Vec<ComponentData>,
197
198 pub strict: bool,
202
203 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
205}
206
207impl DescribeClientQuotasRequest {
208 pub fn with_components(mut self, value: Vec<ComponentData>) -> Self {
214 self.components = value;
215 self
216 }
217 pub fn with_strict(mut self, value: bool) -> Self {
223 self.strict = value;
224 self
225 }
226 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
228 self.unknown_tagged_fields = value;
229 self
230 }
231 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
233 self.unknown_tagged_fields.insert(key, value);
234 self
235 }
236}
237
238#[cfg(feature = "client")]
239impl Encodable for DescribeClientQuotasRequest {
240 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
241 if version >= 1 {
242 types::CompactArray(types::Struct { version }).encode(buf, &self.components)?;
243 } else {
244 types::Array(types::Struct { version }).encode(buf, &self.components)?;
245 }
246 types::Boolean.encode(buf, &self.strict)?;
247 if version >= 1 {
248 let num_tagged_fields = self.unknown_tagged_fields.len();
249 if num_tagged_fields > std::u32::MAX as usize {
250 bail!(
251 "Too many tagged fields to encode ({} fields)",
252 num_tagged_fields
253 );
254 }
255 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
256
257 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
258 }
259 Ok(())
260 }
261 fn compute_size(&self, version: i16) -> Result<usize> {
262 let mut total_size = 0;
263 if version >= 1 {
264 total_size +=
265 types::CompactArray(types::Struct { version }).compute_size(&self.components)?;
266 } else {
267 total_size += types::Array(types::Struct { version }).compute_size(&self.components)?;
268 }
269 total_size += types::Boolean.compute_size(&self.strict)?;
270 if version >= 1 {
271 let num_tagged_fields = self.unknown_tagged_fields.len();
272 if num_tagged_fields > std::u32::MAX as usize {
273 bail!(
274 "Too many tagged fields to encode ({} fields)",
275 num_tagged_fields
276 );
277 }
278 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
279
280 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
281 }
282 Ok(total_size)
283 }
284}
285
286#[cfg(feature = "broker")]
287impl Decodable for DescribeClientQuotasRequest {
288 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
289 let components = if version >= 1 {
290 types::CompactArray(types::Struct { version }).decode(buf)?
291 } else {
292 types::Array(types::Struct { version }).decode(buf)?
293 };
294 let strict = types::Boolean.decode(buf)?;
295 let mut unknown_tagged_fields = BTreeMap::new();
296 if version >= 1 {
297 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
298 for _ in 0..num_tagged_fields {
299 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
300 let size: u32 = types::UnsignedVarInt.decode(buf)?;
301 let unknown_value = buf.try_get_bytes(size as usize)?;
302 unknown_tagged_fields.insert(tag as i32, unknown_value);
303 }
304 }
305 Ok(Self {
306 components,
307 strict,
308 unknown_tagged_fields,
309 })
310 }
311}
312
313impl Default for DescribeClientQuotasRequest {
314 fn default() -> Self {
315 Self {
316 components: Default::default(),
317 strict: false,
318 unknown_tagged_fields: BTreeMap::new(),
319 }
320 }
321}
322
323impl Message for DescribeClientQuotasRequest {
324 const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
325 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
326}
327
328impl HeaderVersion for DescribeClientQuotasRequest {
329 fn header_version(version: i16) -> i16 {
330 if version >= 1 {
331 2
332 } else {
333 1
334 }
335 }
336}