odradek_protocol/messages/
heartbeat_response.rs1#![allow(
6 clippy::field_reassign_with_default,
7 clippy::nonminimal_bool,
8 clippy::derivable_impls,
9 clippy::borrow_deref_ref,
10 clippy::double_parens,
11 unused_parens
12)]
13
14use bytes::{Buf, BufMut};
15
16use crate::budget::{Budget, Limits};
17#[allow(unused_imports)]
18use crate::error::{DecodeError, EncodeError};
19use crate::wire::{self, RawTaggedField};
20
21pub fn is_flexible(version: i16) -> bool {
23 let _ = version;
24 version >= 4
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct HeartbeatResponse {
31 pub throttle_time_ms: i32,
33 pub error_code: i16,
35 pub unknown_tagged_fields: Vec<RawTaggedField>,
37}
38
39impl Default for HeartbeatResponse {
40 fn default() -> Self {
41 Self {
42 throttle_time_ms: 0,
43 error_code: 0,
44 unknown_tagged_fields: Vec::new(),
45 }
46 }
47}
48
49impl HeartbeatResponse {
50 pub const API_KEY: i16 = 12;
52 pub const MIN_VERSION: i16 = 0;
54 pub const MAX_VERSION: i16 = 4;
56
57 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
59 if version >= 1 {
60 buf.put_i32(self.throttle_time_ms);
61 }
62 buf.put_i16(self.error_code);
63 if is_flexible(version) {
64 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
65 }
66 Ok(())
67 }
68
69 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
72 Self::decode_with_limits(buf, version, Limits::default())
73 }
74
75 pub fn decode_with_limits(
77 buf: &mut impl Buf,
78 version: i16,
79 limits: Limits,
80 ) -> Result<Self, DecodeError> {
81 let mut budget = limits.budget(buf.remaining());
82 Self::decode_with_budget(buf, version, &mut budget)
83 }
84
85 pub fn decode_with_budget(
87 buf: &mut impl Buf,
88 version: i16,
89 budget: &mut Budget,
90 ) -> Result<Self, DecodeError> {
91 let mut this = Self::default();
92 if version >= 1 {
93 this.throttle_time_ms = wire::get_i32(buf)?;
94 }
95 this.error_code = wire::get_i16(buf)?;
96 if is_flexible(version) {
97 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
98 }
99 Ok(this)
100 }
101}
102
103impl crate::Message for HeartbeatResponse {
104 const API_KEY: i16 = 12;
105 const MIN_VERSION: i16 = 0;
106 const MAX_VERSION: i16 = 4;
107
108 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
109 HeartbeatResponse::encode(self, buf, version)
110 }
111 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
112 HeartbeatResponse::decode(buf, version)
113 }
114 fn decode_with_limits(
115 buf: &mut impl Buf,
116 version: i16,
117 limits: Limits,
118 ) -> Result<Self, DecodeError> {
119 HeartbeatResponse::decode_with_limits(buf, version, limits)
120 }
121 fn throttle_time_ms(&self) -> Option<i32> {
122 Some(self.throttle_time_ms)
123 }
124}