odradek_protocol/messages/
heartbeat_request.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 HeartbeatRequest {
31 pub group_id: String,
33 pub generation_id: i32,
35 pub member_id: String,
37 pub group_instance_id: Option<String>,
39 pub unknown_tagged_fields: Vec<RawTaggedField>,
41}
42
43impl Default for HeartbeatRequest {
44 fn default() -> Self {
45 Self {
46 group_id: String::new(),
47 generation_id: 0,
48 member_id: String::new(),
49 group_instance_id: None,
50 unknown_tagged_fields: Vec::new(),
51 }
52 }
53}
54
55impl HeartbeatRequest {
56 pub const API_KEY: i16 = 12;
58 pub const MIN_VERSION: i16 = 0;
60 pub const MAX_VERSION: i16 = 4;
62
63 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
65 if is_flexible(version) {
66 wire::put_compact_string(buf, &self.group_id);
67 } else {
68 wire::put_string(buf, &self.group_id)?;
69 }
70 buf.put_i32(self.generation_id);
71 if is_flexible(version) {
72 wire::put_compact_string(buf, &self.member_id);
73 } else {
74 wire::put_string(buf, &self.member_id)?;
75 }
76 if version >= 3 {
77 if is_flexible(version) {
78 wire::put_compact_nullable_string(buf, self.group_instance_id.as_deref());
79 } else {
80 wire::put_nullable_string(buf, self.group_instance_id.as_deref())?;
81 }
82 }
83 if is_flexible(version) {
84 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
85 }
86 Ok(())
87 }
88
89 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
92 Self::decode_with_limits(buf, version, Limits::default())
93 }
94
95 pub fn decode_with_limits(
97 buf: &mut impl Buf,
98 version: i16,
99 limits: Limits,
100 ) -> Result<Self, DecodeError> {
101 let mut budget = limits.budget(buf.remaining());
102 Self::decode_with_budget(buf, version, &mut budget)
103 }
104
105 pub fn decode_with_budget(
107 buf: &mut impl Buf,
108 version: i16,
109 budget: &mut Budget,
110 ) -> Result<Self, DecodeError> {
111 let mut this = Self::default();
112 this.group_id = if is_flexible(version) {
113 wire::get_compact_string(buf)?
114 } else {
115 wire::get_string(buf)?
116 };
117 this.generation_id = wire::get_i32(buf)?;
118 this.member_id = if is_flexible(version) {
119 wire::get_compact_string(buf)?
120 } else {
121 wire::get_string(buf)?
122 };
123 if version >= 3 {
124 this.group_instance_id = if is_flexible(version) {
125 wire::get_compact_nullable_string(buf)?
126 } else {
127 wire::get_nullable_string(buf)?
128 };
129 }
130 if is_flexible(version) {
131 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
132 }
133 Ok(this)
134 }
135}
136
137impl crate::Message for HeartbeatRequest {
138 const API_KEY: i16 = 12;
139 const MIN_VERSION: i16 = 0;
140 const MAX_VERSION: i16 = 4;
141
142 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
143 HeartbeatRequest::encode(self, buf, version)
144 }
145 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
146 HeartbeatRequest::decode(buf, version)
147 }
148 fn decode_with_limits(
149 buf: &mut impl Buf,
150 version: i16,
151 limits: Limits,
152 ) -> Result<Self, DecodeError> {
153 HeartbeatRequest::decode_with_limits(buf, version, limits)
154 }
155}