1use std::io::{Read, Result, Write};
4
5use serde::{Deserialize, Serialize};
6#[cfg(test)] use proptest_derive::Arbitrary;
7
8use crate::arrays::{read_array, write_array};
9use crate::markers::{ApiMessage, Response};
10use crate::readable_writable::{Readable, Writable};
11#[cfg(test)] use crate::test_utils::proptest_strategies;
12
13#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
15#[cfg_attr(test, derive(Arbitrary))]
16pub struct MetadataResponse {
17 pub throttle_time_ms: i32,
19 #[cfg_attr(test, proptest(strategy = "proptest_strategies::vec()"))]
21 pub brokers: Vec<MetadataResponseBroker>,
22 #[cfg_attr(test, proptest(strategy = "proptest_strategies::optional_string()"))]
24 pub cluster_id: Option<String>,
25 pub controller_id: i32,
27 #[cfg_attr(test, proptest(strategy = "proptest_strategies::vec()"))]
29 pub topics: Vec<MetadataResponseTopic>,
30}
31
32impl ApiMessage for MetadataResponse {
33 fn api_key(&self) -> i16 {
34 3
35 }
36
37 fn version(&self) -> i16 {
38 4
39 }
40}
41
42impl Response for MetadataResponse { }
43
44impl Default for MetadataResponse {
45 fn default() -> Self {
46 MetadataResponse {
47 throttle_time_ms: 0_i32,
48 brokers: Vec::<MetadataResponseBroker>::new(),
49 cluster_id: None,
50 controller_id: -1_i32,
51 topics: Vec::<MetadataResponseTopic>::new(),
52 }
53 }
54}
55
56impl MetadataResponse {
57 pub fn new<S1: AsRef<str>>(throttle_time_ms: i32, brokers: Vec<MetadataResponseBroker>, cluster_id: Option<S1>, controller_id: i32, topics: Vec<MetadataResponseTopic>) -> Self {
58 Self {
59 throttle_time_ms,
60 brokers,
61 cluster_id: cluster_id.map(|s| s.as_ref().to_string()),
62 controller_id,
63 topics,
64 }
65 }
66}
67
68#[cfg(test)]
69mod tests_metadata_response_new_and_default {
70 use super::*;
71
72 #[test]
73 fn test() {
74 let d = MetadataResponse::new(
75 0_i32,
76 Vec::<MetadataResponseBroker>::new(),
77 None::<String>,
78 -1_i32,
79 Vec::<MetadataResponseTopic>::new(),
80 );
81 assert_eq!(d, MetadataResponse::default());
82 }
83}
84
85impl Readable for MetadataResponse {
86 fn read(#[allow(unused)] input: &mut impl Read) -> Result<Self> {
87 let throttle_time_ms = i32::read(input)?;
88 let brokers = read_array::<MetadataResponseBroker>(input, "brokers", false)?;
89 let cluster_id = Option::<String>::read_ext(input, "cluster_id", false)?;
90 let controller_id = i32::read(input)?;
91 let topics = read_array::<MetadataResponseTopic>(input, "topics", false)?;
92 Ok(MetadataResponse {
93 throttle_time_ms, brokers, cluster_id, controller_id, topics
94 })
95 }
96}
97
98impl Writable for MetadataResponse {
99 fn write(&self, #[allow(unused)] output: &mut impl Write) -> Result<()> {
100 self.throttle_time_ms.write(output)?;
101 write_array(output, "self.brokers", &self.brokers, false)?;
102 self.cluster_id.write_ext(output, "self.cluster_id", false)?;
103 self.controller_id.write(output)?;
104 write_array(output, "self.topics", &self.topics, false)?;
105 Ok(())
106 }
107}
108
109#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
111#[cfg_attr(test, derive(Arbitrary))]
112pub struct MetadataResponseBroker {
113 pub node_id: i32,
115 #[cfg_attr(test, proptest(strategy = "proptest_strategies::string()"))]
117 pub host: String,
118 pub port: i32,
120 #[cfg_attr(test, proptest(strategy = "proptest_strategies::optional_string()"))]
122 pub rack: Option<String>,
123}
124
125impl Default for MetadataResponseBroker {
126 fn default() -> Self {
127 MetadataResponseBroker {
128 node_id: 0_i32,
129 host: String::from(""),
130 port: 0_i32,
131 rack: None,
132 }
133 }
134}
135
136impl MetadataResponseBroker {
137 pub fn new<S1: AsRef<str>, S2: AsRef<str>>(node_id: i32, host: S1, port: i32, rack: Option<S2>) -> Self {
138 Self {
139 node_id,
140 host: host.as_ref().to_string(),
141 port,
142 rack: rack.map(|s| s.as_ref().to_string()),
143 }
144 }
145}
146
147#[cfg(test)]
148mod tests_metadata_response_broker_new_and_default {
149 use super::*;
150
151 #[test]
152 fn test() {
153 let d = MetadataResponseBroker::new(
154 0_i32,
155 String::from(""),
156 0_i32,
157 None::<String>,
158 );
159 assert_eq!(d, MetadataResponseBroker::default());
160 }
161}
162
163impl Readable for MetadataResponseBroker {
164 fn read(#[allow(unused)] input: &mut impl Read) -> Result<Self> {
165 let node_id = i32::read(input)?;
166 let host = String::read_ext(input, "host", false)?;
167 let port = i32::read(input)?;
168 let rack = Option::<String>::read_ext(input, "rack", false)?;
169 Ok(MetadataResponseBroker {
170 node_id, host, port, rack
171 })
172 }
173}
174
175impl Writable for MetadataResponseBroker {
176 fn write(&self, #[allow(unused)] output: &mut impl Write) -> Result<()> {
177 self.node_id.write(output)?;
178 self.host.write_ext(output, "self.host", false)?;
179 self.port.write(output)?;
180 self.rack.write_ext(output, "self.rack", false)?;
181 Ok(())
182 }
183}
184
185#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
187#[cfg_attr(test, derive(Arbitrary))]
188pub struct MetadataResponseTopic {
189 pub error_code: i16,
191 #[cfg_attr(test, proptest(strategy = "proptest_strategies::string()"))]
193 pub name: String,
194 pub is_internal: bool,
196 #[cfg_attr(test, proptest(strategy = "proptest_strategies::vec()"))]
198 pub partitions: Vec<MetadataResponsePartition>,
199}
200
201impl Default for MetadataResponseTopic {
202 fn default() -> Self {
203 MetadataResponseTopic {
204 error_code: 0_i16,
205 name: String::from(""),
206 is_internal: false,
207 partitions: Vec::<MetadataResponsePartition>::new(),
208 }
209 }
210}
211
212impl MetadataResponseTopic {
213 pub fn new<S1: AsRef<str>>(error_code: i16, name: S1, is_internal: bool, partitions: Vec<MetadataResponsePartition>) -> Self {
214 Self {
215 error_code,
216 name: name.as_ref().to_string(),
217 is_internal,
218 partitions,
219 }
220 }
221}
222
223#[cfg(test)]
224mod tests_metadata_response_topic_new_and_default {
225 use super::*;
226
227 #[test]
228 fn test() {
229 let d = MetadataResponseTopic::new(
230 0_i16,
231 String::from(""),
232 false,
233 Vec::<MetadataResponsePartition>::new(),
234 );
235 assert_eq!(d, MetadataResponseTopic::default());
236 }
237}
238
239impl Readable for MetadataResponseTopic {
240 fn read(#[allow(unused)] input: &mut impl Read) -> Result<Self> {
241 let error_code = i16::read(input)?;
242 let name = String::read_ext(input, "name", false)?;
243 let is_internal = bool::read(input)?;
244 let partitions = read_array::<MetadataResponsePartition>(input, "partitions", false)?;
245 Ok(MetadataResponseTopic {
246 error_code, name, is_internal, partitions
247 })
248 }
249}
250
251impl Writable for MetadataResponseTopic {
252 fn write(&self, #[allow(unused)] output: &mut impl Write) -> Result<()> {
253 self.error_code.write(output)?;
254 self.name.write_ext(output, "self.name", false)?;
255 self.is_internal.write(output)?;
256 write_array(output, "self.partitions", &self.partitions, false)?;
257 Ok(())
258 }
259}
260
261#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
263#[cfg_attr(test, derive(Arbitrary))]
264pub struct MetadataResponsePartition {
265 pub error_code: i16,
267 pub partition_index: i32,
269 pub leader_id: i32,
271 #[cfg_attr(test, proptest(strategy = "proptest_strategies::vec()"))]
273 pub replica_nodes: Vec<i32>,
274 #[cfg_attr(test, proptest(strategy = "proptest_strategies::vec()"))]
276 pub isr_nodes: Vec<i32>,
277}
278
279impl Default for MetadataResponsePartition {
280 fn default() -> Self {
281 MetadataResponsePartition {
282 error_code: 0_i16,
283 partition_index: 0_i32,
284 leader_id: 0_i32,
285 replica_nodes: Vec::<i32>::new(),
286 isr_nodes: Vec::<i32>::new(),
287 }
288 }
289}
290
291impl MetadataResponsePartition {
292 pub fn new(error_code: i16, partition_index: i32, leader_id: i32, replica_nodes: Vec<i32>, isr_nodes: Vec<i32>) -> Self {
293 Self {
294 error_code,
295 partition_index,
296 leader_id,
297 replica_nodes,
298 isr_nodes,
299 }
300 }
301}
302
303#[cfg(test)]
304mod tests_metadata_response_partition_new_and_default {
305 use super::*;
306
307 #[test]
308 fn test() {
309 let d = MetadataResponsePartition::new(
310 0_i16,
311 0_i32,
312 0_i32,
313 Vec::<i32>::new(),
314 Vec::<i32>::new(),
315 );
316 assert_eq!(d, MetadataResponsePartition::default());
317 }
318}
319
320impl Readable for MetadataResponsePartition {
321 fn read(#[allow(unused)] input: &mut impl Read) -> Result<Self> {
322 let error_code = i16::read(input)?;
323 let partition_index = i32::read(input)?;
324 let leader_id = i32::read(input)?;
325 let replica_nodes = read_array::<i32>(input, "replica_nodes", false)?;
326 let isr_nodes = read_array::<i32>(input, "isr_nodes", false)?;
327 Ok(MetadataResponsePartition {
328 error_code, partition_index, leader_id, replica_nodes, isr_nodes
329 })
330 }
331}
332
333impl Writable for MetadataResponsePartition {
334 fn write(&self, #[allow(unused)] output: &mut impl Write) -> Result<()> {
335 self.error_code.write(output)?;
336 self.partition_index.write(output)?;
337 self.leader_id.write(output)?;
338 write_array(output, "self.replica_nodes", &self.replica_nodes, false)?;
339 write_array(output, "self.isr_nodes", &self.isr_nodes, false)?;
340 Ok(())
341 }
342}
343
344#[cfg(test)]
345mod tests {
346 use super::*;
347 use proptest::prelude::*;
348
349 #[test]
350 fn test_java_default() {
351 crate::test_utils::test_java_default::<MetadataResponse>("MetadataResponse", 4);
352 }
353
354 proptest! {
355 #[test]
356 fn test_serde(data: MetadataResponse) {
357 crate::test_utils::test_serde(&data)?;
358 }
359 }
360
361 proptest! {
362 #[test]
363 fn test_java_arbitrary(data: MetadataResponse) {
364 crate::test_utils::test_java_arbitrary(&data, "MetadataResponse", 4);
365 }
366 }
367}