kafka_protocol/messages/
find_coordinator_request.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 FindCoordinatorRequest {
24 pub key: StrBytes,
28
29 pub key_type: i8,
33
34 pub coordinator_keys: Vec<StrBytes>,
38
39 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
41}
42
43impl FindCoordinatorRequest {
44 pub fn with_key(mut self, value: StrBytes) -> Self {
50 self.key = value;
51 self
52 }
53 pub fn with_key_type(mut self, value: i8) -> Self {
59 self.key_type = value;
60 self
61 }
62 pub fn with_coordinator_keys(mut self, value: Vec<StrBytes>) -> Self {
68 self.coordinator_keys = value;
69 self
70 }
71 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
73 self.unknown_tagged_fields = value;
74 self
75 }
76 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
78 self.unknown_tagged_fields.insert(key, value);
79 self
80 }
81}
82
83#[cfg(feature = "client")]
84impl Encodable for FindCoordinatorRequest {
85 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
86 if version < 0 || version > 6 {
87 bail!("specified version not supported by this message type");
88 }
89 if version <= 3 {
90 if version >= 3 {
91 types::CompactString.encode(buf, &self.key)?;
92 } else {
93 types::String.encode(buf, &self.key)?;
94 }
95 } else {
96 if !self.key.is_empty() {
97 bail!("A field is set that is not available on the selected protocol version");
98 }
99 }
100 if version >= 1 {
101 types::Int8.encode(buf, &self.key_type)?;
102 } else {
103 if self.key_type != 0 {
104 bail!("A field is set that is not available on the selected protocol version");
105 }
106 }
107 if version >= 4 {
108 types::CompactArray(types::CompactString).encode(buf, &self.coordinator_keys)?;
109 } else {
110 if !self.coordinator_keys.is_empty() {
111 bail!("A field is set that is not available on the selected protocol version");
112 }
113 }
114 if version >= 3 {
115 let num_tagged_fields = self.unknown_tagged_fields.len();
116 if num_tagged_fields > std::u32::MAX as usize {
117 bail!(
118 "Too many tagged fields to encode ({} fields)",
119 num_tagged_fields
120 );
121 }
122 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
123
124 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
125 }
126 Ok(())
127 }
128 fn compute_size(&self, version: i16) -> Result<usize> {
129 let mut total_size = 0;
130 if version <= 3 {
131 if version >= 3 {
132 total_size += types::CompactString.compute_size(&self.key)?;
133 } else {
134 total_size += types::String.compute_size(&self.key)?;
135 }
136 } else {
137 if !self.key.is_empty() {
138 bail!("A field is set that is not available on the selected protocol version");
139 }
140 }
141 if version >= 1 {
142 total_size += types::Int8.compute_size(&self.key_type)?;
143 } else {
144 if self.key_type != 0 {
145 bail!("A field is set that is not available on the selected protocol version");
146 }
147 }
148 if version >= 4 {
149 total_size +=
150 types::CompactArray(types::CompactString).compute_size(&self.coordinator_keys)?;
151 } else {
152 if !self.coordinator_keys.is_empty() {
153 bail!("A field is set that is not available on the selected protocol version");
154 }
155 }
156 if version >= 3 {
157 let num_tagged_fields = self.unknown_tagged_fields.len();
158 if num_tagged_fields > std::u32::MAX as usize {
159 bail!(
160 "Too many tagged fields to encode ({} fields)",
161 num_tagged_fields
162 );
163 }
164 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
165
166 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
167 }
168 Ok(total_size)
169 }
170}
171
172#[cfg(feature = "broker")]
173impl Decodable for FindCoordinatorRequest {
174 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
175 if version < 0 || version > 6 {
176 bail!("specified version not supported by this message type");
177 }
178 let key = if version <= 3 {
179 if version >= 3 {
180 types::CompactString.decode(buf)?
181 } else {
182 types::String.decode(buf)?
183 }
184 } else {
185 Default::default()
186 };
187 let key_type = if version >= 1 {
188 types::Int8.decode(buf)?
189 } else {
190 0
191 };
192 let coordinator_keys = if version >= 4 {
193 types::CompactArray(types::CompactString).decode(buf)?
194 } else {
195 Default::default()
196 };
197 let mut unknown_tagged_fields = BTreeMap::new();
198 if version >= 3 {
199 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
200 for _ in 0..num_tagged_fields {
201 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
202 let size: u32 = types::UnsignedVarInt.decode(buf)?;
203 let unknown_value = buf.try_get_bytes(size as usize)?;
204 unknown_tagged_fields.insert(tag as i32, unknown_value);
205 }
206 }
207 Ok(Self {
208 key,
209 key_type,
210 coordinator_keys,
211 unknown_tagged_fields,
212 })
213 }
214}
215
216impl Default for FindCoordinatorRequest {
217 fn default() -> Self {
218 Self {
219 key: Default::default(),
220 key_type: 0,
221 coordinator_keys: Default::default(),
222 unknown_tagged_fields: BTreeMap::new(),
223 }
224 }
225}
226
227impl Message for FindCoordinatorRequest {
228 const VERSIONS: VersionRange = VersionRange { min: 0, max: 6 };
229 const DEPRECATED_VERSIONS: Option<VersionRange> = Some(VersionRange { min: 0, max: 0 });
230}
231
232impl HeaderVersion for FindCoordinatorRequest {
233 fn header_version(version: i16) -> i16 {
234 if version >= 3 {
235 2
236 } else {
237 1
238 }
239 }
240}