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