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 >= 1 {
73 types::Int32.encode(buf, &self.throttle_time_ms)?;
74 }
75 types::Int16.encode(buf, &self.error_code)?;
76 if version >= 4 {
77 let num_tagged_fields = self.unknown_tagged_fields.len();
78 if num_tagged_fields > std::u32::MAX as usize {
79 bail!(
80 "Too many tagged fields to encode ({} fields)",
81 num_tagged_fields
82 );
83 }
84 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
85
86 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
87 }
88 Ok(())
89 }
90 fn compute_size(&self, version: i16) -> Result<usize> {
91 let mut total_size = 0;
92 if version >= 1 {
93 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
94 }
95 total_size += types::Int16.compute_size(&self.error_code)?;
96 if version >= 4 {
97 let num_tagged_fields = self.unknown_tagged_fields.len();
98 if num_tagged_fields > std::u32::MAX as usize {
99 bail!(
100 "Too many tagged fields to encode ({} fields)",
101 num_tagged_fields
102 );
103 }
104 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
105
106 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
107 }
108 Ok(total_size)
109 }
110}
111
112#[cfg(feature = "client")]
113impl Decodable for HeartbeatResponse {
114 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
115 let throttle_time_ms = if version >= 1 {
116 types::Int32.decode(buf)?
117 } else {
118 0
119 };
120 let error_code = types::Int16.decode(buf)?;
121 let mut unknown_tagged_fields = BTreeMap::new();
122 if version >= 4 {
123 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
124 for _ in 0..num_tagged_fields {
125 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
126 let size: u32 = types::UnsignedVarInt.decode(buf)?;
127 let unknown_value = buf.try_get_bytes(size as usize)?;
128 unknown_tagged_fields.insert(tag as i32, unknown_value);
129 }
130 }
131 Ok(Self {
132 throttle_time_ms,
133 error_code,
134 unknown_tagged_fields,
135 })
136 }
137}
138
139impl Default for HeartbeatResponse {
140 fn default() -> Self {
141 Self {
142 throttle_time_ms: 0,
143 error_code: 0,
144 unknown_tagged_fields: BTreeMap::new(),
145 }
146 }
147}
148
149impl Message for HeartbeatResponse {
150 const VERSIONS: VersionRange = VersionRange { min: 0, max: 4 };
151 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
152}
153
154impl HeaderVersion for HeartbeatResponse {
155 fn header_version(version: i16) -> i16 {
156 if version >= 4 {
157 1
158 } else {
159 0
160 }
161 }
162}