kafka_protocol/messages/
heartbeat_response.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 HeartbeatResponse {
24 pub throttle_time_ms: i32,
28
29 pub error_code: i16,
33
34 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
36}
37
38impl HeartbeatResponse {
39 pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
45 self.throttle_time_ms = value;
46 self
47 }
48 pub fn with_error_code(mut self, value: i16) -> Self {
54 self.error_code = value;
55 self
56 }
57 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
59 self.unknown_tagged_fields = value;
60 self
61 }
62 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
64 self.unknown_tagged_fields.insert(key, value);
65 self
66 }
67}
68
69#[cfg(feature = "broker")]
70impl Encodable for HeartbeatResponse {
71 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
72 if version < 0 || version > 4 {
73 bail!("specified version not supported by this message type");
74 }
75 if version >= 1 {
76 types::Int32.encode(buf, &self.throttle_time_ms)?;
77 }
78 types::Int16.encode(buf, &self.error_code)?;
79 if version >= 4 {
80 let num_tagged_fields = self.unknown_tagged_fields.len();
81 if num_tagged_fields > std::u32::MAX as usize {
82 bail!(
83 "Too many tagged fields to encode ({} fields)",
84 num_tagged_fields
85 );
86 }
87 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
88
89 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
90 }
91 Ok(())
92 }
93 fn compute_size(&self, version: i16) -> Result<usize> {
94 let mut total_size = 0;
95 if version >= 1 {
96 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
97 }
98 total_size += types::Int16.compute_size(&self.error_code)?;
99 if version >= 4 {
100 let num_tagged_fields = self.unknown_tagged_fields.len();
101 if num_tagged_fields > std::u32::MAX as usize {
102 bail!(
103 "Too many tagged fields to encode ({} fields)",
104 num_tagged_fields
105 );
106 }
107 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
108
109 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
110 }
111 Ok(total_size)
112 }
113}
114
115#[cfg(feature = "client")]
116impl Decodable for HeartbeatResponse {
117 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
118 if version < 0 || version > 4 {
119 bail!("specified version not supported by this message type");
120 }
121 let throttle_time_ms = if version >= 1 {
122 types::Int32.decode(buf)?
123 } else {
124 0
125 };
126 let error_code = types::Int16.decode(buf)?;
127 let mut unknown_tagged_fields = BTreeMap::new();
128 if version >= 4 {
129 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
130 for _ in 0..num_tagged_fields {
131 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
132 let size: u32 = types::UnsignedVarInt.decode(buf)?;
133 let unknown_value = buf.try_get_bytes(size as usize)?;
134 unknown_tagged_fields.insert(tag as i32, unknown_value);
135 }
136 }
137 Ok(Self {
138 throttle_time_ms,
139 error_code,
140 unknown_tagged_fields,
141 })
142 }
143}
144
145impl Default for HeartbeatResponse {
146 fn default() -> Self {
147 Self {
148 throttle_time_ms: 0,
149 error_code: 0,
150 unknown_tagged_fields: BTreeMap::new(),
151 }
152 }
153}
154
155impl Message for HeartbeatResponse {
156 const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
157 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
158}
159
160impl HeaderVersion for HeartbeatResponse {
161 fn header_version(version: i16) -> i16 {
162 if version >= 4 {
163 1
164 } else {
165 0
166 }
167 }
168}