1use bytes::BufMut;
3
4use crate::{
5 DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6 owned::get_telemetry_subscriptions_response,
7 primitives::{
8 array::{array_len_prefix_len, get_array_len, put_array_len},
9 fixed::{get_bool, get_i8, get_i16, get_i32, put_bool, put_i8, put_i16, put_i32},
10 string_bytes::{compact_string_len, put_compact_string, put_string, string_len},
11 string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed},
12 uuid::{Uuid, get_uuid, put_uuid},
13 },
14 tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
15};
16pub const API_KEY: i16 = 71;
17pub const MIN_VERSION: i16 = 0;
18pub const MAX_VERSION: i16 = 0;
19pub const FLEXIBLE_MIN: i16 = 0;
20#[inline]
21#[must_use]
22pub fn is_flexible(version: i16) -> bool {
23 version >= FLEXIBLE_MIN
24}
25#[derive(Debug, Clone, PartialEq, Eq, Default)]
26pub struct GetTelemetrySubscriptionsResponse<'a> {
27 pub throttle_time_ms: i32,
28 pub error_code: i16,
29 pub client_instance_id: Uuid,
30 pub subscription_id: i32,
31 pub accepted_compression_types: Vec<i8>,
32 pub push_interval_ms: i32,
33 pub telemetry_max_bytes: i32,
34 pub delta_temporality: bool,
35 pub requested_metrics: Vec<&'a str>,
36 pub unknown_tagged_fields: UnknownTaggedFields,
37}
38impl<'a> GetTelemetrySubscriptionsResponse<'a> {
39 #[must_use]
40 pub fn to_owned(
41 &self,
42 ) -> get_telemetry_subscriptions_response::GetTelemetrySubscriptionsResponse {
43 get_telemetry_subscriptions_response::GetTelemetrySubscriptionsResponse {
44 throttle_time_ms: (self.throttle_time_ms),
45 error_code: (self.error_code),
46 client_instance_id: (self.client_instance_id),
47 subscription_id: (self.subscription_id),
48 accepted_compression_types: (self.accepted_compression_types).clone(),
49 push_interval_ms: (self.push_interval_ms),
50 telemetry_max_bytes: (self.telemetry_max_bytes),
51 delta_temporality: (self.delta_temporality),
52 requested_metrics: (self.requested_metrics)
53 .iter()
54 .map(|&s| s.to_owned())
55 .collect(),
56 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
57 }
58 }
59 fn encode_field_0<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
60 if version >= 0 {
61 put_i32(buf, self.throttle_time_ms);
62 }
63 }
64 fn encode_field_1<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
65 if version >= 0 {
66 put_i16(buf, self.error_code);
67 }
68 }
69 fn encode_field_2<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
70 if version >= 0 {
71 put_uuid(buf, self.client_instance_id);
72 }
73 }
74 fn encode_field_3<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
75 if version >= 0 {
76 put_i32(buf, self.subscription_id);
77 }
78 }
79 fn encode_field_4<B: BufMut>(
80 &self,
81 buf: &mut B,
82 version: i16,
83 flex: bool,
84 ) -> Result<(), ProtocolError> {
85 if version >= 0 {
86 {
87 put_array_len(buf, (self.accepted_compression_types).len(), flex)?;
88 for it in &self.accepted_compression_types {
89 put_i8(buf, *it);
90 }
91 }
92 }
93 Ok(())
94 }
95 fn encode_field_5<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
96 if version >= 0 {
97 put_i32(buf, self.push_interval_ms);
98 }
99 }
100 fn encode_field_6<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
101 if version >= 0 {
102 put_i32(buf, self.telemetry_max_bytes);
103 }
104 }
105 fn encode_field_7<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
106 if version >= 0 {
107 put_bool(buf, self.delta_temporality);
108 }
109 }
110 fn encode_field_8<B: BufMut>(
111 &self,
112 buf: &mut B,
113 version: i16,
114 flex: bool,
115 ) -> Result<(), ProtocolError> {
116 if version >= 0 {
117 {
118 put_array_len(buf, (self.requested_metrics).len(), flex)?;
119 for it in &self.requested_metrics {
120 if flex {
121 put_compact_string(buf, it)?;
122 } else {
123 put_string(buf, it)?;
124 }
125 }
126 }
127 }
128 Ok(())
129 }
130 fn encode_tagged_fields<B: BufMut>(
131 &self,
132 buf: &mut B,
133 _version: i16,
134 flex: bool,
135 ) -> Result<(), ProtocolError> {
136 if flex {
137 let tagged = WriteTaggedFields::new();
138 tagged.write(buf, &self.unknown_tagged_fields)?;
139 }
140 Ok(())
141 }
142 fn decode_field_0(
143 out: &mut Self,
144 buf: &mut &'a [u8],
145 version: i16,
146 _flex: bool,
147 ) -> Result<(), ProtocolError> {
148 if version >= 0 {
149 out.throttle_time_ms = get_i32(buf)?;
150 }
151 Ok(())
152 }
153 fn decode_field_1(
154 out: &mut Self,
155 buf: &mut &'a [u8],
156 version: i16,
157 _flex: bool,
158 ) -> Result<(), ProtocolError> {
159 if version >= 0 {
160 out.error_code = get_i16(buf)?;
161 }
162 Ok(())
163 }
164 fn decode_field_2(
165 out: &mut Self,
166 buf: &mut &'a [u8],
167 version: i16,
168 _flex: bool,
169 ) -> Result<(), ProtocolError> {
170 if version >= 0 {
171 out.client_instance_id = get_uuid(buf)?;
172 }
173 Ok(())
174 }
175 fn decode_field_3(
176 out: &mut Self,
177 buf: &mut &'a [u8],
178 version: i16,
179 _flex: bool,
180 ) -> Result<(), ProtocolError> {
181 if version >= 0 {
182 out.subscription_id = get_i32(buf)?;
183 }
184 Ok(())
185 }
186 fn decode_field_4(
187 out: &mut Self,
188 buf: &mut &'a [u8],
189 version: i16,
190 flex: bool,
191 ) -> Result<(), ProtocolError> {
192 if version >= 0 {
193 out.accepted_compression_types = {
194 let n = get_array_len(buf, flex)?;
195 let mut v = Vec::with_capacity(n);
196 for _ in 0..n {
197 v.push(get_i8(buf)?);
198 }
199 v
200 };
201 }
202 Ok(())
203 }
204 fn decode_field_5(
205 out: &mut Self,
206 buf: &mut &'a [u8],
207 version: i16,
208 _flex: bool,
209 ) -> Result<(), ProtocolError> {
210 if version >= 0 {
211 out.push_interval_ms = get_i32(buf)?;
212 }
213 Ok(())
214 }
215 fn decode_field_6(
216 out: &mut Self,
217 buf: &mut &'a [u8],
218 version: i16,
219 _flex: bool,
220 ) -> Result<(), ProtocolError> {
221 if version >= 0 {
222 out.telemetry_max_bytes = get_i32(buf)?;
223 }
224 Ok(())
225 }
226 fn decode_field_7(
227 out: &mut Self,
228 buf: &mut &'a [u8],
229 version: i16,
230 _flex: bool,
231 ) -> Result<(), ProtocolError> {
232 if version >= 0 {
233 out.delta_temporality = get_bool(buf)?;
234 }
235 Ok(())
236 }
237 fn decode_field_8(
238 out: &mut Self,
239 buf: &mut &'a [u8],
240 version: i16,
241 flex: bool,
242 ) -> Result<(), ProtocolError> {
243 if version >= 0 {
244 out.requested_metrics = {
245 let n = get_array_len(buf, flex)?;
246 let mut v = Vec::with_capacity(n);
247 for _ in 0..n {
248 v.push(if flex {
249 get_compact_string_borrowed(buf)?
250 } else {
251 get_string_borrowed(buf)?
252 });
253 }
254 v
255 };
256 }
257 Ok(())
258 }
259 fn decode_tagged_fields(
260 out: &mut Self,
261 buf: &mut &'a [u8],
262 _version: i16,
263 flex: bool,
264 ) -> Result<(), ProtocolError> {
265 if flex {
266 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
267 }
268 Ok(())
269 }
270}
271impl Encode for GetTelemetrySubscriptionsResponse<'_> {
272 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
273 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
274 return Err(ProtocolError::UnsupportedVersion {
275 api_key: API_KEY,
276 version,
277 });
278 }
279 let flex = is_flexible(version);
280 self.encode_field_0(buf, version, flex);
281 self.encode_field_1(buf, version, flex);
282 self.encode_field_2(buf, version, flex);
283 self.encode_field_3(buf, version, flex);
284 self.encode_field_4(buf, version, flex)?;
285 self.encode_field_5(buf, version, flex);
286 self.encode_field_6(buf, version, flex);
287 self.encode_field_7(buf, version, flex);
288 self.encode_field_8(buf, version, flex)?;
289 self.encode_tagged_fields(buf, version, flex)?;
290 Ok(())
291 }
292 fn encoded_len(&self, version: i16) -> usize {
293 let flex = is_flexible(version);
294 let mut n: usize = 0;
295 if version >= 0 {
296 n += 4;
297 }
298 if version >= 0 {
299 n += 2;
300 }
301 if version >= 0 {
302 n += 16;
303 }
304 if version >= 0 {
305 n += 4;
306 }
307 if version >= 0 {
308 n += {
309 let prefix = array_len_prefix_len((self.accepted_compression_types).len(), flex);
310 let body: usize = (self.accepted_compression_types).iter().map(|_| 1).sum();
311 prefix + body
312 };
313 }
314 if version >= 0 {
315 n += 4;
316 }
317 if version >= 0 {
318 n += 4;
319 }
320 if version >= 0 {
321 n += 1;
322 }
323 if version >= 0 {
324 n += {
325 let prefix = array_len_prefix_len((self.requested_metrics).len(), flex);
326 let body: usize = (self.requested_metrics)
327 .iter()
328 .map(|it| {
329 if flex {
330 compact_string_len(it)
331 } else {
332 string_len(it)
333 }
334 })
335 .sum();
336 prefix + body
337 };
338 }
339 if flex {
340 let known_pairs: Vec<(u32, usize)> = Vec::new();
341 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
342 }
343 n
344 }
345}
346impl<'de> DecodeBorrow<'de> for GetTelemetrySubscriptionsResponse<'de> {
347 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
348 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
349 return Err(ProtocolError::UnsupportedVersion {
350 api_key: API_KEY,
351 version,
352 });
353 }
354 let flex = is_flexible(version);
355 let mut out = Self::default();
356 Self::decode_field_0(&mut out, buf, version, flex)?;
357 Self::decode_field_1(&mut out, buf, version, flex)?;
358 Self::decode_field_2(&mut out, buf, version, flex)?;
359 Self::decode_field_3(&mut out, buf, version, flex)?;
360 Self::decode_field_4(&mut out, buf, version, flex)?;
361 Self::decode_field_5(&mut out, buf, version, flex)?;
362 Self::decode_field_6(&mut out, buf, version, flex)?;
363 Self::decode_field_7(&mut out, buf, version, flex)?;
364 Self::decode_field_8(&mut out, buf, version, flex)?;
365 Self::decode_tagged_fields(&mut out, buf, version, flex)?;
366 Ok(out)
367 }
368}
369#[cfg(test)]
370impl GetTelemetrySubscriptionsResponse<'_> {
371 #[must_use]
372 pub fn populated(version: i16) -> Self {
373 let mut m = Self::default();
374 if version >= 0 {
375 m.throttle_time_ms = 1i32;
376 }
377 if version >= 0 {
378 m.error_code = 1i16;
379 }
380 if version >= 0 {
381 m.client_instance_id = Uuid([1u8; 16]);
382 }
383 if version >= 0 {
384 m.subscription_id = 1i32;
385 }
386 if version >= 0 {
387 m.accepted_compression_types = vec![1i8];
388 }
389 if version >= 0 {
390 m.push_interval_ms = 1i32;
391 }
392 if version >= 0 {
393 m.telemetry_max_bytes = 1i32;
394 }
395 if version >= 0 {
396 m.delta_temporality = true;
397 }
398 if version >= 0 {
399 m.requested_metrics = vec!["x"];
400 }
401 m
402 }
403}