1use bytes::BufMut;
4
5use crate::primitives::fixed::{get_bool, get_i16, get_i32, put_bool, put_i16, put_i32};
6use crate::primitives::string_bytes::{
7 compact_nullable_string_len, compact_string_len, nullable_string_len,
8 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
9 string_len,
10};
11use crate::primitives::string_bytes_borrowed::{
12 get_compact_nullable_string_borrowed, get_compact_string_borrowed,
13 get_nullable_string_borrowed, get_string_borrowed,
14};
15use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
16use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
17
18pub const API_KEY: i16 = 19;
19pub const MIN_VERSION: i16 = 2;
20pub const MAX_VERSION: i16 = 7;
21pub const FLEXIBLE_MIN: i16 = 5;
22
23#[inline]
24fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct CreateTopicsRequest<'a> {
28 pub topics: Vec<CreatableTopic<'a>>,
29 pub timeout_ms: i32,
30 pub validate_only: bool,
31 pub unknown_tagged_fields: UnknownTaggedFields,
32}
33
34impl<'a> Default for CreateTopicsRequest<'a> {
35 fn default() -> Self {
36 Self {
37 topics: Vec::new(),
38 timeout_ms: 60_000i32,
39 validate_only: false,
40 unknown_tagged_fields: Default::default(),
41 }
42 }
43}
44
45impl<'a> CreateTopicsRequest<'a> {
46 pub fn to_owned(&self) -> crate::owned::create_topics_request::CreateTopicsRequest {
47 crate::owned::create_topics_request::CreateTopicsRequest {
48 topics: (self.topics).iter().map(|it| it.to_owned()).collect(),
49 timeout_ms: (self.timeout_ms),
50 validate_only: (self.validate_only),
51 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
52 }
53 }
54}
55
56impl<'a> Encode for CreateTopicsRequest<'a> {
57 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
58 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
59 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
60 }
61 let flex = is_flexible(version);
62 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { it.encode(buf, version)?; } } }
63 if version >= 0 { put_i32(buf, self.timeout_ms) }
64 if version >= 1 { put_bool(buf, self.validate_only) }
65 if flex {
66 let tagged = WriteTaggedFields::new();
67 tagged.write(buf, &self.unknown_tagged_fields);
68 }
69 Ok(())
70 }
71 fn encoded_len(&self, version: i16) -> usize {
72 let flex = is_flexible(version);
73 let mut n: usize = 0;
74 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.topics).len(), flex); let body: usize = (self.topics).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
75 if version >= 0 { n += 4; }
76 if version >= 1 { n += 1; }
77 if flex {
78 let known_pairs: Vec<(u32, usize)> = Vec::new();
79 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
80 }
81 n
82 }
83}
84
85impl<'de> DecodeBorrow<'de> for CreateTopicsRequest<'de> {
86 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
87 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
88 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
89 }
90 let flex = is_flexible(version);
91 let mut out = Self::default();
92 if version >= 0 { out.topics = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(CreatableTopic::decode_borrow(buf, version)?); } v }; }
93 if version >= 0 { out.timeout_ms = get_i32(buf)?; }
94 if version >= 1 { out.validate_only = get_bool(buf)?; }
95 if flex {
96 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
97 Ok(false)
98 })?;
99 }
100 Ok(out)
101 }
102}
103
104#[derive(Debug, Clone, PartialEq, Eq)]
105pub struct CreatableTopic<'a> {
106 pub name: &'a str,
107 pub num_partitions: i32,
108 pub replication_factor: i16,
109 pub assignments: Vec<CreatableReplicaAssignment>,
110 pub configs: Vec<CreatableTopicConfig<'a>>,
111 pub unknown_tagged_fields: UnknownTaggedFields,
112}
113
114impl<'a> Default for CreatableTopic<'a> {
115 fn default() -> Self {
116 Self {
117 name: "",
118 num_partitions: 0i32,
119 replication_factor: 0i16,
120 assignments: Vec::new(),
121 configs: Vec::new(),
122 unknown_tagged_fields: Default::default(),
123 }
124 }
125}
126
127impl<'a> CreatableTopic<'a> {
128 pub fn to_owned(&self) -> crate::owned::create_topics_request::CreatableTopic {
129 crate::owned::create_topics_request::CreatableTopic {
130 name: (self.name).to_string(),
131 num_partitions: (self.num_partitions),
132 replication_factor: (self.replication_factor),
133 assignments: (self.assignments).iter().map(|it| it.to_owned()).collect(),
134 configs: (self.configs).iter().map(|it| it.to_owned()).collect(),
135 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
136 }
137 }
138}
139
140impl<'a> Encode for CreatableTopic<'a> {
141 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
142 let flex = version >= 5;
143 if version >= 0 { if flex { put_compact_string(buf, self.name) } else { put_string(buf, self.name) } }
144 if version >= 0 { put_i32(buf, self.num_partitions) }
145 if version >= 0 { put_i16(buf, self.replication_factor) }
146 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.assignments).len(), flex); for it in &self.assignments { it.encode(buf, version)?; } } }
147 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.configs).len(), flex); for it in &self.configs { it.encode(buf, version)?; } } }
148 if flex {
149 let tagged = WriteTaggedFields::new();
150 tagged.write(buf, &self.unknown_tagged_fields);
151 }
152 Ok(())
153 }
154 fn encoded_len(&self, version: i16) -> usize {
155 let flex = version >= 5;
156 let mut n: usize = 0;
157 if version >= 0 { n += if flex { compact_string_len(self.name) } else { string_len(self.name) }; }
158 if version >= 0 { n += 4; }
159 if version >= 0 { n += 2; }
160 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.assignments).len(), flex); let body: usize = (self.assignments).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
161 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.configs).len(), flex); let body: usize = (self.configs).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
162 if flex {
163 let known_pairs: Vec<(u32, usize)> = Vec::new();
164 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
165 }
166 n
167 }
168}
169
170impl<'de> DecodeBorrow<'de> for CreatableTopic<'de> {
171 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
172 let flex = version >= 5;
173 let mut out = Self::default();
174 if version >= 0 { out.name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
175 if version >= 0 { out.num_partitions = get_i32(buf)?; }
176 if version >= 0 { out.replication_factor = get_i16(buf)?; }
177 if version >= 0 { out.assignments = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(CreatableReplicaAssignment::decode_borrow(buf, version)?); } v }; }
178 if version >= 0 { out.configs = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(CreatableTopicConfig::decode_borrow(buf, version)?); } v }; }
179 if flex {
180 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
181 Ok(false)
182 })?;
183 }
184 Ok(out)
185 }
186}
187
188#[derive(Debug, Clone, PartialEq, Eq)]
189pub struct CreatableReplicaAssignment {
190 pub partition_index: i32,
191 pub broker_ids: Vec<i32>,
192 pub unknown_tagged_fields: UnknownTaggedFields,
193}
194
195impl Default for CreatableReplicaAssignment {
196 fn default() -> Self {
197 Self {
198 partition_index: 0i32,
199 broker_ids: Vec::new(),
200 unknown_tagged_fields: Default::default(),
201 }
202 }
203}
204
205impl CreatableReplicaAssignment {
206 pub fn to_owned(&self) -> crate::owned::create_topics_request::CreatableReplicaAssignment {
207 crate::owned::create_topics_request::CreatableReplicaAssignment {
208 partition_index: (self.partition_index),
209 broker_ids: (self.broker_ids).clone(),
210 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
211 }
212 }
213}
214
215impl Encode for CreatableReplicaAssignment {
216 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
217 let flex = version >= 5;
218 if version >= 0 { put_i32(buf, self.partition_index) }
219 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.broker_ids).len(), flex); for it in &self.broker_ids { put_i32(buf, *it); } } }
220 if flex {
221 let tagged = WriteTaggedFields::new();
222 tagged.write(buf, &self.unknown_tagged_fields);
223 }
224 Ok(())
225 }
226 fn encoded_len(&self, version: i16) -> usize {
227 let flex = version >= 5;
228 let mut n: usize = 0;
229 if version >= 0 { n += 4; }
230 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.broker_ids).len(), flex); let body: usize = (self.broker_ids).iter().map(|_| 4).sum(); prefix + body }; }
231 if flex {
232 let known_pairs: Vec<(u32, usize)> = Vec::new();
233 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
234 }
235 n
236 }
237}
238
239impl<'de> DecodeBorrow<'de> for CreatableReplicaAssignment {
240 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
241 let flex = version >= 5;
242 let mut out = Self::default();
243 if version >= 0 { out.partition_index = get_i32(buf)?; }
244 if version >= 0 { out.broker_ids = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(get_i32(buf)?); } v }; }
245 if flex {
246 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
247 Ok(false)
248 })?;
249 }
250 Ok(out)
251 }
252}
253
254#[derive(Debug, Clone, PartialEq, Eq)]
255pub struct CreatableTopicConfig<'a> {
256 pub name: &'a str,
257 pub value: Option<&'a str>,
258 pub unknown_tagged_fields: UnknownTaggedFields,
259}
260
261impl<'a> Default for CreatableTopicConfig<'a> {
262 fn default() -> Self {
263 Self {
264 name: "",
265 value: None,
266 unknown_tagged_fields: Default::default(),
267 }
268 }
269}
270
271impl<'a> CreatableTopicConfig<'a> {
272 pub fn to_owned(&self) -> crate::owned::create_topics_request::CreatableTopicConfig {
273 crate::owned::create_topics_request::CreatableTopicConfig {
274 name: (self.name).to_string(),
275 value: (self.value).map(|s| s.to_string()),
276 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
277 }
278 }
279}
280
281impl<'a> Encode for CreatableTopicConfig<'a> {
282 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
283 let flex = version >= 5;
284 if version >= 0 { if flex { put_compact_string(buf, self.name) } else { put_string(buf, self.name) } }
285 if version >= 0 { if flex { put_compact_nullable_string(buf, self.value) } else { put_nullable_string(buf, self.value) } }
286 if flex {
287 let tagged = WriteTaggedFields::new();
288 tagged.write(buf, &self.unknown_tagged_fields);
289 }
290 Ok(())
291 }
292 fn encoded_len(&self, version: i16) -> usize {
293 let flex = version >= 5;
294 let mut n: usize = 0;
295 if version >= 0 { n += if flex { compact_string_len(self.name) } else { string_len(self.name) }; }
296 if version >= 0 { n += if flex { compact_nullable_string_len(self.value) } else { nullable_string_len(self.value) }; }
297 if flex {
298 let known_pairs: Vec<(u32, usize)> = Vec::new();
299 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
300 }
301 n
302 }
303}
304
305impl<'de> DecodeBorrow<'de> for CreatableTopicConfig<'de> {
306 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
307 let flex = version >= 5;
308 let mut out = Self::default();
309 if version >= 0 { out.name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
310 if version >= 0 { out.value = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
311 if flex {
312 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
313 Ok(false)
314 })?;
315 }
316 Ok(out)
317 }
318}