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