odradek_protocol/messages/
sync_group_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 SyncGroupResponse {
31 pub throttle_time_ms: i32,
33 pub error_code: i16,
35 pub protocol_type: Option<String>,
37 pub protocol_name: Option<String>,
39 pub assignment: bytes::Bytes,
41 pub unknown_tagged_fields: Vec<RawTaggedField>,
43}
44
45impl Default for SyncGroupResponse {
46 fn default() -> Self {
47 Self {
48 throttle_time_ms: 0,
49 error_code: 0,
50 protocol_type: None,
51 protocol_name: None,
52 assignment: bytes::Bytes::new(),
53 unknown_tagged_fields: Vec::new(),
54 }
55 }
56}
57
58impl SyncGroupResponse {
59 pub const API_KEY: i16 = 14;
61 pub const MIN_VERSION: i16 = 0;
63 pub const MAX_VERSION: i16 = 5;
65
66 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
68 if version >= 1 {
69 buf.put_i32(self.throttle_time_ms);
70 }
71 buf.put_i16(self.error_code);
72 if version >= 5 {
73 if is_flexible(version) {
74 wire::put_compact_nullable_string(buf, self.protocol_type.as_deref());
75 } else {
76 wire::put_nullable_string(buf, self.protocol_type.as_deref())?;
77 }
78 }
79 if version >= 5 {
80 if is_flexible(version) {
81 wire::put_compact_nullable_string(buf, self.protocol_name.as_deref());
82 } else {
83 wire::put_nullable_string(buf, self.protocol_name.as_deref())?;
84 }
85 }
86 if is_flexible(version) {
87 wire::put_compact_bytes(buf, &self.assignment);
88 } else {
89 wire::put_bytes(buf, &self.assignment)?;
90 }
91 if is_flexible(version) {
92 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
93 }
94 Ok(())
95 }
96
97 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
100 Self::decode_with_limits(buf, version, Limits::default())
101 }
102
103 pub fn decode_with_limits(
105 buf: &mut impl Buf,
106 version: i16,
107 limits: Limits,
108 ) -> Result<Self, DecodeError> {
109 let mut budget = limits.budget(buf.remaining());
110 Self::decode_with_budget(buf, version, &mut budget)
111 }
112
113 pub fn decode_with_budget(
115 buf: &mut impl Buf,
116 version: i16,
117 budget: &mut Budget,
118 ) -> Result<Self, DecodeError> {
119 let mut this = Self::default();
120 if version >= 1 {
121 this.throttle_time_ms = wire::get_i32(buf)?;
122 }
123 this.error_code = wire::get_i16(buf)?;
124 if version >= 5 {
125 this.protocol_type = if is_flexible(version) {
126 wire::get_compact_nullable_string(buf)?
127 } else {
128 wire::get_nullable_string(buf)?
129 };
130 }
131 if version >= 5 {
132 this.protocol_name = if is_flexible(version) {
133 wire::get_compact_nullable_string(buf)?
134 } else {
135 wire::get_nullable_string(buf)?
136 };
137 }
138 this.assignment = {
139 let raw = if is_flexible(version) {
140 wire::get_compact_nullable_bytes(buf)?
141 } else {
142 wire::get_nullable_bytes(buf)?
143 };
144 match raw {
145 Some(v) => v,
146 None => return Err(DecodeError::InvalidLength(-1)),
147 }
148 };
149 if is_flexible(version) {
150 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
151 }
152 Ok(this)
153 }
154}
155
156impl crate::Message for SyncGroupResponse {
157 const API_KEY: i16 = 14;
158 const MIN_VERSION: i16 = 0;
159 const MAX_VERSION: i16 = 5;
160
161 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
162 SyncGroupResponse::encode(self, buf, version)
163 }
164 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
165 SyncGroupResponse::decode(buf, version)
166 }
167 fn decode_with_limits(
168 buf: &mut impl Buf,
169 version: i16,
170 limits: Limits,
171 ) -> Result<Self, DecodeError> {
172 SyncGroupResponse::decode_with_limits(buf, version, limits)
173 }
174 fn throttle_time_ms(&self) -> Option<i32> {
175 Some(self.throttle_time_ms)
176 }
177}