kafka_protocol/messages/
controller_registration_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 ControllerRegistrationResponse {
24 pub throttle_time_ms: i32,
28
29 pub error_code: i16,
33
34 pub error_message: Option<StrBytes>,
38
39 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
41}
42
43impl ControllerRegistrationResponse {
44 pub fn with_throttle_time_ms(mut self, value: i32) -> Self {
50 self.throttle_time_ms = value;
51 self
52 }
53 pub fn with_error_code(mut self, value: i16) -> Self {
59 self.error_code = value;
60 self
61 }
62 pub fn with_error_message(mut self, value: Option<StrBytes>) -> Self {
68 self.error_message = 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 = "broker")]
84impl Encodable for ControllerRegistrationResponse {
85 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
86 types::Int32.encode(buf, &self.throttle_time_ms)?;
87 types::Int16.encode(buf, &self.error_code)?;
88 types::CompactString.encode(buf, &self.error_message)?;
89 let num_tagged_fields = self.unknown_tagged_fields.len();
90 if num_tagged_fields > std::u32::MAX as usize {
91 bail!(
92 "Too many tagged fields to encode ({} fields)",
93 num_tagged_fields
94 );
95 }
96 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
97
98 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
99 Ok(())
100 }
101 fn compute_size(&self, version: i16) -> Result<usize> {
102 let mut total_size = 0;
103 total_size += types::Int32.compute_size(&self.throttle_time_ms)?;
104 total_size += types::Int16.compute_size(&self.error_code)?;
105 total_size += types::CompactString.compute_size(&self.error_message)?;
106 let num_tagged_fields = self.unknown_tagged_fields.len();
107 if num_tagged_fields > std::u32::MAX as usize {
108 bail!(
109 "Too many tagged fields to encode ({} fields)",
110 num_tagged_fields
111 );
112 }
113 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
114
115 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
116 Ok(total_size)
117 }
118}
119
120#[cfg(feature = "client")]
121impl Decodable for ControllerRegistrationResponse {
122 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
123 let throttle_time_ms = types::Int32.decode(buf)?;
124 let error_code = types::Int16.decode(buf)?;
125 let error_message = types::CompactString.decode(buf)?;
126 let mut unknown_tagged_fields = BTreeMap::new();
127 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
128 for _ in 0..num_tagged_fields {
129 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
130 let size: u32 = types::UnsignedVarInt.decode(buf)?;
131 let unknown_value = buf.try_get_bytes(size as usize)?;
132 unknown_tagged_fields.insert(tag as i32, unknown_value);
133 }
134 Ok(Self {
135 throttle_time_ms,
136 error_code,
137 error_message,
138 unknown_tagged_fields,
139 })
140 }
141}
142
143impl Default for ControllerRegistrationResponse {
144 fn default() -> Self {
145 Self {
146 throttle_time_ms: 0,
147 error_code: 0,
148 error_message: Some(Default::default()),
149 unknown_tagged_fields: BTreeMap::new(),
150 }
151 }
152}
153
154impl Message for ControllerRegistrationResponse {
155 const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
156 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
157}
158
159impl HeaderVersion for ControllerRegistrationResponse {
160 fn header_version(version: i16) -> i16 {
161 1
162 }
163}