kafka_protocol/messages/
push_telemetry_request.rs1#![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 PushTelemetryRequest {
24 pub client_instance_id: Uuid,
28
29 pub subscription_id: i32,
33
34 pub terminating: bool,
38
39 pub compression_type: i8,
43
44 pub metrics: Bytes,
48
49 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
51}
52
53impl PushTelemetryRequest {
54 pub fn with_client_instance_id(mut self, value: Uuid) -> Self {
60 self.client_instance_id = value;
61 self
62 }
63 pub fn with_subscription_id(mut self, value: i32) -> Self {
69 self.subscription_id = value;
70 self
71 }
72 pub fn with_terminating(mut self, value: bool) -> Self {
78 self.terminating = value;
79 self
80 }
81 pub fn with_compression_type(mut self, value: i8) -> Self {
87 self.compression_type = value;
88 self
89 }
90 pub fn with_metrics(mut self, value: Bytes) -> Self {
96 self.metrics = value;
97 self
98 }
99 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
101 self.unknown_tagged_fields = value;
102 self
103 }
104 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
106 self.unknown_tagged_fields.insert(key, value);
107 self
108 }
109}
110
111#[cfg(feature = "client")]
112impl Encodable for PushTelemetryRequest {
113 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
114 if version != 0 {
115 bail!("specified version not supported by this message type");
116 }
117 types::Uuid.encode(buf, &self.client_instance_id)?;
118 types::Int32.encode(buf, &self.subscription_id)?;
119 types::Boolean.encode(buf, &self.terminating)?;
120 types::Int8.encode(buf, &self.compression_type)?;
121 types::CompactBytes.encode(buf, &self.metrics)?;
122 let num_tagged_fields = self.unknown_tagged_fields.len();
123 if num_tagged_fields > std::u32::MAX as usize {
124 bail!(
125 "Too many tagged fields to encode ({} fields)",
126 num_tagged_fields
127 );
128 }
129 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
130
131 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
132 Ok(())
133 }
134 fn compute_size(&self, version: i16) -> Result<usize> {
135 let mut total_size = 0;
136 total_size += types::Uuid.compute_size(&self.client_instance_id)?;
137 total_size += types::Int32.compute_size(&self.subscription_id)?;
138 total_size += types::Boolean.compute_size(&self.terminating)?;
139 total_size += types::Int8.compute_size(&self.compression_type)?;
140 total_size += types::CompactBytes.compute_size(&self.metrics)?;
141 let num_tagged_fields = self.unknown_tagged_fields.len();
142 if num_tagged_fields > std::u32::MAX as usize {
143 bail!(
144 "Too many tagged fields to encode ({} fields)",
145 num_tagged_fields
146 );
147 }
148 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
149
150 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
151 Ok(total_size)
152 }
153}
154
155#[cfg(feature = "broker")]
156impl Decodable for PushTelemetryRequest {
157 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
158 if version != 0 {
159 bail!("specified version not supported by this message type");
160 }
161 let client_instance_id = types::Uuid.decode(buf)?;
162 let subscription_id = types::Int32.decode(buf)?;
163 let terminating = types::Boolean.decode(buf)?;
164 let compression_type = types::Int8.decode(buf)?;
165 let metrics = types::CompactBytes.decode(buf)?;
166 let mut unknown_tagged_fields = BTreeMap::new();
167 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
168 for _ in 0..num_tagged_fields {
169 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
170 let size: u32 = types::UnsignedVarInt.decode(buf)?;
171 let unknown_value = buf.try_get_bytes(size as usize)?;
172 unknown_tagged_fields.insert(tag as i32, unknown_value);
173 }
174 Ok(Self {
175 client_instance_id,
176 subscription_id,
177 terminating,
178 compression_type,
179 metrics,
180 unknown_tagged_fields,
181 })
182 }
183}
184
185impl Default for PushTelemetryRequest {
186 fn default() -> Self {
187 Self {
188 client_instance_id: Uuid::nil(),
189 subscription_id: 0,
190 terminating: false,
191 compression_type: 0,
192 metrics: Default::default(),
193 unknown_tagged_fields: BTreeMap::new(),
194 }
195 }
196}
197
198impl Message for PushTelemetryRequest {
199 const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
200 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
201}
202
203impl HeaderVersion for PushTelemetryRequest {
204 fn header_version(version: i16) -> i16 {
205 2
206 }
207}